JavaMail发送邮件Demo
Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- lombok 用于简化JavaBean结构 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
JavaMail工具类
public class JavaMailUtil {
public static String MYEMAILACCOUNT = "自己的邮箱";
public static String MYEMAILPASSWORD = "uvgjctgmnfhziehe";
public static String MYEMAILSMTPHOST = "smtp.qq.com";
public static String RECEIVEMAILACCOUNT = "写自己的邮箱";
public static Properties properties = new Properties();
public static Session session;
public static Transport transport;
public static void setProperties() throws NoSuchProviderException, MessagingException {
properties.setProperty("mail.transport.protocol","smtp");
properties.setProperty("mail.smtp.host",MYEMAILSMTPHOST);
properties.setProperty("mail.smtp.auth","true");
session = Session.getInstance(properties);
session.setDebug(true);
transport = session.getTransport();
transport.connect(MYEMAILACCOUNT,MYEMAILPASSWORD);
}
public static MimeMessage getMimeMessage(MailBean mailBean) throws UnsupportedEncodingException,MessagingException{
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(MYEMAILACCOUNT,"迷人的猪肉佬","utf-8"));
mimeMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(mailBean.getReceiveMail(),mailBean.getReceiverName(),"utf-8"));
mimeMessage.setSubject(mailBean.getSuject(),"utf-8");
return mimeMessage;
}
public static void sendSimpleMail(MailBean mailBean) throws MessagingException, UnsupportedEncodingException {
MimeMessage mimeMessage = getMimeMessage(mailBean);
mimeMessage.setText(mailBean.getText());
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
transport.close();
}
public static void sendHtmlMail(MailBean mailBean) throws UnsupportedEncodingException,MessagingException{
MimeMessage mimeMessage = getMimeMessage(mailBean);
mimeMessage.setText(mailBean.getText(),"utf-8","html");
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
transport.close();
}
public static void sendAttchmentMail(MailBean mailBean,String url) throws UnsupportedEncodingException,MessagingException{
MimeMessage mimeMessage = getMimeMessage(mailBean);
MimeBodyPart attachment = new MimeBodyPart();
DataHandler handler = new DataHandler(new FileDataSource(url));
attachment.setDataHandler(handler);
attachment.setFileName(MimeUtility.encodeText(handler.getName()));
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(attachment);
mimeMessage.setContent(multipart);
mimeMessage.setSentDate(new Date());
mimeMessage.saveChanges();
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
transport.close();
}
public static void sendAttchmentImageMail(MailBean mailBean,String imagePath,String filePath) throws MessagingException,UnsupportedEncodingException{
MimeMessage mimeMessage = getMimeMessage(mailBean);
mimeMessage.setText(mailBean.getText());
MimeBodyPart image = new MimeBodyPart();
DataHandler dataHandler = new DataHandler(new FileDataSource(imagePath));
image.setDataHandler(dataHandler);
image.setContentID("image_test");
MimeBodyPart text = new MimeBodyPart();
text.setContent("这是一个图片<br/><img src='cid:image_test'/>","text/html;charset=UTF-8");
MimeMultipart text_image = new MimeMultipart();
text_image.addBodyPart(text);
text_image.addBodyPart(image);
text_image.setSubType("related");
MimeBodyPart textImage = new MimeBodyPart();
textImage.setContent(text_image);
MimeBodyPart attachment = new MimeBodyPart();
DataHandler handler = new DataHandler(new FileDataSource(filePath));
attachment.setDataHandler(handler);
attachment.setFileName(MimeUtility.encodeText(handler.getName()));
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(textImage);
multipart.addBodyPart(attachment);
multipart.setSubType("mixed");
mimeMessage.setContent(multipart);
mimeMessage.setSentDate(new Date());
mimeMessage.saveChanges();
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
transport.close();
}
}
封装好的MailBean
@Data
public class MailBean {
private String suject;
private String text;
private FileSystemResource file;
private String attachmentname;
private String contentId;
private String receiveMail;
private String receiverName;
public static MailBean getMaileBean(){
return new MailBean();
}
}
测试类
public class JavaMailApp {
public static void main(String[] args) throws NoSuchProviderException, MessagingException, UnsupportedEncodingException {
JavaMailUtil.setProperties();
MailBean mailBean = new MailBean();
mailBean.setReceiveMail("收件人邮箱");
mailBean.setReceiverName("唏嘘的猪肉佬");
mailBean.setText("测试文本");
mailBean.setSuject("测试邮件");
System.out.println("-----发送简单邮件开始-----");
JavaMailUtil.sendSimpleMail(mailBean);
System.out.println("-----发送简单邮件结束-----");
System.out.println("-----发送Html邮件开始-----");
JavaMailUtil.sendHtmlMail(mailBean);
System.out.println("-----发送Html邮件结束-----");
System.out.println("-----发送附件邮件开始-----");
JavaMailUtil.sendAttchmentMail(mailBean,"E:\\私人保存\\huan\\DSC_0099.jpg");
System.out.println("-----发送附件邮件结束");
}
}