Zac*_*yne 10 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";
}
Run Code Online (Sandbox Code Playgroud)
这是在我的主类中使用这些变量:
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);
}
Run Code Online (Sandbox Code Playgroud)
就像旁注一样.该程序在使用我的凭据时,在通过我自己的服务器时工作,只是在链接到他们的服务器时不起作用.
为了解决这个问题,我不得不使用可选参数Domain为NetworkCredential
我的代码现在看起来像这样:
NetworkCredential basicCredential = new NetworkCredential(MailConst.UserName, MailConst.Password, MailConst.Domain
其中MailConst.Domain是指向Exchange域的字符串.