Sending mail error: "Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAdd
I'm using Visual Studio 2005 to create an application that sends email, using authorization. But for some reason i get the error
| Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress |
|
using this code:
SmtpClient server = new SmtpClient("my.server"); server.Credentials = new NetworkCredential("user", "pass"); MailMessage epost = new MailMessage(); email.From = "from"; email.To = "to"; email.Subject = "subject"; email.Body = "message"; try { server.Send(email); } catch (SmtpFailedRecipientException error) { MessageBox.Show("error: " + error.Message + "\nFailing recipient: " + error.FailedRecipient); } |
|
Does anyone know whats wrong?
Hi,
There are actually 2 issues with the code snippet. Here is the working one below:
1.] You need to use the MailAddress class for assigning the From Address
2.] You need to actually Add to the MailAddressCollection exposed by the To field and cannot assign an address directly.
| |
SmtpClient server = new SmtpClient("my.server"); server.Credentials = new NetworkCredential("user", "pass"); MailMessage email = new MailMessage(); email.From = new MailAddress("FromAddress"); email.To.Add("ToAddress"); email.Subject = "subject"; email.Body = "message"; try{ server.Send(email); } catch (SmtpFailedRecipientException error) { MessageBox.Show("error: " + error.Message + "\nFailing recipient: " + error.FailedRecipient); }
|
Regards,
Vikram