So far this is what i've tried, I want to send an email to our school email account with format of jcvborlagdan@mymail.mapua.edu.ph or something like jcborlagdan@mapua.edu.ph. 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("mymail@mapua.edu.ph", "myPassword");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("mymail@mapua.edu.ph", "CTMIS-no-reply");
mail.To.Add(new MailAddress("carlo.borlagdan@the-v.net"));
mail.CC.Add(new MailAddress("jcborlagdan@ymail.com"));
smtpClient.Send(mail);
Some small changes were required to get your code working.
- UseDefaultCredentials need to be set to False since you want to use custom credentials
- UseDefaultCredentials need to be set to False before setting the credentials.
- 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("mymail@mapua.edu.ph", "myPassword");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("mymail@mapua.edu.ph", "CTMIS-no-reply");
mail.To.Add(new MailAddress("carlo.borlagdan@the-v.net"));
mail.CC.Add(new MailAddress("jcborlagdan@ymail.com"));
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.