0

我正在使用此代码在 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 并使用在我的域中定义的我的一个邮件帐户的用户名和密码)

谢谢

4

1 回答 1

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/上分享这个问题

于 2012-05-17T05:02:55.367 回答