Unable to send emails using C# code from GoDaddy configured Office 365 account

Viewed 1237

I have purchased "Office 365 Email Essentials" plan from GoDaddy for sending the emails. I am able to send and receive these emails in my outlook mailbox, but not programatically via C# code. I keep receiving the error "

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [BMXPR01CA0092.INDPRD01.PROD.OUTLOOK.COM]"

MailMessage msg = new MailMessage();
        msg.To.Add(new MailAddress("xxx@gmail.com", "xxx"));
        msg.From = new MailAddress("xxx@mydomain.com", "Admin");
        msg.Subject = "This is a Test Mail";
        msg.Body = "This is a test message using Exchange OnLine";
        msg.IsBodyHtml = true;

        SmtpClient client = new SmtpClient();
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("xxx@mydomain.com", "mypassword","mydomain.com");
        client.Port = 587;
        client.Host = "smtp.office365.com";
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.EnableSsl = true;
        //Send the msg
        try
        {
            client.Send(msg);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

And here is the mailsettings entry in the config file -

<mailSettings>
<smtp from="xxx@mydomain.com">
  <network host="smtp.office365.com" port="587" />
</smtp>
</mailSettings>

The settings I see in the Outlook are -

Server name: smtp.office365.com Port: 587 Encryption method: STARTTLS

Alternatively, I also tried using the userid in place of actual email address in the network credentials,but that did not work either.

I also tried to use the MX Txt record in GoDaddy i.e. server "relay-hosting.secureserver.net" and port #25 ,but no luck.

I also changed the settings in "Mailbox delegation" as per the link http://edudotnet.blogspot.com/2014/02/smtp-microsoft-office-365-net-smtp.html , but that did not help either.

Please help me what am I missing in these steps.

2 Answers

After spending hours on this, I have found that the smtp host for the godaddy configured office 365 mail id is "smtpout.secureserver.net".

Also program the client as closed connection like below:

SmtpClient client = new SmtpClient {

   Host = "smtpout.secureserver.net",

   Credentials = new NetworkCredential("USERNAME@DOMAIN.COM", "YOURPWD"),

   Port = 587,

   EnableSsl = true,

   Timeout = 10000

};

after these updates, it will work!!! Enjoy!

Example Console application code!

static void Main(string[] args)
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                string userId = "username@domainname.com";
                string _pwd = "mypassword";
                var toAddress = "touseremailaddress";
                SmtpClient client = new SmtpClient
                {
                    Host = "smtpout.secureserver.net",
                    Credentials = new NetworkCredential(userId, _pwd),
                    Port = 587,
                    EnableSsl = true
                };
               
                var Msg = new MailMessage();
                Msg.From = new MailAddress(userId);
                Msg.To.Add(new MailAddress(toAddress.ToString()));
                Msg.Subject = "TEST EMAIL";
                Msg.Body = "Hello world";

                Console.WriteLine("start to send email over SSL...");
                client.Send(Msg);
                Console.WriteLine("email was sent successfully!");
                Console.ReadLine();
            }
            catch (Exception ep)
            {
                Console.WriteLine("failed to send email with the following error:");
                Console.WriteLine(ep.Message);
                Console.ReadLine();
            }
        }

Try to change the SMTP server name to outlook.office365.com instead of smtp.office365.com

Related