Skip to content

Instantly share code, notes, and snippets.

@lzxz1234
Last active December 2, 2016 06:26
Show Gist options
  • Save lzxz1234/1372e5489887c0094b38fbcc66df589d to your computer and use it in GitHub Desktop.
Save lzxz1234/1372e5489887c0094b38fbcc66df589d to your computer and use it in GitHub Desktop.
Mails
package com.bj58.hrg.creative.pingjia.web.utils;
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 = "qq.com";
private static final String smtphost = "smtp.qq.com";
private static final String username = "cubieboard2";
private static final String password = "1qaz@WSX";
private static final String fromEmail = username + "@" + host;
private String[] receiveMails = new String[] {"pangzhengzheng@58.com"};
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;
}
/**
* 设置收件人邮箱
* @param receivers
* @return
* @throws Exception
*/
public Mails receiver(String[] receivers) throws Exception {
this.receiveMails = receivers;
return this;
}
/**
* 执行发送操作
* @return
* @throws Exception
*/
public Mails send() throws Exception {
message.saveChanges();
message.setRecipients(Message.RecipientType.TO, getRecipient());
Transport ts = session.getTransport();
ts.connect(smtphost, username, password);
ts.sendMessage(message, getRecipient());
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.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 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment