SMTP的发送方法总是抛出异常。消息是:发送邮件失败
这是我的代码:
MailMessage mm = new MailMessage();
mm.To.Add("Yuvraj.dhot@xxxxx.co.in");
mm.From = new MailAddress("Kumar.Chaudhari@xxxxx.co.in");
mm.Subject = "Ant Subject";
mm.Body = "Body Cha MEssag here ";
SmtpClient ss = new SmtpClient("localhost");
ss.EnableSsl = false;
try
{
**ss.Send(mm);**
Result.Text = "Message Sent";
Result.ForeColor = System.Drawing.Color.Green;
}
catch (SmtpException ex)
{
Result.Text = "Message Not Sent : \n\n " + ex.Message;
Result.ForeColor = System.Drawing.Color.Red;
}
我也尝试过使用
ss.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
现在它没有抛出任何异常,它执行正常,但接收者在他的收件箱中没有收到任何邮件。
我该如何解决这个问题?
编辑 - 这是我正在获取的堆栈跟踪
Message=Failure sending mail. Source=System StackTrace: at System.Net.Mail.SmtpClient.Send
(MailMessage message) at WebApplication1._Default.Page_Load(Object sender, EventArgs e) in D:\Emcure-
Kumar\Work\Not in Use\WebApplication1\WebApplication1\Default.aspx.cs:line 30 InnerException:
System.Net.WebException Message=Unable to connect to the remote server Source=System StackTrace: –
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async,
IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at
System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout,
GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject,
GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject,
GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at
System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at
System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)
InnerException: System.Net.Sockets.SocketException Message=No connection could be made because the
target machine actively refused it 127.0.0.1:25 Source=System ErrorCode=10061 NativeErrorCode=10061
答案 0 :(得分:2)
您的execption应包含的信息多于“发送邮件失败”
有关调试的信息,请查看此链接,了解有关SmtpClient.Send方法引发的Exeptions的详细信息 - > SmtpClient.Send Method
此代码适用于“smtp.google.com”
MailMessage mm = new MailMessage();
mm.From = new MailAddress("xx");
mm.To.Add("xx");
mm.Subject = "Ant Subject";
mm.Body = "Body Cha MEssag here ";
SmtpClient ss = new SmtpClient();
ss.Host="smtp.gmail.com";
ss.Port = 587;
ss.EnableSsl = true;
ss.Credentials = new System.Net.NetworkCredential("xx", "xx");
try
{
ss.Send(mm);
Label1.Text = "Message Sent";
Label1.ForeColor = System.Drawing.Color.Green;
}
catch (SmtpException ex)
{
Label1.Text = "Message Not Sent : \n\n " + ex.Message;
Label1.ForeColor = System.Drawing.Color.Red;
}
Google要求启用SSL并将端口587作为传出端口。您还需要一个Google帐户才能获得凭据。
您的代码没有任何问题 - 它很可能是您的服务器或防火墙
答案 1 :(得分:1)
首先尝试在web.config中使用它:
<system.net>
<mailSettings>
<!-- Use this setting for development
<smtp deliveryMethod="Network">
<network host="mail.mydomain.com" port="25" />
</smtp>
-->
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Tmp"/>
</smtp>
</mailSettings>
</system.net>
这会将所有文件复制到C:\ Tmp中。 你可以像这样实例化这个类:
SmtpClient client = new SmtpClient();
然后更改web.config中的配置。试一试,如果有帮助,请告诉我们。
答案 2 :(得分:1)
我遇到了同样的问题,并且是我的代理,我真的不知道,因为我的ie具有良好配置的代理,并且我读到C#app获得OS代理的配置。我只是将我的锥形改为没有代理的一些并且运行得很好。所有解决方案,第1,第2和第3。也许是代理或防火墙,或尝试在其他PC上的程序来确认。我希望能帮助你。
答案 3 :(得分:0)
试试这个
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
// System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
MailAddress fromAddress = new MailAddress(txt_name.Text, txt_to.Text);
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 25;
// msg.From = new System.Net.Mail.MailAddress("xyz@gmail.com");
message.From = fromAddress;
message.To.Add("xyz111@gmail.com");
message.Body = txt_des.Text;
smtpClient.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "xyz@gmail.com";
NetworkCred.Password = "xtz";
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = NetworkCred;
smtpClient.Send(message);
lblStatus.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblStatus.Text = "Send Email Failed." + ex.Message;
}