2013-05-10 100 views
1

發送電子郵件我想從一個控制檯應用程序發送郵件,但它拋出一個錯誤:錯誤而從控制檯應用程序

Failure Sending Mail

我不明白是什麼導致了錯誤。我在另一臺機器上嘗試了相同的代碼,它工作正常。這裏是堆棧跟蹤:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebExceptio 
n: Unable to connect to the remote server ---> System.Net.Sockets.SocketExceptio 
n: The requested address is not valid in its context 66.96.147.108:25 
    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre 
ss socketAddress) 
    at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock 
et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, 
IAsyncResult asyncResult, Int32 timeout, Exception& exception) 
    --- End of inner exception stack trace --- 
    at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object ow 
ner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket 
6, 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, GeneralAsyncD 
elegate asyncCallback, Int32 creationTimeout) 
    at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) 
    at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) 
    at System.Net.Mail.SmtpClient.GetConnection() 
    at System.Net.Mail.SmtpClient.Send(MailMessage message) 
    --- End of inner exception stack trace --- 
    at System.Net.Mail.SmtpClient.Send(MailMessage message) 
    at PaymentReminder.Program.SendMail(List`1 SourceList) in C:\Documents and Se 
ttings\amols\my documents\visual studio 2010\Projects\PaymentReminder\PaymentRem 
inder\Program.cs:line 110 
InnerException is: System.Net.WebException: Unable to connect to the remote serv 
er ---> System.Net.Sockets.SocketException: The requested address is not valid i 
n its context 66.96.147.108:25 
    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre 
ss socketAddress) 
    at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock 
et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, 
IAsyncResult asyncResult, Int32 timeout, Exception& exception) 
    --- End of inner exception stack trace --- 
    at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object ow 
ner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket 
6, 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, GeneralAsyncD 
elegate asyncCallback, Int32 creationTimeout) 
    at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) 
    at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) 
    at System.Net.Mail.SmtpClient.GetConnection() 
    at System.Net.Mail.SmtpClient.Send(MailMessage message) 

這裏是我的代碼:

List<PaymentReminderList> prList = SourceList;    
MailAddress fromMail = new MailAddress(ConfigurationManager.AppSettings.Get("FromEmail")); 
string NetworkUser = ConfigurationManager.AppSettings.Get("Network_UserName"); 
string Password = ConfigurationManager.AppSettings.Get("Password"); 
string Host = ConfigurationManager.AppSettings.Get("Host"); 
int Port = int.Parse(ConfigurationManager.AppSettings.Get("Port")); 
string FromMail = ConfigurationManager.AppSettings.Get("FromEmail"); 
string ToMail = ConfigurationManager.AppSettings.Get("ToMail"); 
string mailBody = "Respected xxx, <br>This is Test Mail.<br> [[List]] <br><br> Sincerely, <br> ABC<br>"; 

string mailContent = ""; 

foreach (var item in prList) 
{ 
    mailContent += "" + item.abc + " | " + item.pqr + " | " + item.xyz+"<br>"; 
} 

mailBody.Replace("[[List]]", mailContent); 

//Console.WriteLine("Mail To"); 
MailAddress to = new MailAddress(ToMail); 

//Console.WriteLine("Mail From"); 
MailAddress from = new MailAddress(FromMail); 

MailMessage mail = new MailMessage(from, to); 

Console.WriteLine("Subject"); 
mail.Subject = "This is a Reminder Mail"; 

Console.WriteLine("Your Message"); 
//mail.Body = Console.ReadLine(); 
mail.Body = mailBody; 

mail.From = from; 
mail.To.Add(to); 

mail.IsBodyHtml = true; 
mail.Priority = MailPriority.High; 

System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential(); 
NetworkCred.UserName = NetworkUser; 
NetworkCred.Password = Password; 

SmtpClient smtp = new SmtpClient(Host, Port); 
smtp.Credentials = NetworkCred; 

Console.WriteLine("Sending email..."); 

try 
{ 
    smtp.Send(mail); 
} 
catch (Exception ex) 
{ 
    Console.WriteLine(ex); //Should print stacktrace + details of inner exception 

    if (ex.InnerException != null) 
     Console.WriteLine("InnerException is: {0}", ex.InnerException); 
} 
+0

你的代碼是什麼樣的? – 2013-05-10 11:16:13

+0

@SonerGönül我已添加我的代碼 – 2013-05-10 11:21:25

+0

錯誤很明顯無法建立與地址的連接。你確定這些信息是正確的? – 2013-05-10 14:34:03

回答

0

是否在遠程機器設備的安全SMTP?如果是這樣,你需要的端口屬性設置爲465

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.port.aspx

+0

現在顯示沒有錯誤顯示;但仍然不發送郵件 – 2013-05-10 11:37:32

+0

相同的代碼正在另一臺機器上工作 – 2013-05-10 11:38:00

+0

嗯...我會再選擇我的大腦。除此之外,它可能是一個防火牆問題。 – 2013-05-10 12:17:31

0

我用我自己的郵件服務器測試你的代碼,它工作得很好那裏。

問題可能出在您的配置中。您在代碼中使用身份驗證,並且通常需要爲連接啓用SSL(請參閱SmtpClient.EnableSsl),並將端口設置爲465.端口25通常不需要身份驗證。

+0

我正在使用端口25,它在其他機器和web應用程序上工作正常 – 2013-05-10 11:42:58

+0

也許您可以嘗試使用telnet手動連接到您遇到問題的計算機上的SMTP服務器,以便更好地查看連接階段發生的情況。基於異常消息,它看起來像一個網絡問題。 – zsgalusz 2013-05-10 11:54:33

相關問題