Search code examples
c#asp.netemailsmtphotmail

Sending email using Hotmail SMTP C# works locally, but doesn't on live server


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("[email protected]", "mysenderemailpassword");
        MailMessage ProviderMail = new MailMessage();

        ProviderMail.From = new MailAddress("[email protected]");
        ProviderMail.To.Add("[email protected]");


        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.


Solution

  • 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 = "[email protected]"  ''IMPORTANT: This must be same as your smtp authentication address.
            Dim strTo = "[email protected]" 
            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("[email protected]", "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>