1.依赖
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
2.实现
import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.util.Properties;public class Message { private static String mailFrom = null;// 指明邮件的发件人 private static String password_mailFrom = null;// 指明邮件的发件人登陆密码 private static String mail_host =null; // 邮件的服务器域名 public static void sendMail(String[] mailInfo) throws Exception { String mailTo = mailInfo[0]; String mailTittle = mailInfo[1]; String mailText = mailInfo[2]; mailFrom = "cloud@huawei.com"; password_mailFrom="123456"; mail_host="smtp.exmail.qq.com"; Properties prop = new Properties(); prop.setProperty("mail.host", mail_host); prop.setProperty("mail.transport.protocol", "smtp"); prop.setProperty("mail.smtp.port", "465"); prop.setProperty("mail.smtp.auth", "true"); prop.setProperty("mail.smtp.ssl.enable", "true"); // 1、创建session Session session = Session.getInstance(prop); // 开启Session的debug模式,这样就可以查看到程序发送Email的运行状态 //session.setDebug(true); // 2、通过session得到transport对象 Transport ts = session.getTransport(); // 3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。 ts.connect(mail_host,mailFrom, password_mailFrom); // 4、创建邮件 javax.mail.Message message = createSimpleMail(session,mailFrom,mailTo,mailTittle,mailText); // 5、发送邮件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); } /** * @Method: createSimpleMail * @Description: 创建一封只包含文本的邮件 */ public static MimeMessage createSimpleMail(Session session, String mailfrom, String mailTo, String mailTittle, String mailText) throws Exception { // 创建邮件对象 MimeMessage message = new MimeMessage(session); // 指明邮件的发件人 message.setFrom(new InternetAddress(mailfrom)); // 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发 message.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(mailTo)); // 邮件的标题 message.setSubject(mailTittle); // 邮件的文本内容 message.setContent(mailText, "text/plain;charset=UTF-8"); // 返回创建好的邮件对象 return message; }}
3.案例
Message.sendMail(args)val args = Array("cloud@huawei.com", "spark任务监控", "exception")