今天在看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 == 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存在。
新聞熱點
疑難解答