简体   繁体   中英

Exception when sending email in asp.net mvc application

I am trying to test out sending an email in my asp.net mvc application but I keep getting the exception below thrown:

"An attempt was made to access a socket in a way forbidden by its access permissions ipaddress:587"

Any ideas what it could be? I have turned off windows firewall and I still get the exception. I have included the using System.Net.Mail; statement and my code is below:

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
SmtpServer.Credentials = new System.Net.NetworkCredential("info@mydomain.com", "mypassword");

mail.From = new MailAddress("info@mydomain.com");
mail.To.Add("myemail@gmail.com");
mail.Subject = "Testing application";
mail.Body = "Testing the application email";   
SmtpServer.Send(mail);

This is a code that working. Have two extra setting compare it with yours, maybe this is the issue.

MailMessage msgMail = new MailMessage("a@gmail.com", "b@mail.me", "subject", "message body");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential("a@gmail.com", "a");
try
{
   smtp.Send(msgMail);
}
catch (Exception ex)
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM