使用 Hotmail SMTP C# 发送电子邮件在本地有效,但在实时服务器上无效
Sending email using Hotmail SMTP C# works locally, but doesn't on live server
我有一个 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: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP
我尝试了很多事情,例如:
- 尝试了这些主机 smtp.live.com、smtp.office365.com、smtp-mail.live.com 和其他主机
- 尝试了其他端口,例如 25
- 试图将此 UseDefaultCredentials 设置为 false 和 true
- 确保用户名和密码正确
- 从SMPT Config代码的顺序确定
我为此花了好几个小时,但我没有找到任何解决方案!我需要任何见解。
好的,问题是 Hotmail 检测到我的帐户有异常行为,这是因为我试图在本地和 Live 主机上发送电子邮件,所以他们阻止了我并向我发送了一封电子邮件,如果那个人试图发送电子邮件是不是我,一旦我确认是我,一切都很好。
您可以选择使用 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>
我有一个 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: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP
我尝试了很多事情,例如:
- 尝试了这些主机 smtp.live.com、smtp.office365.com、smtp-mail.live.com 和其他主机
- 尝试了其他端口,例如 25
- 试图将此 UseDefaultCredentials 设置为 false 和 true
- 确保用户名和密码正确
- 从SMPT Config代码的顺序确定
我为此花了好几个小时,但我没有找到任何解决方案!我需要任何见解。
好的,问题是 Hotmail 检测到我的帐户有异常行为,这是因为我试图在本地和 Live 主机上发送电子邮件,所以他们阻止了我并向我发送了一封电子邮件,如果那个人试图发送电子邮件是不是我,一旦我确认是我,一切都很好。
您可以选择使用 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>