Dfr*_*Dkn 2 c# email smtpclient office365
我已从 GoDaddy 购买了“Office 365 Email Essentials”计划来发送电子邮件。我可以在我的 Outlook 邮箱中发送和接收这些电子邮件,但不能通过 C# 代码以编程方式发送。我一直收到错误“
SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.57 SMTP;在 MAIL FROM [BMXPR01CA0092.INDPRD01.PROD.OUTLOOK.COM] 期间,客户端未通过身份验证发送匿名邮件”
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("xxx@gmail.com", "xxx"));
msg.From = new MailAddress("xxx@mydomain.com", "Admin");
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 NetworkCredential("xxx@mydomain.com", "mypassword","mydomain.com");
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
//Send the msg
try
{
client.Send(msg);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
这是配置文件中的 mailsettings 条目 -
<mailSettings>
<smtp from="xxx@mydomain.com">
<network host="smtp.office365.com" port="587" />
</smtp>
</mailSettings>
Run Code Online (Sandbox Code Playgroud)
我在 Outlook 中看到的设置是 -
服务器名称:smtp.office365.com 端口:587 加密方式:STARTTLS
或者,我也尝试使用用户 ID 代替网络凭据中的实际电子邮件地址,但这也不起作用。
我还尝试在 GoDaddy 中使用 MX Txt 记录,即服务器“relay-hosting.secureserver.net”和端口 #25,但没有成功。
我还根据链接http://edudotnet.blogspot.com/2014/02/smtp-microsoft-office-365-net-smtp.html更改了“邮箱委托”中的设置,但这也无济于事。
请帮助我在这些步骤中缺少什么。
小智 6
在花了几个小时之后,我发现godaddy配置的office 365邮件ID的smtp主机是“ smtpout.secureserver.net ”。
还将客户端编程为关闭连接,如下所示:
SmtpClient client = new SmtpClient {
Host = "smtpout.secureserver.net",
Credentials = new NetworkCredential("USERNAME@DOMAIN.COM", "YOURPWD"),
Port = 587,
EnableSsl = true,
Timeout = 10000
};
Run Code Online (Sandbox Code Playgroud)
这些更新后,它会工作!!!享受!
示例控制台应用程序代码!
static void Main(string[] args)
{
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string userId = "username@domainname.com";
string _pwd = "mypassword";
var toAddress = "touseremailaddress";
SmtpClient client = new SmtpClient
{
Host = "smtpout.secureserver.net",
Credentials = new NetworkCredential(userId, _pwd),
Port = 587,
EnableSsl = true
};
var Msg = new MailMessage();
Msg.From = new MailAddress(userId);
Msg.To.Add(new MailAddress(toAddress.ToString()));
Msg.Subject = "TEST EMAIL";
Msg.Body = "Hello world";
Console.WriteLine("start to send email over SSL...");
client.Send(Msg);
Console.WriteLine("email was sent successfully!");
Console.ReadLine();
}
catch (Exception ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)