發送郵件:
1.創建一個郵件會話(MailSession)實例;
2.使用郵件會話創建一個郵件消息(Message)實例;
3.設置郵件消息的內容;
4.使用Tansport.send()方法發送郵件。
1 /**
2 * <p>Title: JavaMail</p>
3 * <p>Description: 發電子郵件</p>
4 * <p>Copyright: Copyright (c) 2005</p>
5 * <p>Company: </p>
6 * @author Derek.G
7 * @version 1.0
8 */
9 import java.util.*;
10 import javax.mail.*;
11 import javax.mail.internet.*;
12 import java.util.Date;
13 import javax.activation.*;
14 import java.io.*;
15
16 public class SendMail {
17 private MimeMessage mimeMsg; //MIME郵件對象
18 private Session session; //郵件會話對象
19 private Properties props; //系統屬性
20 private String username = ""; //smtp認證用戶名和密碼
21 private String password = "";
22 private Multipart mp; //Multipart對象,郵件內容,標題,附件等內容均添加到其中后再生成MimeMessage對象
23 private boolean auth = false; //smtp驗證與否
24
25 public SendMail(String smtp){
26 setSmtpHost(smtp);
27 createMimeMessage();
28 }
29
30 /**
31 * 創建系統屬性Properties
32 * @param hostName String
33 */
34 public void setSmtpHost(String hostName) {
35 System.out.println("設置系統屬性:mail.smtp.host = "+hostName);
36 if(props == null) props = System.getProperties(); //獲得系統屬性對象
37 props.put("mail.smtp.host",hostName); //設置SMTP主機
38 }
39
40
41 /**
42 * 創建郵件會話對象Session、MIME郵件對象MimeMessage
43 * @return boolean
44 */
45 public boolean createMimeMessage(){
46 try{
47 System.out.println("準備獲取郵件會話對象!");
48 if(auth){
49 PopupAuthenticator popupAuthenticator = new PopupAuthenticator();//驗證類
50 PasswordAuthentication pop = popupAuthenticator.performCheck(username,password);
51 session = Session.getInstance(props,popupAuthenticator);
52 }else {
53 session = Session.getInstance(props,null); //獲得郵件會話對象
54 }
55 }catch(Exception e){
56 System.err.println("獲取郵件會話對象時發生錯誤!"+e);
57 return false;
58 }
59 System.out.println("準備創建MIME郵件對象!");
60 try{
61 mimeMsg = new MimeMessage(session); //創建MIME郵件對象
62 mp = new MimeMultipart();
63 return true;
64 }catch(Exception e){
65 System.err.println("創建MIME郵件對象失敗!"+e);
66 return false;}
67 }
68
69 /**
70 * 設置smtp身份認證
71 * @param need boolean
72 */
73 public void setNeedAuth(boolean need) {
74 System.out.println("設置smtp身份認證:mail.smtp.auth = "+need);
75 if(props == null)props = System.getProperties();
76 if(need){
77 props.put("mail.smtp.auth","true");
78 auth = true;
79 }else props.put("mail.smtp.auth","false");
80 }
81
82 /**
83 * 設置用戶名密碼
84 * @param name String
85 * @param pass String
86 */
87 public void setNamePass(String name,String pass) {
88 username = name;
89 password = pass;
90 }
91
92 /**
93 * 設置郵件主題
94 * @param mailSubject String
95 * @return boolean
96 */
97 public boolean setSubject(String mailSubject) {
98 System.out.println("設置郵件主題!");
99 try{
100 mimeMsg.setSubject(mailSubject);
101 return true;
102 }catch(Exception e) {
103 System.err.println("設置郵件主題發生錯誤!");
104 return false;}
105 }
106
107 /**
108 * 設置郵件體文本內容
109 * @param mailBody String
110 */
111 public boolean setBody(String mailBody) {
112 try{
113 BodyPart bp = new MimeBodyPart();
114 bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+mailBody,"text/html;charset=GB2312");
115 mp.addBodyPart(bp);
116 return true;
117 }catch(Exception e){
118 System.err.println("設置郵件正文時發生錯誤!"+e);
119 return false;
120 }
121 }
122
123 /**
124 * 設置郵件附件
125 * @param filename String
126 * @return boolean
127 */
128 public boolean addFileAffix(String filename) {
129
130 System.out.println("增加郵件附件:"+filename);
131
132 try{
133 BodyPart bp = new MimeBodyPart();
134 FileDataSource fileds = new FileDataSource(filename);
135 bp.setDataHandler(new DataHandler(fileds));
136 bp.setFileName(fileds.getName());
137 mp.addBodyPart(bp);
138 return true;
139 }catch(Exception e){
140 System.err.println("增加郵件附件:"+filename+"發生錯誤!"+e);
141 return false;
142 }
143 }
144
145 /**
146 * 設置發信人
147 * @param from String
148 * @return boolean
149 */
150 public boolean setFrom(String from) {
151 System.out.println("發信人:"+from);
152 try{
153 mimeMsg.setFrom(new InternetAddress(from)); //設置發信人
154 return true;
155 }catch(Exception e){ return false; }
156 }
157
158 /**
159 * 設置收件人
160 * @param to String
161 * @return boolean
162 */
163 public boolean setTo(String to){
164 System.out.println("收件人:"+to);
165 if(to == null)return false;
166 try{
167 mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
168 return true;
169 }catch(Exception e){ return false; }
170 }
171
172 /**
173 * @param copyto String
174 * @return boolean
175 */
176 public boolean setCopyTo(String copyto)
177 {
178 if(copyto == null)return false;
179 try{
180 mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto));
181 return true;
182 }catch(Exception e){ return false; }
183 }
184
185 /**
186 * 發郵件操作
187 * @return boolean
188 */
189 public boolean sendout(){
190 try {
191 mimeMsg.setContent(mp);
192 mimeMsg.saveChanges();
193 System.out.println("正在發送郵件
.");
194 mimeMsg.setSentDate(new Date());
195 Transport transport = session.getTransport("smtp");
196 transport.connect( (String) props.get("mail.smtp.host"), username,password);
197 transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
198 transport.close();
199 // Transport.send(mimeMsg);
200 }catch (MessagingException ex) {
201 System.err.println("郵件發送失敗!"+ex.getMessage());
202 return false;
203 }
204 System.out.println("郵件發送完成!");
205 return true;
206 }
207 /**
208 * Just do it as this
209 */
210 public static void main(String[] args) {
211 String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+
212 "<div align=center>JAVAMAIL發送郵件測試<img src='usi.png' width='30' height='30'></div>";
213 SendMail themail = new SendMail("smtp.126.com");
214 themail.setNamePass("envoydada","12140827"); //登陸帳號密碼
215 themail.setNeedAuth(true);//設置SMTP是否驗證
216 if(themail.setSubject("JavaMailTest") == false) return; //設置標題
217 if(themail.setBody(mailbody) == false) return; //設置郵件體
218 if(themail.setTo("derek_g@usish.com") == false) return; //設置收件人
219 if(themail.setFrom("envoydada@126.com") == false) return; //設置發件人
220 if(themail.addFileAffix("c:\\usi.png") == false) return; //設置附件
221 if(themail.addFileAffix("c:\\tmuninst.ini") == false) return;
222 if(themail.sendout() == false) return; //發出郵件
223 }
224 }
225
posted on 2006-03-24 09:38
Derek.Guo 閱讀(513)
評論(0) 編輯 收藏 所屬分類:
Java