須要引入 mail.jar,spring.jarjava
1 package com.krm.slsint.mail.util; 2 3 import java.io.File; 4 import java.io.InputStream; 5 import java.util.List; 6 import java.util.Properties; 7 8 import javax.mail.internet.MimeMessage; 9 import javax.mail.internet.MimeUtility; 10 11 import org.apache.commons.io.IOUtils; 12 import org.apache.commons.lang.StringUtils; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.springframework.core.io.ByteArrayResource; 16 import org.springframework.core.io.FileSystemResource; 17 import org.springframework.mail.javamail.JavaMailSenderImpl; 18 import org.springframework.mail.javamail.MimeMessageHelper; 19 20 import com.krm.slsint.mail.vo.Mail; 21 import com.krm.slsint.mail.vo.MailAttachment; 22 import com.krm.slsint.mail.vo.MailUser; 23 24 25 /** 26 * 郵件發送接口 27 */ 28 public class CrsMailSender { 29 30 private static Log log = LogFactory.getLog(CrsMailSender.class); 31 32 /** 33 * 發送郵件 34 * 35 * @param receivers收件人列表 36 * @param carbonCopy抄送人列表可爲空 37 * @param title郵件標題 38 * @param content郵件正文 39 * @param attachments附件 40 * @return 41 */ 42 public boolean sendMail(Mail mail) { 43 MailUser mailUser = mail.getMailUser(); 44 JavaMailSenderImpl sender = this.getSpringMailSender(mail.getMailUser()); 45 try { 46 MimeMessage mimeMessage = sender.createMimeMessage(); 47 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, 48 "GBK"); 49 helper.setFrom(mailUser.getMailAddress(), mailUser.getNickName()); 50 List<String> receivers = mail.getReceiver(); 51 if (receivers != null) { 52 helper.setTo(receivers.toArray(new String[receivers.size()])); 53 } 54 List<String> corbonCopys = mail.getCorbonCopy(); 55 if (corbonCopys != null) { 56 helper.setCc(corbonCopys.toArray(new String[corbonCopys.size()])); 57 } 58 helper.setSubject(mail.getTitle()); 59 helper.setText(mail.getContent(), true); 60 List<MailAttachment> attachments = mail.getAttachments(); 61 if (attachments != null) { 62 for (MailAttachment mailAttachment : attachments) { 63 File file = mailAttachment.getAttachmentFile(); 64 if (file != null && file.exists()) { 65 helper.addAttachment( 66 MimeUtility.encodeWord(mailAttachment.getAttachmentName()), 67 new FileSystemResource(file)); 68 } 69 InputStream stream = mailAttachment.getAttachmentStream(); 70 if (stream != null) { 71 helper.addAttachment( 72 MimeUtility.encodeWord(mailAttachment.getAttachmentName()), 73 new ByteArrayResource(IOUtils 74 .toByteArray(stream))); 75 } 76 } 77 } 78 sender.send(mimeMessage); 79 log.info("Send mail[" + mail.getTitle() + "] end."); 80 } catch (Exception e) { 81 log.error("Send mail[" + mail.getTitle() + "] error."); 82 log.error(e.getMessage(), e); 83 return false; 84 } 85 return true; 86 } 87 88 private JavaMailSenderImpl getSpringMailSender(MailUser mailUser) { 89 JavaMailSenderImpl sender = new JavaMailSenderImpl(); 90 sender.setHost(mailUser.getSmtpIp()); 91 sender.setPort(mailUser.getSmtpPort()); 92 sender.setProtocol("smtp"); 93 sender.setUsername(mailUser.getMailAddress()); 94 sender.setPassword(StringUtils.defaultString(mailUser.getPassword(), "")); 95 sender.setDefaultEncoding("GBK"); 96 Properties javaMailProperties = new Properties(); 97 if ("true".equals(mailUser.getEnableSSL())) { 98 javaMailProperties.setProperty("mail.smtp.socketFactory.class", 99 "javax.net.ssl.SSLSocketFactory"); 100 javaMailProperties.setProperty("mail.smtp.socketFactory.fallback", 101 "false"); 102 javaMailProperties.setProperty("mail.smtp.starttls.enable", "true"); 103 javaMailProperties.setProperty("mail.smtp.starttls.required", 104 "true"); 105 } 106 javaMailProperties.setProperty("mail.smtp.auth", "true"); 107 javaMailProperties.setProperty("mail.smtp.quitwait", "false"); 108 javaMailProperties.setProperty("mail.smtp.timeout", "25000"); 109 sender.setJavaMailProperties(javaMailProperties); 110 return sender; 111 } 112 }