做用戶註冊時免不瞭要讓用戶使用郵件激活賬號,這時就得用到使用程序發郵件的技術瞭。如下是我在項目中常用的發郵件的方法:
【C#】
1 usingSystem.Net.Mail;
2 usingSystem.Net;
3
4 publicclassEmailEntity
5 {
6 privateMailMessage mm;
7
8 ///<summary>
9 ///發送郵件
10 ///</summary>
11 publicvoidsendMessage()
12 {
13
14 //指定發件人的郵箱地址
15 MailAddress fromAddress = newMailAddress("abc@163.com");
16 //指定收件人的郵箱地址
17 MailAddress toAddress = newMailAddress("efg@qq.com");
18 //創建郵件對象
19 mm = newMailMessage(fromAddress, toAddress);
20 //添加一個密送的郵件地址
21 mm.Bcc.Add(newMailAddress("xiuxiu@qq.com"));
22 //添加一個抄送的郵件地址
23 mm.CC.Add(newMailAddress("me@qq.com"));
24 //指定郵件標題的編碼格式
25 mm.SubjectEncoding = Encoding.UTF8;
26 //設置郵件標題
27 mm.Subject = "我是標題";
28 //指定郵件正文編碼
29 mm.BodyEncoding = Encoding.UTF8;
30 //指定郵件正文是否text/html類型
31 mm.IsBodyHtml = true;
32 //設置郵件正文內容
33 mm.Body = "<span style='color:#ff0000;font-weight:bold;'>我愛秀秀!</span>";
34 //創建附件
35 Attachment file = newAttachment(AppDomain.CurrentDomain.BaseDirectory+"xiuxiu.jpg");
36 //設置附件的創建日期
37 file.ContentDisposition.CreationDate = DateTime.Now;
38 //設置附件的類型
39 file.ContentType = newSystem.Net.Mime.ContentType("image/jpeg");
40 //將附件添加到郵件中
41 mm.Attachments.Add(file);
42
43 //指定郵件發送服務器的地址和端口號
44 SmtpClient sc = newSmtpClient("smtp.163.com", 25);
45 //指定發件人的身份驗證信息
46 sc.Credentials = newNetworkCredential("abc", "123456");
47 //sc.Send(mm);
48 //註冊異步發送事件
49 sc.SendCompleted += newSendCompletedEventHandler(sc_SendCompleted);
50 //開始執行異步發送郵件
51 sc.SendAsync(mm, null);
52 }
53 //異步發送郵件完成時處理事件
54 voidsc_SendCompleted(objectsender, System.ComponentModel.AsyncCompletedEventArgs e)
55 {
56 if(mm != null)
57 {
58 mm.Dispose();//釋放資源
59 }
60 if(e.Cancelled)
61 {
62 Console.WriteLine("用戶已取消!");
63 }
64 elseif(e.Error != null)
65 {
66 Console.WriteLine("發送失敗,原因如下:"+ e.Error.Message);
67 }
68 else
69 {
70 Console.WriteLine("發送成功!");
71 }
72 }
73 }
【Java】
1importjava.util.Date;
2importjava.util.Properties;
3importjavax.mail.Authenticator;
4importjavax.mail.PasswordAuthentication;
5importjavax.mail.Message.RecipientType;
6importjavax.mail.MessagingException;
7importjavax.mail.Session;
8importjavax.mail.Transport;
9importjavax.mail.internet.AddressException;
10importjavax.mail.internet.InternetAddress;
11importjavax.mail.internet.MimeMessage;
12importjavax.mail.internet.MimeMultipart;
13
14/**
15 * 郵件實體類,封裝瞭有關發送郵件的方法。
16 * @authorxxxxxx
17 *
18 */
19publicclassEmailEntity {
20
21 privateMimeMessage msg;
22
23 /**
24 * 初始化郵件實體
25 *
26 * @paramfromDomin
27 * 發件人郵件服務器域名,如:163.com、qq.com
28 * @paramuserName
29 * 發件人用戶名,如:qmail、20082345
30 * @parampassword
31 * 發件人密碼
32 * @paramtoAddress
33 * 收件人郵箱地址,如:200712345@qq.com
34 */
35 publicEmailEntity(String fromDomin, String userName, String password) {
36 Properties p = newProperties();
37 //設置郵件發送服務器的地址
38 p.setProperty("mail.host", "smtp." + fromDomin);
39 //設置使用權限驗證
40 p.setProperty("mail.smtp.auth", "true");
41 //設置用戶身份驗證憑據
42 Session ses = Session.getDefaultInstance(p, newMyAuthentic
ator(userName, password));
43 //ses.setDebug(true);//設置是否出現回顯信息
44 //創建郵件實體
45 msg = newMimeMessage(ses);
46 try{
47 //設置發件人郵箱地址
48 msg.setFrom(newInternetAddress(userName + "@" + fromDomin));
49 } catch(AddressException e) {
50 e.printStackTrace();
51 } catch(MessagingException e) {
52 e.printStackTrace();
53 }
54 }
55
56 /**
57 * 發送消息
58 *
59 * @paramtitle
60 * 郵件標題
61 * @paramcontent
62 * 郵件正文
63 * @paramtype
64 * 正文的類型,如:text/html、text/plain
65 * @return
66 */
67 publicbooleansendMessage(String toAddress, String title, String content, String type) {
68
69 try{
70 //設置發件人郵箱地址
71 msg.setRecipient(RecipientType.TO, newInternetAddress
(toAddress));
72 //設置郵件發送日期
73 msg.setSentDate(newDate());
74 //設置郵件標題
75 msg.setSubject(title);
76 //設置郵件正文
77 msg.setContent(content, type);
78 //開始發送郵件
79 Transport.send(msg);
80 returntrue;
81 } catch(MessagingException ex) {
82 ex.printStackTrace();
83 returnfalse;
84 }
85 }
86
87 /**
88 * 發送帶附件的郵件
89 * @paramtoAddress 收件人的郵箱地址,如suxibo@126.com
90 * @paramtitle 郵件標題
91 * @paramcontent 郵件正文,包括附件
92 * @paramtype 郵件正文的類型
93 * @return
94 */
95 publicbooleansendEmailWithAttachment(String toAddress, String title, MimeMultipart content, String type) {
96 try{
97 msg.setRecipient(RecipientType.TO, newInternetAddress(toAddress));
98 msg.setSentDate(newDate());
99 msg.setSubject(title);
100 msg.setContent(content);
101 Transport.send(msg);
102 returntrue;
103 } catch(MessagingException ex) {
104 ex.printStackTrace();
105 returnfalse;
106 }
107 }
108}
109
110//用戶身份驗證憑據類
111classMyAuthenticator extendsAuthenticator {
112
113 privateString _userName;
114 privateString _password;
115
116 publicMyAuthenticator(String userName,String password){
117 this._userName = userName;
118 this._password = password;
119 }
120
121 @Override
122 publicPasswordAuthentication getPasswordAuthentication() {
123 //返回使用瞭用戶名和密碼的身份驗證憑據
124 returnnewPasswordAuthentication(_userName, _password);
125 }
126}