0

I have an asp.net web API that is used to send emails to another email (gmail), and another frontend project calls that API, and it works perfect it sends emails as expected when the backend & frontend projects are locally, but it doesn't work on live hosting (SmarterAsp).

Here's my conflagration and code:

    //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();
        }
    }

And here the error that I got from the 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

I tried to do many things such as:

  • Tried these hosts smtp.live.com, smtp.office365.com, smtp-mail.live.com and others
  • Tried other ports such as 25
  • Tried to make this UseDefaultCredentials as false and true
  • Made sure that the User name and Pass are correct
  • Made sure from the order of SMPT Config code

I spent hours on this, but I not fining any solution! I need any insight.

Saif Obeidat
  • 128
  • 1
  • 2
  • 16
  • I've seen this error when using gmail, is there an "app password" for sending emails in smtp.office365.com where the user password will not work? – Pete -S- Jul 21 '20 at 21:10
  • 1
    Do you have the option to use SmarterAsp SMTP server instead? – Pete -S- Jul 21 '20 at 21:21
  • Possibly related to - https://stackoverflow.com/questions/30342884/5-7-57-smtp-client-was-not-authenticated-to-send-anonymous-mail-during-mail-fr – sh1rts Jul 21 '20 at 21:45
  • It seems that you are using Gmail service, is that right? If yes, you need to make sure that your MX record has pointed to Gmail service. – Mark Spencer Jul 22 '20 at 05:55
  • Thanks, @pete-S- I tried your idea by using SmarterAsp Emails, and it worked, add your answer as a separate post, and I will make it the correct one for my case – Saif Obeidat Jul 22 '20 at 15:03

2 Answers2

1

Do you have the option to use SmarterAsp SMTP server instead?

From their site:

<%@ 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> 
Pete -S-
  • 542
  • 5
  • 13
0

Okay, the issue was Hotmail detected unusual behavior about my account, and that happened because I tried to send emails Locally and on Live hosting, so they blocked me and sent me an email if that person who was trying to send emails is me or not, once I confirmed it was me, everything went okay.

Saif Obeidat
  • 128
  • 1
  • 2
  • 16