我使用這個代碼在asp.net發送電子郵件:是什麼錯誤? (在asp.net發送電子郵件)
使用System.Net.Mail
public string SendEmail()
{
SmtpClient obj = new SmtpClient();
MailMessage Mailmsg = new MailMessage();
Mailmsg.To.Clear();
Recievers = new MailAddressCollection();
Recievers.Add(txtToAddress.Text);
SenderName = "Info";
SenderEmail = txtFromAddress.Text;
Subject = "subj";
Body = "body";
UseBcc = false;
if (UseBcc)
{
foreach (MailAddress RecieverItem in Recievers)
{
Mailmsg.Bcc.Add(RecieverItem);
}
}
else
{
foreach (MailAddress RecieverItem in Recievers)
{
Mailmsg.To.Add(RecieverItem);
}
}
Mailmsg.From = new MailAddress(SenderEmail, SenderName, System.Text.Encoding.UTF8);
Mailmsg.Subject = Subject;
Mailmsg.SubjectEncoding = Encoding.UTF8;
Mailmsg.BodyEncoding = System.Text.Encoding.UTF8;
Mailmsg.IsBodyHtml = false;
obj.Host = mail.domain.com;
System.Net.NetworkCredential BasicAuthenticationInfo = new System.Net.NetworkCredential("[email protected]", "password");
obj.UseDefaultCredentials = false;
obj.Credentials = BasicAuthenticationInfo;
Mailmsg.Body = Body;
Mailmsg.IsBodyHtml = true;
try
{
obj.Send(Mailmsg);
return "sent";
}
catch (Exception ex)
{
return ex.ToString();
}
}
它正確地發送電子郵件給其定義在recievers我的域名(如[email protected]),但我無法將電子郵件發送到其他郵件服務器(如[email protected])。
我的代碼有什麼問題?
(它可能涉及到SmtpClient屬性呢?我已經smtpclient.host設置爲mail.mydomain.com 並使用它們在我的域中定義我的郵件帳戶之一的用戶名和密碼)
謝謝
這個問題是否引發任何異常?您是否檢查過SMTP服務器管理員(他們是否阻止了SMTP服務器上的程序以防止將它發送到外部?) –
是的例外是:System.Net.Mail.SmtpFailedRecipientException:郵箱不可用。服務器響應是:System.Net.Mail.SmtpClient.Send(MailMessage消息)位於System.Net.Mail.SmtpTransport.SendMail(MailAddress發件人,MailAddressCollection收件人,String deliveryNotify,SmtpFailedRecipientException和異常)位於_Default.SendEmail () –
您可以檢查異常SmtpFailedRecipientException中的屬性「FailedRecipient」。你確定這是一個正確的收件人地址? –