为什么出错-无法将类型'string'隐式转换为'System.Net.Mail.MailAddress' [英] Why error - Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress'

查看:66
本文介绍了为什么出错-无法将类型'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("contact@kamnatrust.com");
               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("admin1@yoursite.com")
               //message.CC.Add("admin1@yoursite.com");
               //message.CC.Add("admin2@yoursite.com");

               // You can specify Address directly as string
               // message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
               // message.Bcc.Add(new MailAddress("admin4@yoursite.com"));

               //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;
           }
       }

推荐答案

为什么要隐式转换某些内容?这是C#.除非定义了隐式类型转换运算符,否则不会隐式转换任何内容.

使用构造函数
Why should it implicitly convert something? This is C#. Nothing is converted implicitly unless implicit type conversion operator is defined.

Use the constructor
message.From = new System.Net.Mail.MailAddress(textBox1.Text);


请参阅:
http://msdn.microsoft.com/en-us/library/system. net.mail.mailaddress.aspx [ ^ ].

—SA


Please see:
http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx[^].

—SA


您好..

试试这个.

Hi there..

try this.

message.From = (MailAddress)textBox1.Text;



一切顺利.



All the best..


这篇关于为什么出错-无法将类型'string'隐式转换为'System.Net.Mail.MailAddress'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆