发送电子邮件的问题

SMTP的发送方法总是抛出exception。 消息是: 发送邮件失败

这是我的代码:

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; 

现在它没有抛出任何exception,它执行正常,但接收器在他的收件箱中没有收到任何邮件。

我怎样才能解决这个问题?

编辑 – 这是我正在获得的堆栈跟踪

  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 

你的execption应该包含比“发送邮件失败”更多的信息

有关调试的信息,请查看此链接,了解有关SmtpClient.Send方法引发的Exeptions的详细信息 – > SmtpClient.Send方法

此代码适用于“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帐户才能获得凭据。

您的代码没有任何问题 – 它很可能是您的服务器或防火墙

首先尝试在web.config中使用它:

          

这会将所有文件复制到C:\ Tmp中。 您可以像这样实例化类:

 SmtpClient client = new SmtpClient(); 

然后更改web.config中的配置。 试一试让我们知道这是否有帮助。

我有同样的问题,并且是我的代理,我真的不知道,因为我的ie具有良好配置的代理,并且我读到C#app获得OS代理的配置。 我只是将我的锥形改为没有代理的一些并且运行得很好。 所有解决方案,第1,第2和第3。 也许是代理或防火墙之类的东西,或者在其他PC上尝试你的程序来确认。 我希望能帮助你。

试试这个

  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; }