2012-05-17 109 views
0

我使用這個代碼在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 並使用它們在我的域中定義我的郵件帳戶之一的用戶名和密碼)

謝謝

+0

這個問題是否引發任何異常?您是否檢查過SMTP服務器管理員(他們是否阻止了SMTP服務器上的程序以防止將它發送到外部?) –

+0

是的例外是:System.Net.Mail.SmtpFailedRecipientException:郵箱不可用。服務器響應是:System.Net.Mail.SmtpClient.Send(MailMessage消息)位於System.Net.Mail.SmtpTransport.SendMail(MailAddress發件人,MailAddressCollection收件人,String deliveryNotify,SmtpFailedRecipientException和異常)位於_Default.SendEmail () –

+0

您可以檢查異常SmtpFailedRecipientException中的屬性「FailedRecipient」。你確定這是一個正確的收件人地址? –

回答

5

它必須是與您的交換服務器相關的東西。有交通規則交換,定義你如何與外界溝通。

http://www.msexchange.org/articles_tutorials/exchange-server-2007/management-administration/restricting-users-send-receive-external-messages-exchange-server-2007.html

則必須在發送郵件到外部網絡

System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.1.1 User unknown 
    at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) 

如果這是錯誤您收到這意味着您的交換不支持直接發送電子郵件到外部網絡可以得到一些例外。因爲我不是MS交換專家,但我一直在使用在我的網絡中配置的交換服務器,無法將電子郵件發送到外部網絡,但我們啓用了將電子郵件轉發給聯繫人。

可能這會幫助你。 http://www.petri.co.il/configuring-exchange-2007-send-connectors.htm

還我建議你分享https://serverfault.com/

+0

是的,我只是想說,發送到其他域的電子郵件可能會阻止您的交換服務器.. –

+0

我該如何解決它? –

+0

檢查我的答案我已添加一些更多詳細信息 –