Send mail to outlook account ASP.Net C#

六月ゝ 毕业季﹏ 提交于 2019-12-25 07:17:20

问题


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);

回答1:


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("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.



来源:https://stackoverflow.com/questions/37505394/send-mail-to-outlook-account-asp-net-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!