SMTP Exception when trying to send mail in C#
I am using the following very simple code and I keep getting System.Net.Mail.SmtpException
failure sending mail. This is my code:
static void main(string[] args)
{
MailAddress from = new MailAddress("MyEmail@gmail.com", "Mr. Test");
MailAddress to = new MailAddress("AnotherEmail@gmail.com", "mr. man");
MailMessage msg = new MailMessage(from, to);
开发者_StackOverflow中文版 msg.Subject = "email";
msg.Body = "This is email.";
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Send(msg);
}
I have never tried programmatically sending email before so I appreciate the help.
You're missing credentials and sending as TLS (secured connection):
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
More Details here: Sending email through Gmail SMTP server with C#
First thing I see incorrect with the code given is there is no username/password given for validation with the smtp server.
Also, to get a better idea of what exactly is causing the SmtpException, catch the exception in your debugger and look at the details of the exception. I've gotten good explanations of what is causing SMTP errors by doing this.
You can try following these directions to send mail using SMTP via gmail. http://blogs.msdn.com/b/mariya/archive/2006/06/15/633007.aspx
Google does not want you to use port 25, they want you to use 587 (ssl) or 467. They also require that you authenticate when sending mail.
精彩评论