發送郵件的工具類

實現java發送郵件的過程大致有如下幾步:html

  1. 準備一個properties文件,該文件中存放SMTP服務器地址等參數。
  2. 利用properties建立一個Session對象
  3. 利用Session建立Message對象,而後設置郵件主題和正文
  4. 利用Transport對象發送郵件

須要的jar有2個:activation.jar和mail.jarjava

下面是實現:服務器

 1 /**
 2      * 發送郵件的方法
 3      * 
 4      * @param serverAddress 服務器地址
 5      * @param user 服務器用戶名
 6      * @param pwd 服務器密碼
 7      * @param from 發送人
 8      * @param subject 標題
 9      * @param emailReceiver 接收人
10      * @param htmlContents 發送內容
11      * @param ucode 編碼格式 text/html;charset=utf-8
12      */
13     public static void send(String serverAddress, String user, String pwd, String from, String subject, String[] emailReceiver, String htmlContents, String ucode,List<String> fileName, String fileNames) {
14         if (null == fileName) {
15             fileName = new ArrayList<String>();
16         }
17         
18         try {
19             Properties props = System.getProperties();
20             props.put("mail.smtp.host", serverAddress);
21             props.put("mail.smtp.auth", "true");
22             
23             Session session = Session.getDefaultInstance(props, null);
24             
25             Message msg = new MimeMessage(session);
26             msg.setFrom(new InternetAddress(from));
27             msg.setSentDate(new Date());
28             msg.setSubject(subject);
29             if (ucode.indexOf("GBK") != -1) {
30                 msg.setFrom(new InternetAddress(new String(from.getBytes("GBK"), "UTF-8")));
31             }
32             
33             InternetAddress[] address = new InternetAddress[emailReceiver.length];
34             for (int i = 0; i < address.length; i++) {
35                 address[i] = new InternetAddress(emailReceiver[i]);
36             }
37             msg.setRecipients(Message.RecipientType.TO, address);
38             
39             Multipart mp = new MimeMultipart();// 後面的BodyPart將加入到此處建立的Multipart中
40             BodyPart bp = new MimeBodyPart();// 設置內容
41             bp.setContent(htmlContents, ucode);
42             mp.addBodyPart(bp);
43             addFileAffix(mp, fileName, fileNames);//保存附件
44             msg.setContent(mp);// Multipart加入到信件
45             
46             msg.saveChanges();
47             
48             // 發送信件
49             Transport trans = session.getTransport("smtp");
50             trans.connect(serverAddress, user, pwd);
51             trans.sendMessage(msg, msg.getAllRecipients());
52             trans.close();
53         } catch (AddressException e) {
54             throw new SysException("Email地址錯誤", e);
55         } catch (UnsupportedEncodingException e) {
56             throw new SysException("Email編碼格式錯誤", e);
57         } catch (NoSuchProviderException e) {
58             throw new SysException("Email服務器鏈接錯誤", e);
59         } catch (MessagingException e) {
60             throw new SysException("Email服務器鏈接錯誤", e);
61         } catch (Exception e) {
62             throw new SysException(e.getMessage(), e);
63         }
64     }
65     
66     /**
67      * 發送附件
68      * @param mp 郵件信息
69      * @param fileName 附件全路徑(包括文件名稱)
70      * @return
71      */
72     public static void addFileAffix(Multipart mp, List<String> list,String fileNames) {
73         if (CollectionUtils.isEmpty(list)) {
74             return;
75         }
76         
77         try{
78             String[] arr = fileNames.split(";");
79             for (String fileName : list) {
80                 if(StringUtils.isEmpty(fileName)) {
81                     return;
82                 }
83                 
84                 String newName = fileName.substring(fileName.lastIndexOf(File.separator) + 1, fileName.length());
85                 for (int i = 0; i < arr.length; i++) {
86                     if (newName.equals(arr[i].split(":")[0])) {
87                         newName = arr[i].split(":")[1];
88                     }
89                 }
90                 
91                 BodyPart bp = new MimeBodyPart();
92                 bp.setDataHandler(new DataHandler(new FileDataSource(fileName)));
93                 bp.setFileName(MimeUtility.encodeText(newName));//附件名字亂碼
94                 mp.addBodyPart(bp);
95             }
96         } catch(Exception e) {
97             e.printStackTrace();
98         }
99     }
相關文章
相關標籤/搜索