SmtpClient发邮件时为什么用MailMessage.From而不用MailMessage.Sender

时间:2023-03-08 20:37:46
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下,编译,但是出问题,报了个错

SmtpClient发邮件时为什么用MailMessage.From而不用MailMessage.Sender

我心思是没有指定发件人的用户名和密码,因此又增加了一行代码

sc.Credentials = new System.Net.NetworkCredential("发件邮箱", "密码");

但是依然报这个错误,继续找办法吧,看了看网上其他人发邮件的代码,发现他们用的是From,因此,又增加了一行代码

mm.From = new MailAddress("公司邮箱", "发件人");

发现顿时就好使了,而且两个可以同时存在,或单独From存在,都好使。

这……我就郁闷了,因为F12定位到定义,From和Sender的注解里面只差了一个字

SmtpClient发邮件时为什么用MailMessage.From而不用MailMessage.Sender

一个是发信人,一个是发件人,而且From是发信人,Sender是发件人,一字之差不应该Sender不好使,From好使啊,疑惑增加ing……

那就用ILSpy反编译看一下吧

SmtpClient发邮件时为什么用MailMessage.From而不用MailMessage.Sender

我擦,更加疑惑,不可能只是因为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存在。