本文介绍了为什么出错-无法将类型'string'隐式转换为'System.Net.Mail.MailAddress'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么此错误给出
Why is this error giving
Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress' in the line message.From = textBox1.Text;
我的代码在下面
My code is given below
private void button29_Click(object sender, EventArgs e)
{
// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(textBox1.Text, textBox2.Text);
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "gmail.com";
//Default port will be 25
smtpClient.Port = 25;
//From address will be given as a MailAddress Object
message.From = textBox1.Text;
// To address collection of MailAddress
message.To.Add("[email protected]");
message.Subject = "Feedback";
// CC and BCC optional
// MailAddressCollection class is used to send the email to various users
// You can specify Address as new MailAddress("[email protected]")
//message.CC.Add("[email protected]");
//message.CC.Add("[email protected]");
// You can specify Address directly as string
// message.Bcc.Add(new MailAddress("[email protected]"));
// message.Bcc.Add(new MailAddress("[email protected]"));
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;
// Message body content
message.Body =richTextBox1.Text;
// Send SMTP mail
smtpClient.Send(message);
label1.Text = "Email successfully sent.";
}
catch (Exception ex)
{
label1.Text= "Send Email Failed." + ex.Message;
}
}
推荐答案
message.From = new System.Net.Mail.MailAddress(textBox1.Text);
请参阅:
http://msdn.microsoft.com/en-us/library/system. net.mail.mailaddress.aspx [ ^ ].
Please see:
http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx[^].
message.From = (MailAddress)textBox1.Text;
一切顺利.
All the best..
这篇关于为什么出错-无法将类型'string'隐式转换为'System.Net.Mail.MailAddress'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!