Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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
C#
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;
           }
       }
Posted
Updated 12-Mar-12 20:57pm
v2
Comments
André Kraak 13-Mar-12 2:58am    
Edited question:
Added pre tags
Formatted text/code
Spelling/Grammar
Sergey Alexandrovich Kryukov 13-Mar-12 3:00am    
You had everything. You are given the type "System.Net.Mail.MailAddress" in the error message. Why not looking at the MSDN help page on MSDN class? MSDN broken? This should be your first action before asking such questions.
--SA

Why should it implicitly convert something? This is C#. Nothing is converted implicitly unless implicit type conversion operator is defined.

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

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

—SA
 
Share this answer
 
Hi there..

try this.

C#
message.From = (MailAddress)textBox1.Text;


All the best..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900