发送邮件的工具类
实现java发送邮件的过程大体有以下几步:
- 准备一个properties文件,该文件中存放SMTP服务器地址等参数。
- 利用properties创建一个Session对象
- 利用Session创建Message对象,然后设置邮件主题和正文
- 利用Transport对象发送邮件
需要的jar有2个:activation.jar和mail.jar
下面是实现:
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 }