如何通过ExchangeService C#发送电子邮件

时间:2018-11-22 09:35:56

标签: c# exchange-server

我必须通过Exchange发送电子邮件。我发现可以使用ExchangeService来做到这一点。我尝试了许多方法来执行此操作,但只有错误:(440)登录超时。

我什至不知道凭据是否有问题,或者我的代码是错误的。

string login = @"login";
string password = @"password";
ExchangeService service = new ExangeService();
service.Credentials = new NetworkCredential(login, password);
service.Url = new Uri(@"https://mail.exchangemail.com");

EmailMessage message = new EmailMessage(service);
message.Subject = "Interesting";
message.Body = "The proposition has been considered.";
message.ToRecipients.Add("quarkpol@gmail.com");
message.From = "quarkpol_test@gmail.com";
message.Send();

1 个答案:

答案 0 :(得分:0)

我已经在网上找到了问题解决方案,并且可以正常工作。

using EASendMail;

string login = @"login";
string password = @"password";

SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();

oMail.From = "from@email.com";
oMail.To = "to@email.com";
oMail.AddAttachment(fileName, fileByteArray);

oMail.Subject = "Subject";
oMail.HtmlBody = "HTMLBody";

SmtpServer oServer = new SmtpServer("mail.email.com");

oServer.Protocol = ServerProtocol.ExchangeEWS;
oServer.User = login;
oServer.Password = password;

oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

try
{
    Console.WriteLine("start to send email ...");
    oSmtp.SendMail(oServer, oMail);
    Console.WriteLine("email was sent successfully!");
}
catch (Exception ep)
{
    Console.WriteLine("failed to send email with the following error:");
    Console.WriteLine(ep.Message);
}