赞
踩
一、使用javax.mail传统方式。需引入mail-1.4.jar包。这是用java自带的类库实现,但是实现起来比较麻烦。
二、使用Apache Commons Mail方式。需要引入commons-email-1.2.jar包。用MultiPartEmail对象,设置完邮箱信息和EmailAttachment附件对象后,使用send方法发送。
- //附件,可以定义多个附件对象
- EmailAttachment attachment = new EmailAttachment();
- attachment.setPath("C:\\123.txt");
- attachment.setDisposition(EmailAttachment.ATTACHMENT);
- attachment.setDescription("这是附件描述");
- MultiPartEmail email = new MultiPartEmail();
-
- email.setHostName("smtp.163.com"); //设置发送主机的服务器地址
- email.addTo("xxx"); //设置收件人邮箱
- email.setFrom("xxx"); //发件人邮箱
- email.setAuthentication("用户名", "密码");//设置发件人在邮件服务器上注册的用户名和密码
- email.setCharset("UTF-8"); //设置主题的字符集
- email.setSubject("你好, 这是邮件主题"); //设置邮件的主题
- email.setMsg("这是邮件正文部分"); //邮件正文内容
- email.attach(attachment); //添加附件
- email.send();
三、使用spring框架方式。需引入spring-context-support-4.3.9.jar包。使用MimeMessageHelper对象发送。
- JavaMailSender javaMailSender = new JavaMailSenderImpl();
-
- MimeMessage mailMessage = javaMailSender.createMimeMessage();
- MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "UTF-8");
-
- messageHelper.setTo(tos);//邮件接收人
- messageHelper.setFrom(text);//邮件内容
- messageHelper.setSubject(subject);//主题
- messageHelper.setText(text, true);// true 表示启动HTML格式的邮件
-
- FileSystemResource file = new FileSystemResource(new File("c://123.txt"));
- messageHelper.addAttachment(AttachName, file);// 发送邮件
- javaMailSender.send(mailMessage);
配置类:
- package cn.com.maxntech.email;
-
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
-
- @Component("mailConfig")
- @ConfigurationProperties(prefix = "application.email")
- public class MailConfig {
- private String host;
- private String username;
- private String password;
- private String auth;
- //省略get set
- }
工具类:
- package cn.com.maxntech.hu.utils;
-
- import java.io.File;
-
- import javax.mail.MessagingException;
- import javax.mail.internet.MimeMessage;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.io.FileSystemResource;
- import org.springframework.mail.javamail.JavaMailSender;
- import org.springframework.mail.javamail.MimeMessageHelper;
- import org.springframework.stereotype.Component;
-
- import cn.com.maxtech.email.MailConfig;
-
- /**
- * 邮件发送工具类
- * */
- @Component
- public class SendMailUtils {
-
- @Autowired
- private MailConfig mailConfig;
- @Autowired
- private JavaMailSender javaMailSender;
-
- /**
- * 发送带html格式的邮件
- * @param tos
- * @param subject
- * @param text
- * @throws Exception
- */
- public void sendTextWithHtml(String[] tos, String subject, String text)
- throws Exception {
- MimeMessage mailMessage = javaMailSender.createMimeMessage();
- initMimeMessageHelper(mailMessage, tos, mailConfig.getUsername(), subject, text);
- // 发送邮件
- javaMailSender.send(mailMessage);
- }
-
- /**
- * 发送带图片的邮件
- * @param tos
- * @param subject
- * @param text
- * @param imgId
- * @param imgPath
- * @throws MessagingException
- */
- public void sendTextWithImg(String[] tos, String subject, String text,
- String imgId, String imgPath) throws MessagingException {
- MimeMessage mailMessage = javaMailSender.createMimeMessage();
- MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage,tos,mailConfig.getUsername(),subject, text,true, true, "UTF-8");
- // 发送图片
- FileSystemResource img = new FileSystemResource(new File(imgPath));
- messageHelper.addInline(imgId, img);
- // 发送邮件
- javaMailSender.send(mailMessage);
- }
-
- /**
- * 发送带附件的邮件
- * @param tos
- * @param subject
- * @param text
- * @param AttachName
- * @param filePath
- * @throws MessagingException
- */
- public void sendWithAttament(String[] tos, String subject, String text,
- String AttachName, String filePath) throws MessagingException {
- MimeMessage mailMessage = javaMailSender.createMimeMessage();
- MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, mailConfig.getUsername(), subject, text,
- true, true, "UTF-8");
- FileSystemResource file = new FileSystemResource(new File(filePath));
- // 发送邮件
- messageHelper.addAttachment(AttachName, file);
- javaMailSender.send(mailMessage);
- }
-
- /**
- * 发送带图片和附件的邮件
- * @param tos
- * @param from
- * @param subject
- * @param text
- * @param imgId
- * @param imgPath
- * @param AttachName
- * @param filePath
- * @throws MessagingException
- */
- public void sendWithAll(String[] tos, String from, String subject, String text,
- String imgId, String imgPath, String AttachName, String filePath) throws MessagingException {
- MimeMessage mailMessage = javaMailSender.createMimeMessage();
- MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, mailConfig.getUsername(), subject, text,
- true, true, "UTF-8");
- // 插入图片
- FileSystemResource img = new FileSystemResource(new File(imgPath));
- messageHelper.addInline(imgId, img);
- // 插入附件
- FileSystemResource file = new FileSystemResource(new File(filePath));
- messageHelper.addAttachment(AttachName, file);
- // 发送邮件
- javaMailSender.send(mailMessage);
- }
-
- /**
- * 初始化helper
- * @param mailMessage
- * @param tos
- * @param from
- * @param subject
- * @param text
- * @return
- * @throws MessagingException
- */
- private MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,
- String subject, String text) throws MessagingException {
- return initMimeMessageHelper(mailMessage, tos, from, subject, text, true, false, "UTF-8");
- }
-
- /**
- * 初始化helper
- * @param mailMessage
- * @param tos
- * @param from
- * @param subject
- * @param text
- * @param isHTML
- * @param multipart
- * @param encoding
- * @return
- * @throws MessagingException
- */
- private static MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,
- String subject, String text, boolean isHTML, boolean multipart, String encoding) throws MessagingException {
- MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, multipart, encoding);
- messageHelper.setTo(tos);
- messageHelper.setFrom(from);
- messageHelper.setSubject(subject);
- // true 表示启动HTML格式的邮件
- messageHelper.setText(text, isHTML);
- return messageHelper;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。