Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.bj58.ecat.emc.diywebsite.tongji.wltsync.util;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.mail.Address;
- import javax.mail.Message;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import javax.mail.internet.MimeUtility;
- import javax.mail.util.ByteArrayDataSource;
- /**
- * 邮件处理类,基本用法如下:<br>
- * Mails.create().subject("标题").body("内容 ").send();<br>
- * 多次调用 {@link #body(bodyContent)} 或者 {@link #attachment(name, type, bytes)}
- * 可以多次添加邮件内容或者附件
- * @author lzxz1234<lzxz1234@gmail.com>
- */
- public class Mails {
- private static final String host = Configs.getProperty("email.host");
- private static final String smtphost = Configs.getProperty("email.host.smtp");
- private static final String username = Configs.getProperty("email.user");
- private static final String password = Configs.getProperty("email.pass");
- private static final String fromEmail = username + "@" + host;
- public static final String[] receiveMails = Configs.getProperty(String[].class, "email.receivers");
- private Session session;
- private MimeMessage message;
- /**
- * 创建邮件对象的工厂类
- * @return
- * @throws Exception
- */
- public static Mails create() throws Exception {
- Properties prop = new Properties();
- prop.setProperty("mail.host", smtphost);
- prop.setProperty("mail.transport.protocol", "smtp");
- prop.setProperty("mail.smtp.auth", "true");
- prop.setProperty("mail.smtp.starttls.enable","true");
- Mails result = new Mails();
- result.session = Session.getInstance(prop);
- return result;
- }
- /**
- * 执行发送操作
- * @return
- * @throws Exception
- */
- public Mails send() throws Exception {
- message.saveChanges();
- Transport ts = session.getTransport();
- ts.connect(smtphost, username, password);
- ts.sendMessage(message, message.getAllRecipients());
- ts.close();
- return this;
- }
- /**
- * 设置邮件标题
- * @param subject
- * @return
- * @throws Exception
- */
- public Mails subject(String subject) throws Exception {
- message = new MimeMessage(session);
- message.setFrom(new InternetAddress(fromEmail));
- message.setRecipients(Message.RecipientType.TO, getRecipient());
- message.setSubject(subject);
- message.setContent(new MimeMultipart("mixed"));
- return this;
- }
- /**
- * 添加邮件体,支持 HTML 标签
- * @param bodyContent
- * @return
- * @throws Exception
- */
- public Mails body(String bodyContent) throws Exception {
- MimeBodyPart text = new MimeBodyPart();
- text.setContent(bodyContent, "text/html;charset=UTF-8");
- ((MimeMultipart)message.getContent()).addBodyPart(text);
- return this;
- }
- /**
- * 添加附件
- * @param name 附件名称
- * @param type 附件 MIME 值
- * @param bytes 附件体
- * @return
- * @throws Exception
- */
- public Mails attachment(String name, String type, byte[] bytes) throws Exception {
- MimeBodyPart attach = new MimeBodyPart();
- DataHandler dh = new DataHandler(new ByteArrayDataSource(bytes, type));
- attach.setDataHandler(dh);
- attach.setFileName(MimeUtility.encodeWord(name));
- ((MimeMultipart)message.getContent()).addBodyPart(attach);
- return this;
- }
- private static Address[] getRecipient() throws Exception {
- InternetAddress[] result = new InternetAddress[receiveMails.length];
- for(int i = 0; i < receiveMails.length; i ++)
- result[i] = new InternetAddress(receiveMails[i]);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement