我有一个asp.net web API,用于将电子邮件发送到另一封电子邮件(gmail(,另一个前端项目调用该API,它工作得很好,当后端&前端项目是本地的,但它不适用于实时托管(SmarterAsp(。
这是我的火灾和代码:
//This method only sends email as per the data sent
[System.Web.Http.HttpPost]
public EmailResponseModel SendEmail()
{
NetworkCredential basicCredential =
new NetworkCredential("mysenderemail@hotmail.com", "mysenderemailpassword");
MailMessage ProviderMail = new MailMessage();
ProviderMail.From = new MailAddress("mysenderemail@hotmail.com");
ProviderMail.To.Add("myemail@gmail.com");
ProviderMail.Subject = "Title";
ProviderMail.IsBodyHtml = true;
ProviderMail.Body = "Body";
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Host = "smtp.office365.com";
smtp.Credentials = basicCredential;
smtp.EnableSsl = true;
try
{
smtp.Send(ProviderMail);
return Response;
}
catch (Exception ex)
{
return Response;
}
finally
{
smtp.Dispose();
}
}
这里是我从API得到的错误:
System.Net.Mail.SmtpException:SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.7.57 SMTP
我试过做很多事情,比如:
- 尝试了这些主机smtp.live.com、smtp.office365、smtp-mail.live.com和其他
- 已尝试其他端口,如25
- 尝试将此UseDefaultCredentials设为false和true
- 确保用户名和通行证正确无误
- 根据SMPT配置代码的顺序确定
我花了几个小时,但我没有细化任何解决方案!我需要任何见解。
您可以选择使用SmarterAsp SMTP服务器吗?
从他们的网站:
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net.Mail" %>
<script runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim strFrom = "postmaster@yourdomain.com" ''IMPORTANT: This must be same as your smtp authentication address.
Dim strTo = "postmaster@yourdomain.com"
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))
MailMsg.BodyEncoding = Encoding.Default
MailMsg.Subject = "This is a test"
MailMsg.Body = "This is a sample message using SMTP authentication"
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True
'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
Dim basicAuthenticationInfo As New System.Net.NetworkCredential("postmaster@mydoamin.com", "password")
''IMPORANT: Your smtp login email MUST be same as your FROM address.
SmtpMail.Host = "mail.yourdomain.com"
SmtpMail.UseDefaultCredentials = False
SmtpMail.Credentials = basicAuthenticationInfo
SmtpMail.Port = 25; //alternative port number is 8889
SmtpMail.EnableSsl = false;
SmtpMail.Send(MailMsg)
lblMessage.Text = "Mail Sent"
End Sub
</script>
<html>
<body>
<form runat="server">
<asp:Label id="lblMessage" runat="server"></asp:Label>
</form>
</body>
</html>
好的,问题是Hotmail检测到我的帐户有异常行为,这是因为我试图在本地和Live主机上发送电子邮件,所以他们阻止了我,并向我发送了一封电子邮件,如果试图发送电子邮件的人是不是我,一旦我确认是我,一切都很顺利。