麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

SmtpClient發郵件時為什么用MailMessage.From而不用MailMessage.Sender

2019-11-14 14:11:58
字體:
來源:轉載
供稿:網友

今天在看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存在,都好使。

這&hellip;…我就郁悶了,因為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 == 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存在。

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 午夜久久久精品一区二区三区 | 日本a在线观看 | 91精品国产刺激国语对白 | 在线成人看片 | 国产精品久久久久久久娇妻 | 久久久三区 | 九九热精品视频在线 | 羞羞电影在线观看www | 草草视频在线播放 | 日本黄色大片免费 | 91精品国产综合久久久欧美 | 国产精品久久77777 | 午夜精品小视频 | 国产日韩一区二区三区在线观看 | 精品亚洲va在线va天堂资源站 | 一区二区视| 精品成人久久久 | 久久精品一区二区三区国产主播 | 久久久婷婷一区二区三区不卡 | 欧美一级淫片免费视频1 | 国产免费视频一区二区裸体 | 青青国产在线视频 | 欧美成人午夜影院 | 香蕉久久久精品 | 精品黑人一区二区三区国语馆 | 久久大陆 | 久久久久女人精品毛片九一 | 精品久久中文网址 | av懂色 | 91精品观看91久久久久久国产 | 成人在线免费视频播放 | 福利在线免费视频 | 性爱视频在线免费 | 亚洲性在线视频 | 黄色一级毛片免费看 | 久久久国产精品网站 | 国产在线一区二区三区 | 热re91久久精品国产99热 | chengrenzaixian| 亚洲第一精品在线 | 欧美成人精品欧美一级乱黄 |