简体   繁体   English

在ASP.NET MVC应用程序中发送电子邮件时出现异常

[英]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: 我正在尝试测试在asp.net mvc应用程序中发送电子邮件,但我不断收到以下异常消息:

"An attempt was made to access a socket in a way forbidden by its access permissions ipaddress:587" “试图以其访问许可ipaddress:587禁止的方式访问套接字”

Any ideas what it could be? 有什么想法吗? I have turned off windows firewall and I still get the exception. 我已经关闭了Windows防火墙,但仍然出现异常。 I have included the using System.Net.Mail; 我已经包含了使用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)
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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