这是什么错误? (在asp.net发送电子邮件)

时间:2012-05-17 04:57:51

标签: asp.net email dns smtpclient

我使用此代码在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("info@domain.com", "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();
    }
}

它正确地向我的域中定义的收件人发送电子邮件(例如 mail@domain.com ),但我无法向其他邮件服务器发送电子邮件(例如 mail@yahoo.com )。

我的代码有什么问题?

(可能与SmtpClient属性有关吗?我已将smtpclient.host设置为 mail.mydomain.com 并使用我的域中定义的一个邮件帐户的用户名和密码

由于

1 个答案:

答案 0 :(得分:5)

它必须与您的Exchange服务器相关。交换规则决定了你如何与外界沟通。

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/

上分享此问题