當我按在調試模式下發送鍵,從「txtMail」一個特定的地址我得到這個「彈出」錯誤(接收器):
發送電子郵件錯誤:郵箱不可用。客戶端主機拒絕
MailerException was unhandled by user code
Mailbox unavailable.
The server response was: 4.7.1 Client host rejected: cannot find your hostname, [IP]
的代碼實際上絆倒了最後一行,也就是:
catch(Exception ex)
{
throw new MailerException(ex.Message, ex);
}
我不認爲有一個與代碼本身有問題,但這裏是整個代碼,這樣也許有人能夠發現問題:
public void SendMail()
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
// Mail from
if (!string.IsNullOrEmpty(this.mailFrom))
message.From = new MailAddress(this.mailFrom);
else
message.From = from;
if (!IsValidEmailAddress(message.From.Address))
throw new MailAddressException("From address " + message.From + " is not valid");
//message.From = from;
// Add recipients
if (_RecipientNames.Count == 0)
throw new RecipientException("No recipients added");
StringBuilder Recipients = new StringBuilder();
for(int i = 0; i < _RecipientEmailAddresses.Count; i++)
{
if (_RecipientNames[i].ToString().Length > 0)
message.To.Add(new MailAddress((string)_RecipientEmailAddresses[i], (string)_RecipientNames[i]));
else
message.To.Add(new MailAddress((string)_RecipientEmailAddresses[i]));
}
foreach (MailAddress ma in this._Bcc)
message.Bcc.Add(ma);
if (this._ReplyToAddress != null)
message.ReplyTo = this._ReplyToAddress;
message.Subject = _Subject;
message.IsBodyHtml = this.isHtmlMail;
message.BodyEncoding = this.BodyEncoding;
// Priority
message.Priority = this.priorityLevel;
// Load template file?
if (_TemplateFile.Length > 0)
{
message.Body = GetMailTemplateContents();
}
else
message.Body = _MailBody;
// Attachments given?
if (this.attachments.Count > 0)
{
foreach (Attachment a in this.attachments)
message.Attachments.Add(a);
}
SmtpClient client = new SmtpClient(_SmtpServer);
try
{
client.Send(message);
}
catch(System.Web.HttpException ex)
{
throw new MailerException(ex.Message,ex);
}
catch(System.Runtime.InteropServices.COMException ex)
{
// Rethrow the exception
throw new MailerException(ex.Message, ex);
}
catch(Exception ex)
{
throw new MailerException(ex.Message, ex);
}
有人可能會說這實際上是我嘗試傳遞到的服務器返回的錯誤代碼。 (郵箱已滿,無效的電子郵件地址等)
無論哪種方式,還是需要由郵件服務器的管理員解決? 有沒有人可能知道爲什麼會出現這個錯誤?
願意根據需要提供任何其他/更多信息。
在此先感謝,
'SmtpClient'應該在'using'塊中。 –