首先是类
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; /** * @Author: zhangshulong * @Description: * @Date: Created in 13:45 2016/11/30 */ public class MailUtil { private static final Logger LOGGER = LoggerFactory.getLogger(MailUtil.class); private static String mailSmtpServer; private static String mailFrom; private static String mailPassword; private static MimeMessage message; private static Session session; private static Properties properties = new Properties(); /** * 发送邮件 */ public static ExecuteResult<String> sendEmail(String subject, String sendHtml, String receiveUser) { ExecuteResult<String> excute = new ExecuteResult<String>(); try { mailFrom = Env.getProperty(Env.SEND_FORM); mailSmtpServer = Env.getProperty(Env.SMTP_SERVER); mailPassword = Env.getProperty(Env.MAIL_PASSWARD); session = Session.getInstance(properties); message = new MimeMessage(session); doSendHtmlEmail(subject, sendHtml, receiveUser); } catch (Exception e) { LOGGER.error("发送邮件失败!"); excute.addErrorMessage("发送邮件失败"); excute.setResult("发送邮件失败"); return excute; } excute.setResult("发送邮件成功"); return excute; } /** * 发送邮件 * * @param subject 邮件主题 * @param htmlContent 邮件内容 * @param receiveUser 收件人地址 */ private static void doSendHtmlEmail(String subject, String htmlContent, String receiveUser) throws javax.mail.MessagingException { try { Properties props = new Properties(); // 开启debug调试 props.setProperty("mail.debug", "true"); // 发送服务器需要身份验证 props.setProperty("mail.smtp.auth", "true"); // 设置邮件服务器主机名 props.setProperty("mail.host", mailSmtpServer); // 发送邮件协议名称 props.setProperty("mail.transport.protocol", "smtp"); // 设置环境信息 Session localSession = Session.getInstance(props, new javax.mail.Authenticator() { // 在session中设置账户信息,Transport发送邮件时会使用 protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(mailFrom, mailPassword); } }); // 创建邮件对象 Message msg = new MimeMessage(localSession); // 发件人 msg.setFrom(new InternetAddress(mailFrom)); // 多个收件人 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiveUser)); // 抄送人 //msg.setRecipient(Message.RecipientType.CC, new InternetAddress("java_mail_001@163.com")); // 暗送人 //msg.setRecipient(Message.RecipientType.BCC, new InternetAddress("java_mail_004@163.com")); // 主题 msg.setSubject(subject); // HTML内容 msg.setContent(htmlContent, "text/html;charset=utf-8"); Transport.send(msg); } catch (Exception e) { e.printStackTrace(); } } }
然后 可能会碰到如下情况
javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
这是由于jar包冲突造成的
如果你是maven,则只要添加下面代码就行就行
首先是忽略这两个jar包(exclusion就是忽略jar包)
<exclusion> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-javamail_1.4_spec</artifactId> </exclusion> <exclusion> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-activation_1.1_spec</artifactId> </exclusion>然后引入这两个jar包
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.1</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.0.2</version> </dependency>