本文介绍了在尝试从c#console应用程序发送邮件时,我得到的是“SmtpExceptionunhandled”导致发送邮件失败的错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void SendEmail(string message)

{

MailMessage mesg = new MailMessage();

MailAddress sender = new MailAddress(ConfigurationManager .AppSettings [smtpUser]);

MailAddress receiver = new MailAddress(ConfigurationManager.AppSettings [smtpUser]);

SmtpClient smtpc = new SmtpClient()

{

Host = ConfigurationManager.AppSettings [smtpServer],

Port = Convert.ToInt32(ConfigurationManager.AppSettings [smtpPort]),

EnableSsl = true,

Credentials = new NetworkCredential(ConfigurationManager.AppSettings [smtpUser],ConfigurationManager.AppSettings [smtpPass]),

DeliveryMethod = SmtpDeliveryMethod.Network

};

mesg.From =发件人;

mesg.To.Add(接收者);

mesg.Body =消息;

smtpc.Send(mesg);

smtpc.Dispose ();

}



这是我的app.config代码:



public void SendEmail(string message)
{
MailMessage mesg = new MailMessage();
MailAddress sender = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
MailAddress receiver = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
SmtpClient smtpc = new SmtpClient()
{
Host = ConfigurationManager.AppSettings["smtpServer"],
Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]),
EnableSsl = true,
Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"]),
DeliveryMethod = SmtpDeliveryMethod.Network
};
mesg.From = sender;
mesg.To.Add(receiver);
mesg.Body = message;
smtpc.Send(mesg);
smtpc.Dispose();
}

Here is my app.config code:

<appSettings>
    <add key="webpages:Version" value="1.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="smtpServer" value="smtp.gmail.com" />
    <add key="EnableSsl" value = "true"/>
    <add key="smtpPort" value="25" />
    <add key="smtpUser" value="[email protected]" />
    <add key="smtpPass" value="mailpasswordisgiven" />
  </appSettings>

推荐答案

<system.net>
  <mailsettings>
    <smtp from="Sender's display name <[email protected]>">
      <network host="mailserver.yourdomain.com" port="25" username="smtp_server_username" password="secret" defaultcredentials="false" />
    </smtp>
  </mailsettings>
</system.net>



归功于 [ ]。



其次异常不完整,发送邮件失败。实际例外稍后,



1.删除名称无法解析。

2.无法连接到远程服务器。 />


实际上,它们都是因为唯一的原因,未建立连接。由于很多因素。您应该考虑调试应用程序,看看有什么问题。您还必须考虑查看异常消息。它会让您知道解决问题所需的操作。一旦建立连接,用户名/密码问题就会出现在此阶段的后期。在这种情况下的例外是 SMTP服务器需要安全连接...... 。在你的情况下,这还不是问题。 :-)



您可能有兴趣阅读我的这篇文章, []。


Credit to this SO thread[^].

Secondly the exception is incomplete, Failure sending mail. The actual exception comes later,

1. Remove name could not be resolved.
2. Unable to connect to remote server.

In real, they are all because of the only thing, connection was not established. Due to many factors. You should consider debugging the application, and see what is wrong. You must also consider giving a look at the exception message. It will let you know what you need to do to resolve the problem. Username/password problems comes later this stage, once the connection has been established. The exception in that case is "SMTP server requires a secure connection...". In your case, that is not yet the problem. :-)

You may be interested in reading this article of mine, Sending emails over .NET framework, and general problems – using C# code[^].


这篇关于在尝试从c#console应用程序发送邮件时,我得到的是“SmtpExceptionunhandled”导致发送邮件失败的错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 21:51