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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用System.Net.Mail實現郵件群發AI
- 利用System.Web.Mail傳送EMail程式碼WebAI
- C#傳送郵件,使用System.Web.Mail的版本C#WebAI
- Asp.net 2.0 傳送Email(Using System.web.Mail)ASP.NETAIWeb
- .net System.Web.Mail傳送郵件的實際應用程式碼WebAI
- 替代System.Web.Mail的新類庫(新增郵件列表功能) (轉)WebAI
- ../和./和/的區別
- 路徑中./和../和/
- linux中/bin和/sbin和/usr/bin和/usr/sbinLinux
- redis的安裝和啟動和檢測和停止Redis
- 深度解析 Delegate 和 Notification 和 KVO
- xftp和xshell,xftp和xshell的下載和安裝FTP
- 找工作學習筆記1------=和==、&和&&、|和||的區別筆記
- workman 和swoole 區別 和異同
- @NotEmpty和@NotBlank和@NotNull小結Null
- 檔案路徑問題( ./ 和 ../ 和 @/ )
- csv和excel讀取和下載Excel
- Cookie 和 Session 關係和區別CookieSession
- 堆和棧的概念和區別
- js中的typeof和instanceof和===JS
- hbase和zookeeper的安裝和部署
- JSF和Struts、Tiles Portlets和TapestryJS
- oracle中關於in和exists,not in 和 not existsOracle
- js == 和 ===JS
- 字首和
- XML基本操作-建立(DOM和LOINQ)和LINQ查詢和儲存XML
- 函式fgets和fputs、fread和fwrite、fscanf和fprintf用法小結函式
- DOORS和Reqtify — 需求管理和需求追溯工具QT
- Golang 陣列和切片 Slice 和 Map 使用Golang陣列
- count (*) 和 count (1) 和 count (列名) 區別
- DOORS 和Reqtify — 需求管理和需求追溯工具QT
- MySQL 裡的 find_in_set () 和 in () 和 likeMySql
- 淺談mouseenter和mouseover,mouseout和mouseleave
- 和AI談倫理、道德和謊言AI
- count(*) 和 count(1)和count(列名)區別
- ssr、ss和vpn介紹和區別
- Redis RDB和AOF取捨和選擇Redis
- 堆和棧的解釋和區別