星空网 > 软件开发 > ASP.net

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 == 0)        {          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

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

德国VAT申请税务证书需要多久?:https://www.kjdsnews.com/a/1387955.html
德国VAT申请税务证书的时间要求:https://www.kjdsnews.com/a/1387956.html
德国VAT申请的烦恼解决方法汇总:https://www.kjdsnews.com/a/1387957.html
德国VAT申请的步骤及注意事项:https://www.kjdsnews.com/a/1387958.html
德国VAT申请流程指南一步一步走向成功:https://www.kjdsnews.com/a/1387959.html
德国VAT申请流程及费用汇总:https://www.kjdsnews.com/a/1387960.html
怎样做出一个有利可图的SaaS产品?:https://www.kjdsnews.com/a/1836639.html
【再放信号】美国Etsy即将放开中国卖家入驻,官方邮件你收到了吗?:https://www.kjdsnews.com/a/1836640.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流