JavaMail 邮件发送程序

一个JavaMail的邮件发送程序,可以发送普通邮件,HTML格式邮件,以及带单个或者多个附件,代码如下,需要依赖JavaMail包,下载地址

  1 package com.ylk.puzzle;
  2  
  3 import java.io.File;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 import java.util.Properties;
  7  
  8 import javax.activation.DataHandler;
  9 import javax.activation.FileDataSource;
 10 import javax.mail.Address;
 11 import javax.mail.Message;
 12 import javax.mail.Multipart;
 13 import javax.mail.Session;
 14 import javax.mail.Transport;
 15 import javax.mail.internet.InternetAddress;
 16 import javax.mail.internet.MimeBodyPart;
 17 import javax.mail.internet.MimeMessage;
 18 import javax.mail.internet.MimeMultipart;
 19 import javax.mail.internet.MimeUtility;
 20  
 21 public class EmailUtil {
 22  
 23     private static Session session = null;
 24  
 25     static {
 26         // Get system properties
 27         Properties properties = System.getProperties();
 28  
 29         // Setup mail server
 30         properties.setProperty("mail.smtp.host", "your mail host");
 31  
 32         // Get the default Session object.
 33         session = Session.getDefaultInstance(properties);
 34     }
 35  
 36     /**
 37      * 发送普通邮件
 38      */
 39     public static void sendMail(String from, String to, String title,
 40             String text) throws Exception {
 41         List<String> recipients = new ArrayList<String>();
 42         recipients.add(to);
 43         sendMail(from, recipients, title, text);
 44     }
 45  
 46     /**
 47      * 发送普通邮件-- 多收件人
 48      */
 49     public static void sendMail(String from, List<String> to, String title,
 50             String text) throws Exception {
 51         MimeMessage message = new MimeMessage(session);
 52  
 53         message.setFrom(new InternetAddress(from));
 54  
 55         Address[] recipients = new Address[to.size()];
 56         for (int i = 0; i < to.size(); i++) {
 57             recipients[i] = new InternetAddress(to.get(i));
 58         }
 59         message.addRecipients(Message.RecipientType.TO, recipients);
 60  
 61         //UTF-8编码
 62         message.setSubject(title,"UTF-8");
 63  
 64         message.setText(text);
 65  
 66         Transport.send(message);
 67     }
 68  
 69     /**
 70      * 发送单个带附件邮件至单人
 71      */
 72     public static void sendAttachMail(String from, String to, String title,
 73             String text, File file) throws Exception {
 74  
 75         List<String> recipients = new ArrayList<String>();
 76         recipients.add(to);
 77         sendAttachMail(from,recipients,title,text,file);
 78     }
 79     /**
 80      * 发送单个附件邮件到多人
 81      */
 82     public static void sendAttachMail(String from, List<String> to, String title,
 83             String text, File file) throws Exception {
 84  
 85         List<File> files = new ArrayList<File>();
 86         files.add(file);
 87         sendAttachMail(from,to,title,text,files);
 88     }
 89  
 90     /**
 91      * 发送多个附件到单个人
 92      * @param from
 93      * @param to
 94      * @param title
 95      * @param text
 96      * @param files
 97      * @throws Exception
 98      */
 99     public static void sendAttachMail(String from, String to, String title,
100             String text, List<File> files)throws Exception{
101         List<String> recipients = new ArrayList<String>();
102         recipients.add(to);
103         sendAttachMail(from,recipients,title,text,files);
104     }
105  
106     /**
107      * 发送多附件邮件到多人
108      */
109     public static void sendAttachMail(String from, List<String> to,
110             String title, String text, List<File> files) throws Exception {
111         MimeMessage message = new MimeMessage(session);
112         message.setFrom(new InternetAddress(from));
113  
114         InternetAddress[] addresses = new InternetAddress[to.size()];
115         for (int i = 0; i < to.size(); i++) {
116             addresses[i] = new InternetAddress(to.get(i));
117         }
118         message.setRecipients(Message.RecipientType.TO, addresses);
119  
120         //UTF-8编码
121         message.setSubject(title,"UTF-8");
122  
123         // create the Multipart and add its parts to it
124         Multipart multiPart = new MimeMultipart();
125  
126         // create and fill the first message part
127         MimeBodyPart textPart = new MimeBodyPart();
128         textPart.setText(text);
129         multiPart.addBodyPart(textPart);
130  
131         // attach the file to the message
132         for(File file: files){
133             // create the second message part
134             MimeBodyPart attachmentPart = new MimeBodyPart();
135             FileDataSource fds = new FileDataSource(file);
136             attachmentPart.setDataHandler(new DataHandler(fds));
137             attachmentPart.setFileName(fds.getName());
138             multiPart.addBodyPart(attachmentPart);
139         }
140  
141         // add the Multipart to the message
142         message.setContent(multiPart);
143  
144         // send the message
145         Transport.send(message);
146     }
147  
148     /**
149      * 发送html内容邮件到单人
150      * @param from
151      * @param to
152      * @param title
153      * @param text
154      * @throws Exception
155      */
156     public static void sendHtmlMail(String from, String to, String title,
157             String text)throws Exception {
158         List<String> recipients = new ArrayList<String>();
159         recipients.add(to);
160         sendHtmlMail(from,recipients,title,text);
161  
162     }
163  
164     /**
165      * 发送单个html内容邮件到多人
166      * @param from
167      * @param to
168      * @param title
169      * @param text
170      * @throws Exception
171      */
172     public static void sendHtmlMail(String from, List<String> to, String title,
173             String text) throws Exception {
174  
175         MimeMessage message = new MimeMessage(session);
176  
177         message.setFrom(new InternetAddress(from));
178  
179         Address[] recipients = new Address[to.size()];
180         for (int i = 0; i < to.size(); i++) {
181             recipients[i] = new InternetAddress(to.get(i));
182         }
183         message.addRecipients(Message.RecipientType.TO, recipients);
184  
185         //UTF-8编码
186         message.setSubject(title,"UTF-8");
187  
188         Multipart multiPart = new MimeMultipart();
189  
190         MimeBodyPart mimeBodyPart = new MimeBodyPart();
191         mimeBodyPart.setContent(text,"text/html;charset=utf-8");
192         multiPart.addBodyPart(mimeBodyPart);
193  
194         message.setContent(multiPart);
195  
196         Transport.send(message);
197     }
198  
199     /**
200      * 发送单个带附件的html邮件到单人
201      * @param from
202      * @param to
203      * @param title
204      * @param text
205      * @param file
206      * @throws Exception
207      */
208     public static void sendHtmlMailWitAttach(String from, String to, String title,
209             String text,File file) throws Exception{
210         List<String> recipients = new ArrayList<String>();
211         recipients.add(to);
212         sendHtmlMailWitAttach(from, recipients, title, text, file);
213     }
214  
215     /**
216      * 发送单个带附件的html邮件到多人
217      * @param from
218      * @param to
219      * @param title
220      * @param text
221      * @param file
222      * @throws Exception
223      */
224     public static void sendHtmlMailWitAttach(String from, List<String> to, String title,
225             String text,File file) throws Exception{
226         List<File> files = new ArrayList<File>();
227         files.add(file);
228         sendHtmlMailWitAttach(from, to, title, text, files);
229     }
230  
231     /**
232      * 发送多个带附件的html邮件到单人
233      * @param from
234      * @param to
235      * @param title
236      * @param text
237      * @param files
238      * @throws Exception
239      */
240     public static void sendHtmlMailWitAttach(String from, String to, String title,
241             String text,List<File> files) throws Exception{
242         List<String> recipients = new ArrayList<String>();
243         recipients.add(to);
244         sendHtmlMailWitAttach(from, recipients, title, text, files);
245     }
246  
247     /**
248      * 发送带多个附件的html至多人
249      * @param from
250      * @param to
251      * @param title
252      * @param text
253      * @param files
254      * @throws Exception
255      */
256     public static void sendHtmlMailWitAttach(String from, List<String> to, String title,
257             String text, List<File> files) throws Exception{
258         MimeMessage message = new MimeMessage(session);
259         message.setFrom(new InternetAddress(from));
260  
261         InternetAddress[] addresses = new InternetAddress[to.size()];
262         for (int i = 0; i < to.size(); i++) {
263             addresses[i] = new InternetAddress(to.get(i));
264         }
265         message.setRecipients(Message.RecipientType.TO, addresses);
266         //UTF-8 编码
267         message.setSubject(title,"UTF-8");
268  
269         // create the Multipart and add its parts to it
270         Multipart multiPart = new MimeMultipart();
271  
272         // create and fill the html part
273         MimeBodyPart mimeBodyPart = new MimeBodyPart();
274         mimeBodyPart.setContent(text,"text/html;charset=utf-8");
275         multiPart.addBodyPart(mimeBodyPart);
276  
277         // attach the file to the message
278         for(File file: files){
279             // create the second message part
280             MimeBodyPart attachmentPart = new MimeBodyPart();
281             FileDataSource fds = new FileDataSource(file);
282             attachmentPart.setDataHandler(new DataHandler(fds));
283             attachmentPart.setFileName(fds.getName());
284             multiPart.addBodyPart(attachmentPart);
285         }
286  
287         // add the Multipart to the message
288         message.setContent(multiPart);
289  
290         // send the message
291         Transport.send(message);
292     }
293  
294 }

 

posted @ 2013-01-09 15:42  一筐  阅读(227)  评论(0)    收藏  举报