发送邮件到Outlook帐户ASP.Net C# [英] Send mail to outlook account ASP.Net C#

查看:159
本文介绍了发送邮件到Outlook帐户ASP.Net C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,这是我所尝试的,我想向我们学校的电子邮件帐户发送一封电子邮件,其格式为 jcvborlagdan@mymail.mapua.edu.ph 或类似 jcborlagdan @ mapua.edu.ph 。我确定这是一个Outlook帐户,所以我为Outlook设置了smtp设置,但是当我这样做时,我不断遇到以下错误:

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.

我在这里做什么错?我已经搜索了错误,但是除了smtp设置之外,所有答案都与我的语法相同。因此,我的Outlook的smtp设置肯定有问题。

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.


  1. 由于要使用自定义凭据,因此需要将UseDefaultCredentials设置为 False b
  2. UseDefaultCredentials必须先设置为False,然后再设置凭据。

  3. Outlook的SSL端口是587。

  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.

仅此而已。

这里是固定的代码。

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

关于 UseDefaultCredentials

来自 MSDN

某些SMTP服务器要求在客户端代表其发送电子邮件之前,必须先对客户端进行身份验证。 在服务器请求时,此SmtpClient对象应使用当前登录用户的默认凭据进行身份验证时,将此属性设置为true。

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.

-

由于您不想使用Windows凭据进行身份验证,因此
属性设置为False。至于您需要把它放在前面的事实,我没有官方消息来源,但是如果您在将该属性设置为false之前设置您的凭据,那根本就行不通。

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.

这篇关于发送邮件到Outlook帐户ASP.Net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆