SmtpClient发邮件时为什么用MailMessage.From而不用MailMessage.Sender
今天在看C#高级编程(第9版)的时候,在768页看到这样的一段代码
SmtpClient sc = new SmtpClient();
sc.Host = "邮箱服务器地址";
MailMessage mm = new MailMessage();
mm.Sender = new MailAddress("公司邮箱", "发件人");
mm.To.Add(new MailAddress("我的163邮箱", "接收人"));
mm.CC.Add(new MailAddress("抄送的邮箱", "抄送人"));
mm.Subject = "测试程序发送邮件";
mm.Body = "<b>我发送了邮件---我是程序发出的</b>";
mm.IsBodyHtml = true;
mm.Priority = MailPriority.High;
sc.Send(mm);
以前没有做过Email项目,所以,直接就敲到了VS下,编译,但是出问题,报了个错
我心思是没有指定发件人的用户名和密码,因此又增加了一行代码
sc.Credentials = new System.Net.NetworkCredential("发件邮箱", "密码");
但是依然报这个错误,继续找办法吧,看了看网上其他人发邮件的代码,发现他们用的是From,因此,又增加了一行代码
mm.From = new MailAddress("公司邮箱", "发件人");
发现顿时就好使了,而且两个可以同时存在,或单独From存在,都好使。
这……我就郁闷了,因为F12定位到定义,From和Sender的注解里面只差了一个字
一个是发信人,一个是发件人,而且From是发信人,Sender是发件人,一字之差不应该Sender不好使,From好使啊,疑惑增加ing……
那就用ILSpy反编译看一下吧
我擦,更加疑惑,不可能只是因为From比Sender多了一个判断是否为null就用From吧。
那就继续看反编译的代码吧。在SmtpClient里找到Send方法,代码如下
public void Send(MailMessage message)
{
if (Logging.On)
{
Logging.Enter(Logging.Web, this, "Send", message);
}
if (this.disposed)
{
throw new ObjectDisposedException(base.GetType().FullName);
}
try
{
if (Logging.On)
{
Logging.PrintInfo(Logging.Web, this, "Send", "DeliveryMethod=" + this.DeliveryMethod.ToString());
}
if (Logging.On)
{
Logging.Associate(Logging.Web, this, message);
}
SmtpFailedRecipientException ex = null;
if (this.InCall)
{
throw new InvalidOperationException(SR.GetString("net_inasync"));
}
if (message == null)
{
throw new ArgumentNullException("message");
}
if (this.DeliveryMethod == SmtpDeliveryMethod.Network)
{
this.CheckHostAndPort();
}
MailAddressCollection mailAddressCollection = new MailAddressCollection();
if (message.From == null)
{
throw new InvalidOperationException(SR.GetString("SmtpFromRequired"));
}
if (message.To != null)
{
foreach (MailAddress current in message.To)
{
mailAddressCollection.Add(current);
}
}
if (message.Bcc != null)
{
foreach (MailAddress current2 in message.Bcc)
{
mailAddressCollection.Add(current2);
}
}
if (message.CC != null)
{
foreach (MailAddress current3 in message.CC)
{
mailAddressCollection.Add(current3);
}
}
if (mailAddressCollection.Count == )
{
throw new InvalidOperationException(SR.GetString("SmtpRecipientRequired"));
}
this.transport.IdentityRequired = false;
try
{
this.InCall = true;
this.timedOut = false;
this.timer = new Timer(new TimerCallback(this.TimeOutCallback), null, this.Timeout, this.Timeout);
string pickupDirectory = this.PickupDirectoryLocation;
switch (this.DeliveryMethod)
{
case SmtpDeliveryMethod.Network:
goto IL_235;
case SmtpDeliveryMethod.SpecifiedPickupDirectory:
break;
case SmtpDeliveryMethod.PickupDirectoryFromIis:
pickupDirectory = IisPickupDirectory.GetPickupDirectory();
break;
default:
goto IL_235;
}
if (this.EnableSsl)
{
throw new SmtpException(SR.GetString("SmtpPickupDirectoryDoesnotSupportSsl"));
}
bool allowUnicode = this.IsUnicodeSupported();
this.ValidateUnicodeRequirement(message, mailAddressCollection, allowUnicode);
MailWriter mailWriter = this.GetFileMailWriter(pickupDirectory);
goto IL_275;
IL_235:
this.GetConnection();
allowUnicode = this.IsUnicodeSupported();
this.ValidateUnicodeRequirement(message, mailAddressCollection, allowUnicode);
mailWriter = this.transport.SendMail(message.Sender ?? message.From, mailAddressCollection, message.BuildDeliveryStatusNotificationString(), allowUnicode, out ex);
IL_275:
this.message = message;
message.Send(mailWriter, this.DeliveryMethod > SmtpDeliveryMethod.Network, allowUnicode);
mailWriter.Close();
this.transport.ReleaseConnection();
if (this.DeliveryMethod == SmtpDeliveryMethod.Network && ex != null)
{
throw ex;
}
}
catch (Exception ex2)
{
if (Logging.On)
{
Logging.Exception(Logging.Web, this, "Send", ex2);
}
if (ex2 is SmtpFailedRecipientException && !((SmtpFailedRecipientException)ex2).fatal)
{
throw;
}
this.Abort();
if (this.timedOut)
{
throw new SmtpException(SR.GetString("net_timeout"));
}
if (ex2 is SecurityException || ex2 is AuthenticationException || ex2 is SmtpException)
{
throw;
}
throw new SmtpException(SR.GetString("SmtpSendMailFailure"), ex2);
}
finally
{
this.InCall = false;
if (this.timer != null)
{
this.timer.Dispose();
}
}
}
finally
{
if (Logging.On)
{
Logging.Exit(Logging.Web, this, "Send", null);
}
}
}
看代码里我注红色的部分,可以发现,Send方法,必须要求From存在,如果From不存在的话,就会抛出异常,但是并没有强制要求Sender存在,Sender可以存在,如果Sender存在就用Sender,不存在就用From,但是必须保证From存在。
SmtpClient发邮件时为什么用MailMessage.From而不用MailMessage.Sender的更多相关文章
- MimeMessageHelper代码发邮件时,通过客服端登陆到邮箱,在已发送邮件里没有已经通过代码发送的邮件
MimeMessageHelper代码发邮件时,通过客服端登陆到邮箱,在已发送邮件里没有已经通过代码发送的邮件, 这个问题很奇怪,这样的话不能看到通过代码发送的邮件历史记录,所以只好借助秘密抄送了,抄 ...
- 解决升级PHP7.1后,发邮件时提示“fsockopen(): Peer certificate CN=`xxx.xx.com' did not match expected CN=`113.x.xx.98”
把项目环境升级到PHP7.1后,发现在不使用SSL时可以使用IP发邮件,可设置成SSL时就只能使用hostname发送,PHP提示的错误信息大致意思是说,IP与hostname无法通过SSL验证,修改 ...
- C# .net 使用 SmtpClient 发邮件 ,发送邮箱的配置
1.需打开POP3/SMTP/IMAP 2.打开时要求授权码,输入自定义的密码如:1234cb 3.自定义的密码就是 SmtpClient 的密码,而非邮箱密码
- C# SmtpClient 发邮件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Dynamics CRM 2011 通过工作流发邮件时的权限问题
场景: 在CRM中配置工作流,完成某个步骤后,发送邮件通知其他用户.发件人统一配置为管理员,收件人则根据业务需要设定动态值. 相关权限配置 首先启动流程的用户, 需要允许其他用户代表发送电子邮件 另外 ...
- python使用SMTP发邮件时使用Cc(抄送)和Bcc(密送)
SMTP发送邮件的时候,并没有特殊的通信语句告诉邮件服务器 谁是主送,谁是抄送/密送,这三个角色都是以同样的方式告诉邮件服务器的,然后重点在邮件内容里. 邮件内容分为头和体两部分(就像http),头部 ...
- linux(centos8):阿里云ecs配置smtps发邮件(解决不能通过25端口发邮件问题)
一,2016年9月后购买的阿里云ecs不再支持通过25端口发送邮件 官方的建议是使用465端口 465端口(SMTPS): 465端口是为SMTPS(SMTP-over-SSL)协议服务开放的 它是S ...
- C# System.Net.Mail.MailMessage 发邮件
C# System.Net.Mail.MailMessage 发邮件 上篇文化在哪个可以看到使用 System.Web.Mail.MailMessage 发邮件时会提示 ,提供用于构造电子邮件的属性和 ...
- 如何使cron任务出错时发邮件
如果设置了 MAILTO,cron就会将任务的标准输出和标准错误输出重定向到邮箱(即发送邮件).但如果只想接到错误报警邮件 -- 即任务正常执行时不发送,只在出错时发送 -- 应该怎么实现呢? 方法很 ...
随机推荐
- 未能添加对***.dll的引用 问题解决方法
这个不是什么新问题了,这里说一下我遇到的这个操蛋事. 转载请注明出处 http://www.cnblogs.com/zaiyuzhong/p/6236263.html 我做的和往常一样,找到SDK开发 ...
- 总结js的一些复制方法
1.复制对象: var item1={XXX}; var item2=$.extend(true,{},item1);//深度克隆对象(jQuery方法). lodash也有相关方法:https:// ...
- JS写入日志
try { var WSShell = WScript.CreateObject("WScript.Shell"); var FileSys = WScript.CreateObj ...
- dedecms功能性函数封装(XSS过滤、编码、浏览器XSS hack、字符操作函数)
dedecms虽然有诸多漏洞,但不可否认确实是一个很不错的内容管理系统(cms),其他也不乏很多功能实用性的函数,以下就部分列举,持续更新,不作过多说明.使用时需部分修改,你懂的 1.XSS过滤. f ...
- [WPF] 我的WPF自学日记2,自定义入口
在winform中入口文件就是Program.cs,而在WPF中看不到,因为它是自动生成的,可以说隐藏了,我们可以自定义一个入口文件,然后修改项目属性中的启动对象为我们自定义的入口文件. 首先新建入口 ...
- git上传文件出错的时候
$ git pull --rebase origin master 运行这个基本OK!
- jquery修改Switchery复选框的状态
script //选择框 var mySwitch; /* * 初始化Switchery * * classNmae class名 */ function initSwitchery(classNam ...
- AT指令调试程序
extern void Delay(__IO uint32_t nCount); USARType USART_SendStr(UART_HandleTypeDef * USART_Handler,c ...
- 构建 Android 应用程序一定要绕过的 30 个坑
原文地址:Building Android Apps - 30 things that experience made me learn the hard way 原文作者:César Ferreir ...
- nginx-(/usr/local/nginx)配置编译
./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr ...