Saif Obeidat
Saif Obeidat

Reputation: 148

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:

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

Upvotes: 0

Views: 1593

Answers (2)

Pete -S-
Pete -S-

Reputation: 562

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> 

Upvotes: 1

Saif Obeidat
Saif Obeidat

Reputation: 148

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.

Upvotes: 0

Related Questions