0

So far this is what i've tried, I want to send an email to our school email account with format of [email protected] or something like [email protected]. I'm sure that this is an outlook account so I took the smtp settings for outlook, but when I do this I keep on encountering the following error:

Failure sending mail.

What am I doing wrong here? I already search for the error but all of the answers are showing same syntax with mine except for the smtp settings. So there must be something wrong with my smtp settings for outlook.

SmtpClient smtpClient = new SmtpClient("smtp-mail.outlook.com", 25); //587 smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "myPassword"); smtpClient.UseDefaultCredentials = true; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.EnableSsl = true; MailMessage mail = new MailMessage(); mail.From = new MailAddress("[email protected]", "CTMIS-no-reply"); mail.To.Add(new MailAddress("[email protected]")); mail.CC.Add(new MailAddress("[email protected]")); smtpClient.Send(mail); 

1 Answer 1

3

Some small changes were required to get your code working.

  1. UseDefaultCredentials need to be set to False since you want to use custom credentials
  2. UseDefaultCredentials need to be set to False before setting the credentials.
  3. SSL port is 587 for Outlook.

Thats all.

Here is the code fixed.

SmtpClient smtpClient = new SmtpClient("smtp-mail.outlook.com", 587); //587 smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "myPassword"); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.EnableSsl = true; MailMessage mail = new MailMessage(); mail.From = new MailAddress("[email protected]", "CTMIS-no-reply"); mail.To.Add(new MailAddress("[email protected]")); mail.CC.Add(new MailAddress("[email protected]")); smtpClient.Send(mail); 

Concerning UseDefaultCredentials

From MSDN:

Some SMTP servers require that the client be authenticated before the server sends e-mail on its behalf. Set this property to true when this SmtpClient object should, if requested by the server, authenticate using the default credentials of the currently logged on user.

--

Since you don't want to authenticate using your Windows credentials, the property is set to False. As for the fact you need to put it before, I have no official source but it simply does not work if you set your credentials before setting that property to false.

Sign up to request clarification or add additional context in comments.

2 Comments

i'll try first.. thanks a lot for the quick response
ahh my problem was the server port. it's not contacting directly to the port. but many thanks to the information sir @Sage

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.