2013-04-20 448 views
-1

我正在開發一個C#應用程序來使用我們公司郵件的SMTP服務器發送郵件。以下是代碼。SMTP郵件錯誤

MailMessage mail = new MailMessage(); 
SmtpClient SmtpServer = new SmtpClient("10.203.195.48"); 

mail.From = new MailAddress(""); 
mail.To.Add(""); 
mail.Subject = "filename"; 
mail.Body = "Report"; 

SmtpServer.Host = "ip address fo smtp mail server."; 
SmtpServer.Port = 25; 
SmtpServer.Credentials = new System.Net.NetworkCredential("", ""); 

SmtpServer.Send(mail); 

但我得到這個錯誤:

mailbox unavailable.unable to relay.The system doesn't have internet connection.

+1

你確實傳遞了'mail.To.Add'和'System.Net.NetworkCredential'的空白值嗎?錯誤信息(s?)很明顯。 「無法中繼」 - 默認情況下,大多數公司郵件服務器會關閉中繼,以便它們不能用於發送垃圾郵件。 – Tim 2013-04-20 07:30:02

+0

看看這個線程 - ['錯誤:郵箱不可用。服務器響應是5.7.1無法中繼(電子郵件)'](http://forums.devshed.com/net-development-87/error-mailbox-unavailable-the-server-response-was-5-7 -1T-315971.html) – Tim 2013-04-20 07:32:42

回答

1

下面的代碼是使用端口587你只需要改變你的端口,SMTP GmailSMTP

添加命名空間

using system.net 
MailMessage MyMailMessage = new MailMessage(); 
MyMailMessage.From = new MailAddress("emailid"); 
MyMailMessage.To.Add("To"); 
MyMailMessage.Subject = "Feedback Form"; 
MyMailMessage.Body = "This is the test message"; 
MyMailMessage.IsBodyHtml = true; 

SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com"); 
SMTPServer.Port = 587; 
SMTPServer.Credentials = new System.Net.NetworkCredential("Username","password"); 
SMTPServer.EnableSsl = true; 

try 
{ 
    SMTPServer.Send(MyMailMessage); 
} 

catch (Exception ex) 
{ 
    ex.message("error"); 
} 
1

Error: Mailbox unavailable. The server response was 5.7.1 Unable to relay for (email)

Generally it occurs when you have a mail server (e.g. mailserver.com) from one domain, and the addresses are from other domains. Either the From or the To address need to belong to the domain ([email protected] or [email protected]). If neither of the addresses belong to a domain that the mail server 'owns', then you are relaying, which is a spam technique and is generally not allowed nowadays."

參見博客文章Mailbox unavailable. The server response was: 5.7.1 Unable to relay sendername