2011-03-25 231 views
1

SMTP的发送方法始终引发异常。该消息是:发送邮件失败发送电子邮件的问题

这里是我的代码:

MailMessage mm = new MailMessage(); 
mm.To.Add("[email protected]"); 

mm.From = new MailAddress("[email protected]"); 
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

发送邮件失败 – kumar 2011-03-25 08:42:34

+0

@他有,它是“发送邮件失败”。 – 2011-03-25 08:43:56

+3

您可以发布完整的详细信息(类型和* all *属性,包括堆栈跟踪和innerException类型和* all *属性,如果它不为null),而不仅仅是消息。 – Richard 2011-03-25 08:59:09

回答

0

尝试在你的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之后。试试看,如果这有帮助,请告诉我们。

+0

它不工作的敌人我 – kumar 2011-03-25 09:22:46

+0

System.Net.Mail.SmtpException被捕获 消息=发送邮件失败。 Source = System StackTrace: at System.Net.Mail.SmtpClient.Send(MailMessage message) at WebApplication1._Default。D:\ Emcure-Kumar \ Work \中的Page_Load(Object sender,EventArgs e)\未使用\ WebApplication1 \ WebApplication1 \ Default.aspx.cs:line 30 InnerException:System.IO.DirectoryNotFoundException Message =找不到零件的路径'C:\ Tmp \ cd7da094-97a5-4057-af9d-e17a842b89c4.eml'。 – kumar 2011-03-25 09:23:16

+0

是否存在“c:\ Tmp”? – 2011-03-25 09:25:30

2

你execption应包含比“发送邮件失败”

对于这个链接调试找对方法SmtpClient.Send抛出产生的异常细节的详细信息 - >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; 
     } 

谷歌需要启用SSL和端口587是输出端口。您还需要一个Google帐户以及凭据。

没有错,你的代码 - 这是最有可能您的服务器或防火墙

+0

消息=发送邮件失败 来源=系统 堆栈跟踪:? 在System.Net.Mail.SmtpClient.Send(消息MAILMESSAGE) 在WebApplication1._Default.Page_Load(对象发件人, EventArgs e)D:\ Emcure-Kumar \ Work \ Not in Use \ WebApplication1 \ WebApplication1 \ Default.aspx.cs:line 30 InnerException:System.Net.WebException Message =无法连接到远程服务器 Source =系统 堆栈跟踪: – kumar 2011-03-25 09:28:05

+0

在System.Net.ServicePoint.GetConnection(PooledStream PooledStream,对象所有者,布尔异步,ip地址和地址,插座&abortSocket,插座&abortSocket6,的Int32超时) 在System.Net.PooledStream.Activate(对象owningObject,布尔异步, INT32超时,GeneralAsyncDelegate的AsyncCallback) 在System.Net.PooledStream.Activate(对象owningObject,GeneralAsyncDelegate的AsyncCallback) 在System.Net.ConnectionPool.GetConnection(在System.Net.Mail.SmtpConnection对象owningObject,GeneralAsyncDelegate的AsyncCallback,INT32 creationTimeout) – kumar 2011-03-25 09:28:53

+0

.GetConnection(String host,Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host,Int32 port) at System.Net.Mail.SmtpClient.GetConnection() 在System.Net.Mail.SmtpClient.Send(消息MAILMESSAGE) 的InnerException:System.Net.Sockets.SocketException 消息=无连接可以作出,因为目标机器积极地拒绝它127.0.0.1:25 源=系统 ErrorCode = 10061 NativeErrorCode = 10061 – kumar 2011-03-25 09:29:42

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("[email protected]"); 

     message.From = fromAddress; 

     message.To.Add("[email protected]"); 


     message.Body = txt_des.Text; 
     smtpClient.EnableSsl = true; 

     System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential(); 

     NetworkCred.UserName = "[email protected]"; 

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

我有同样的问题,而且是我代理,我不知道是不是真的,因为我即有一个具有良好配置的代理,并且我读取了一个C#应用程序获取OS代理的配置。我只是改变我的conecction一些没有代理和运行得很好。所有的解决方案,第一,第二和第三。也许是代理或防火墙,或者尝试在其他PC上进行确认。我希望能帮助你。