无法通过 ASP.NET 应用程序发送电子邮件
Cannot send emails through ASP.NET application
我在专用服务器 "Windows Server 2012 R2" 上有一个 ASP.NET 应用程序 运行。网站全程使用SSL加密,证书正确安装在服务器上,通过不同浏览器访问时显示有效。
我现在想通过我的应用程序发送电子邮件,为此我按照本文中的说明设置了一个 SMTP 服务器。
http://www.vsysad.com/2014/09/setup-and-configure-smtp-server-on-windows-server-2012/
按照上面指定的方式设置 SMTP 服务器后,我可以使用 powershell 发送电子邮件,并且发送没有问题。但是,当我通过我的应用程序执行相同操作时,出现以下错误
Exception Type: System.Security.Authentication.AuthenticationException
Exception: The remote certificate is invalid according to the validation procedure.
Stack Trace:
at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Mail.SmtpConnection.Flush()
at System.Net.Mail.EHelloCommand.Send(SmtpConnection conn, String domain)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at EmailSender.SendMailSingleRecipient(String recipient, String subject, String message)
从我的应用程序发送电子邮件的代码非常简单,如下所示。
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("no-reply@mysite.com");
mailMessage.To.Add("myemail@email.com");
mailMessage.Subject = "subject"
mailMessage.Body = "message"
SmtpClient smtpClient = new SmtpClient("localhost", 25);
smtpClient.Send(mailMessage);
我看到同样的问题被报告了很多次,但没有一个答案能解决我的问题。有人可以解释这个错误背后的原因和可能 causes/solutions.
问题是,您需要设置 Credentials
。你的 powershell 脚本似乎 运行 在另一个用户上下文中而不是你的 asp.net 主机,所以它有效,但你的 asp.net 应用程序没有。
随便看看:How to set username and password for SmtpClient object in .NET?
这可能是服务器上的 reson SMTP 服务没有使用与 https 相同的证书。
检查这个功能。
protected void SendMail()
{
var fromAddress = "youremail@gmail.com";// Gmail Address from where you send the mail
var toAddress = YourEmail.Text.ToString();
const string fromPassword = "password";//Password of your gmail address
string email = YourEmail.Text.ToString();
string body = "First Name: " + YourName.Text + "\n";
body += "Last Name: " + YourlName.Text + "\n";
body += "Email: " + YourEmail.Text + "\n";
body += "mobile no.: " + Yourmobi.Text + "\n";
body += "Question: \n" + Comments.Text + "\n";
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
smtp.Send(toAddress, fromAddress, email, body);
try
{
lblMsgSend.Text = "Your Comments after sending the mail";
lblMsgSend.Visible = true;
Yourmobi.Text = "";
YourEmail.Text = "";
YourName.Text = "";
YourlName.Text = "";
Comments.Text = "";
}
catch (Exception) { }
}
我设法找到了我最初问题的解决方案。我没有直接从我的应用程序发送,而是将我的 SMTP 服务器设置为使用拾取目录。
现在应用代码很简单,指定配送方式和取货目录也可以移到配置文件中。
SmtpClient client = new SmtpClient("localhost", 25);
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = @"C:\inetpub\mailroot\Pickup";
client.Send(mail);
此方法的另一个优点是无需在开发机器上设置 SMTP 即可测试应用程序生成的电子邮件。通过查看拾取目录,您可以确定应用程序是否正常工作。
在我的实时服务器上使用这种方法,我几乎在从我的应用程序发送电子邮件后立即收到电子邮件,因此性能非常好。
我在专用服务器 "Windows Server 2012 R2" 上有一个 ASP.NET 应用程序 运行。网站全程使用SSL加密,证书正确安装在服务器上,通过不同浏览器访问时显示有效。
我现在想通过我的应用程序发送电子邮件,为此我按照本文中的说明设置了一个 SMTP 服务器。 http://www.vsysad.com/2014/09/setup-and-configure-smtp-server-on-windows-server-2012/
按照上面指定的方式设置 SMTP 服务器后,我可以使用 powershell 发送电子邮件,并且发送没有问题。但是,当我通过我的应用程序执行相同操作时,出现以下错误
Exception Type: System.Security.Authentication.AuthenticationException
Exception: The remote certificate is invalid according to the validation procedure.
Stack Trace:
at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Mail.SmtpConnection.Flush()
at System.Net.Mail.EHelloCommand.Send(SmtpConnection conn, String domain)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at EmailSender.SendMailSingleRecipient(String recipient, String subject, String message)
从我的应用程序发送电子邮件的代码非常简单,如下所示。
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("no-reply@mysite.com");
mailMessage.To.Add("myemail@email.com");
mailMessage.Subject = "subject"
mailMessage.Body = "message"
SmtpClient smtpClient = new SmtpClient("localhost", 25);
smtpClient.Send(mailMessage);
我看到同样的问题被报告了很多次,但没有一个答案能解决我的问题。有人可以解释这个错误背后的原因和可能 causes/solutions.
问题是,您需要设置 Credentials
。你的 powershell 脚本似乎 运行 在另一个用户上下文中而不是你的 asp.net 主机,所以它有效,但你的 asp.net 应用程序没有。
随便看看:How to set username and password for SmtpClient object in .NET?
这可能是服务器上的 reson SMTP 服务没有使用与 https 相同的证书。
检查这个功能。
protected void SendMail()
{
var fromAddress = "youremail@gmail.com";// Gmail Address from where you send the mail
var toAddress = YourEmail.Text.ToString();
const string fromPassword = "password";//Password of your gmail address
string email = YourEmail.Text.ToString();
string body = "First Name: " + YourName.Text + "\n";
body += "Last Name: " + YourlName.Text + "\n";
body += "Email: " + YourEmail.Text + "\n";
body += "mobile no.: " + Yourmobi.Text + "\n";
body += "Question: \n" + Comments.Text + "\n";
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
smtp.Send(toAddress, fromAddress, email, body);
try
{
lblMsgSend.Text = "Your Comments after sending the mail";
lblMsgSend.Visible = true;
Yourmobi.Text = "";
YourEmail.Text = "";
YourName.Text = "";
YourlName.Text = "";
Comments.Text = "";
}
catch (Exception) { }
}
我设法找到了我最初问题的解决方案。我没有直接从我的应用程序发送,而是将我的 SMTP 服务器设置为使用拾取目录。
现在应用代码很简单,指定配送方式和取货目录也可以移到配置文件中。
SmtpClient client = new SmtpClient("localhost", 25);
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = @"C:\inetpub\mailroot\Pickup";
client.Send(mail);
此方法的另一个优点是无需在开发机器上设置 SMTP 即可测试应用程序生成的电子邮件。通过查看拾取目录,您可以确定应用程序是否正常工作。
在我的实时服务器上使用这种方法,我几乎在从我的应用程序发送电子邮件后立即收到电子邮件,因此性能非常好。