通过Microsoft Exchange Server发送电子邮件

时间:2013-11-01 09:12:44

标签: c# email exchange-server

好的,所以我有这个程序本质上充当公司的电子邮件客户端,它为他们构建电子邮件并将其发送出去。

我已经完成了所有工作,但在发送电子邮件时,他们会获得Mailbox Unavailable. Accessed Denied - Invalid HELO Name

有趣的是,我使用相同的用户名作为网络凭据和from部分。

编辑:更新我正在使用的代码...现在出现Failure sending mail错误。

这是我的MailConst.cs课程:

public class MailConst
{

    /*
     */

    public static string Username = "username";
    public static string Password = "password";
    public const string SmtpServer = "smtp.domain.co.uk";

    public static string From = Username + "@domain.co.uk";

}

这是在我的主类中使用这些变量:

    public static void SendMail(string recipient, string subject, string body, string[] attachments)
    {


        SmtpClient smtpClient = new SmtpClient();
        NetworkCredential basicCredential = new NetworkCredential(MailConst.Username, MailConst.Password, MailConst.SmtpServer);
        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress(MailConst.From);

        // setup up the host, increase the timeout to 5 minutes
        smtpClient.Host = MailConst.SmtpServer;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = basicCredential;
        smtpClient.Timeout = (60 * 5 * 1000);

        message.From = fromAddress;
        message.Subject = subject + " - " + DateTime.Now.Date.ToString().Split(' ')[0];
        message.IsBodyHtml = true;
        message.Body = body.Replace("\r\n", "<br>");
        message.To.Add(recipient);

        if (attachments != null)
        {
            foreach (string attachment in attachments)
            {
                message.Attachments.Add(new Attachment(attachment));
            }
        }

        smtpClient.Send(message);
    }

正如旁注。该程序在使用我的凭据时,在通过我自己的服务器时工作,只是在链接到他们的服务器时不起作用。

1 个答案:

答案 0 :(得分:7)

为了解决这个问题,我必须为Domain

使用可选参数NetworkCredential

我的代码现在看起来像这样:

NetworkCredential basicCredential = new NetworkCredential(MailConst.UserName, MailConst.Password, MailConst.Domain

MailConst.Domain是指向Exchange域的字符串。