System.Net.Mail和System.Web.Mail
System.Net.Mail是作為System.Web.Mail的替代來傳送EMAIL.
System.Net.Mail
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1private void SendMailByNet(){
2 MailMessage objMailMessage = new MailMessage();
3
4 objMailMessage.From=new MailAddress("UserFromMail");
5 objMailMessage.To.Add(new MailAddress("UserToMail"));
6 objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
7 objMailMessage.Subject = "This is test";
8 objMailMessage.Body = "Hi,Pippo
This is testing Email.";
9 objMailMessage.IsBodyHtml = true;
10
11 SmtpClient objSmtpClient = new SmtpClient();
12 objSmtpClient.Host = "SMTP";
13 objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
14 objSmtpClient.Credentials = new System.Net.NetworkCredential("UserFromMail","PWD");
15 //objSmtpClient.EnableSsl = true;//SMTP 伺服器要求安全連線需要設定此屬性
16
17 try
18 {
19 objSmtpClient.Send(objMailMessage);
20 }
21 catch (Exception ex)
22 {
23 Response.Write(ex.Message);
24 }
25}
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1private void SendMailByNet(){
2 MailMessage objMailMessage = new MailMessage();
3
4 objMailMessage.From=new MailAddress("UserFromMail");
5 objMailMessage.To.Add(new MailAddress("UserToMail"));
6 objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
7 objMailMessage.Subject = "This is test";
8 objMailMessage.Body = "Hi,Pippo
This is testing Email.";
9 objMailMessage.IsBodyHtml = true;
10
11 SmtpClient objSmtpClient = new SmtpClient();
12 objSmtpClient.Host = "SMTP";
13 objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
14 objSmtpClient.Credentials = new System.Net.NetworkCredential("UserFromMail","PWD");
15 //objSmtpClient.EnableSsl = true;//SMTP 伺服器要求安全連線需要設定此屬性
16
17 try
18 {
19 objSmtpClient.Send(objMailMessage);
20 }
21 catch (Exception ex)
22 {
23 Response.Write(ex.Message);
24 }
25}
System.Web.Mail
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1private void SendMailByWeb()
2{
3 MailMessage objMailMessage = new MailMessage();
4
5 SmtpMail.SmtpServer = System.Configuration.ConfigurationManager.AppSettings["SMTP"];
6
7 objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
8 objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", System.Configuration.ConfigurationManager.AppSettings["FROM"]);
9 //objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", System.Configuration.ConfigurationManager.AppSettings["PWD"]);//密碼可以不提供
10 objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SMTP 伺服器要求安全連線需要設定此屬性
11
12 objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
13 objMailMessage.From = System.Configuration.ConfigurationManager.AppSettings["FROM"];
14 objMailMessage.To = "UserToMail";
15 objMailMessage.Subject = "this is test";
16 objMailMessage.Body = "Hi Pippo,
This is testing EMAIL.";
17 objMailMessage.BodyFormat = MailFormat.Html;
18
19 try
20 {
21 SmtpMail.Send(objMailMessage);
22 }
23 catch (Exception ex)
24 {
25 Response.Write(ex.Message);
26 }
27}
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1private void SendMailByWeb()
2{
3 MailMessage objMailMessage = new MailMessage();
4
5 SmtpMail.SmtpServer = System.Configuration.ConfigurationManager.AppSettings["SMTP"];
6
7 objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
8 objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", System.Configuration.ConfigurationManager.AppSettings["FROM"]);
9 //objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", System.Configuration.ConfigurationManager.AppSettings["PWD"]);//密碼可以不提供
10 objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SMTP 伺服器要求安全連線需要設定此屬性
11
12 objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
13 objMailMessage.From = System.Configuration.ConfigurationManager.AppSettings["FROM"];
14 objMailMessage.To = "UserToMail";
15 objMailMessage.Subject = "this is test";
16 objMailMessage.Body = "Hi Pippo,
This is testing EMAIL.";
17 objMailMessage.BodyFormat = MailFormat.Html;
18
19 try
20 {
21 SmtpMail.Send(objMailMessage);
22 }
23 catch (Exception ex)
24 {
25 Response.Write(ex.Message);
26 }
27}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-617614/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 路徑中./和../和/
- ../和./和/的區別
- not in 和 not exists 比較和用法
- xftp和xshell,xftp和xshell的下載和安裝FTP
- #和&
- !=和<>
- ABAP和Java的destination和JNDIJava
- @NotEmpty和@NotBlank和@NotNull小結Null
- 字首和與二維字首和
- ♻️同步和非同步;並行和併發;阻塞和非阻塞非同步並行
- XML基本操作-建立(DOM和LOINQ)和LINQ查詢和儲存XML
- workman 和swoole 區別 和異同
- 寬鬆相等和嚴格相等(==和===)
- 淺談mouseenter和mouseover,mouseout和mouseleave
- csv和excel讀取和下載Excel
- Cookie 和 Session 關係和區別CookieSession
- javafx 和swing_整合JavaFX和SwingJava
- 檔案路徑問題( ./ 和 ../ 和 @/ )
- 堆和棧的概念和區別
- ThymeleafViewResolver和SpringTemplateEngine和SpringResourceTemplateResolver的關係ViewSpring
- 尤拉計劃739:和的和
- 【-Flutter/Dart 語法補遺-】 sync* 和 async* 、yield 和yield* 、async 和 awaitFlutterDartAI
- if if和if else if
- ul和
- 字首和
- equals 和 ==
- Redis RDB和AOF取捨和選擇Redis
- ssr、ss和vpn介紹和區別
- 使用Jquery和JSON的州和城市列表jQueryJSON
- VM和Container 虛擬機器和容器AI虛擬機
- 和AI談倫理、道德和謊言AI
- 堆和棧的解釋和區別
- lodsb、stosb(和lodsw、stosw和lodsd、stosd指令)
- vue和react的相同點和不同點VueReact
- MySQL 裡的 find_in_set () 和 in () 和 likeMySql
- DOORS和Reqtify — 需求管理和需求追溯工具QT
- count (*) 和 count (1) 和 count (列名) 區別
- DOORS 和Reqtify — 需求管理和需求追溯工具QT
- Golang 陣列和切片 Slice 和 Map 使用Golang陣列