C# 通过 Microsoft Exchange Server 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19723801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:44:40  来源:igfitidea点击:

Sending Email through Microsoft Exchange Server

c#emailexchange-server

提问by Zach Ross-Clyne

Okay, so I have this program which in essence acts as an email client for a company, it constructs the email for them and sends it out.

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

I've done everything on it, but when going to send the email, they get a Mailbox Unavailable. Accessed Denied - Invalid HELO Name

我已经做了所有的事情,但是当要发送电子邮件时,他们收到了 Mailbox Unavailable. Accessed Denied - Invalid HELO Name

Interestingly though, I use the same username for the network credentials and the from part.

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

EDIT:Update code to what I'm now using... Now getting Failure sending mailerror.

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

Here is my MailConst.csclass:

这是我的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";

}

and here is the use of these variables in my main class:

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

    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);
    }

Just as a side note. The program works when using my credentials, when going through my own server, just doesn't work when linking it to theirs.

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

采纳答案by Zach Ross-Clyne

In order to fix this problem I had to use the optional parameter Domainfor the NetworkCredential

为了解决这个问题,我不得不使用可选参数DomainNetworkCredential

My code now looks like this:

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

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

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

Where the MailConst.Domainis a string pointing to the Exchange domain.

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