我已经从GoDaddy购买了“ Office 365电子邮件必备”计划,用于发送电子邮件。我能够在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);
}
这是配置文件中的邮件设置条目-
<mailSettings>
<smtp from="xxx@mydomain.com">
<network host="smtp.office365.com" port="587" />
</smtp>
</mailSettings>
我在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更改了“邮箱委派”中的设置,但这也无济于事。
请帮助我这些步骤中我缺少的东西。
答案 0 :(得分: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
};
经过这些更新后,它将起作用!!!享受!
控制台应用程序代码示例!
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();
}
}
答案 1 :(得分:-1)
尝试将SMTP服务器名称更改为Outlook.office365.com,而不是smtp.office365.com