gpt4 book ai didi

c# - 通过 Exchange Online (Office 365) 使用 System.Net.Mail 发送 SMTP 电子邮件

转载 作者:IT王子 更新时间:2023-10-29 03:54:40 25 4
gpt4 key购买 nike

我们正在测试新的 Office 365 测试版,我在 Exchange Online 服务上有一个邮件帐户。现在我正在尝试连接一个可以从我的测试帐户发送 smtp 电子邮件的 LOB 应用程序。

但是 Exchange 365 平台需要在端口 587 上进行 TLS 加密,System.Net.Mail 的“功能”不允许隐式 SSL 加密。

有没有人设法让C#通过这个平台发送邮件?

我有以下应该发送邮件的基本代码 - 任何建议将不胜感激。

SmtpClient server = new SmtpClient("ServerAddress");
server.Port = 587;
server.EnableSsl = true;
server.Credentials = new System.Net.NetworkCredential("username@mydomain.com", "password");
server.Timeout = 5000;
server.UseDefaultCredentials = false;

MailMessage mail = new MailMessage();
mail.From = new MailAddress("recipent@anyaddress");
mail.To.Add("username@mydomain.com");
mail.Subject = "test out message sending";
mail.Body = "this is my message body";
mail.IsBodyHtml = true;

server.Send(mail);

最佳答案

修正了上面工作代码中的一些拼写错误:

MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("someone@somedomain.com", "SomeOne"));
msg.From = new MailAddress("you@yourdomain.com", "You");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message using Exchange OnLine";
msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("your user name", "your password");
client.Port = 587; // You can use Port 25 if 587 is blocked (mine is!)
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
try
{
client.Send(msg);
lblText.Text = "Message Sent Succesfully";
}
catch (Exception ex)
{
lblText.Text = ex.ToString();
}

我有两个使用上述代码的 Web 应用程序,都可以正常工作,没有任何问题。

关于c# - 通过 Exchange Online (Office 365) 使用 System.Net.Mail 发送 SMTP 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6244694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com