1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| package com.panshi.utils;
import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Date; import java.util.Properties;
public class SendEmail {
public static final String SMTPSERVER = "smtp.163.com"; public static final String SMTPPORT = "465"; public static final String ACCOUT = "xxx@163.com"; public static final String PWD = "密码";
public static void sendVerification(String address, String code){ Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", SMTPSERVER); props.setProperty("mail.smtp.port", SMTPPORT); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.ssl.enable", "true");
Session session = Session.getDefaultInstance(props); MimeMessage message = new MimeMessage(session);; try { InternetAddress fromAddress = new InternetAddress(ACCOUT,"小尾巴96", "utf-8"); message.setFrom(fromAddress); InternetAddress receiveAddress = new InternetAddress("xxx@qq.com", "VinhoJiang", "utf-8"); message.setRecipient(Message.RecipientType.TO, receiveAddress); message.setSubject("验证码", "utf-8"); String htmlText = "您的验证码是<H2 style='color:pink'>"+code+"</H2>"; message.setContent(htmlText, "text/html;charset=UTF-8"); message.setSentDate(new Date()); message.saveChanges(); } catch (Exception e) { e.printStackTrace(); } Transport transport = null; try { transport = session.getTransport(); } catch (NoSuchProviderException e) { e.printStackTrace(); } try { transport.connect(SMTPSERVER,ACCOUT, PWD); } catch (MessagingException e) { e.printStackTrace(); } try { transport.sendMessage(message, message.getAllRecipients()); } catch (MessagingException e) { e.printStackTrace(); } finally { try { transport.close(); } catch (MessagingException e) { e.printStackTrace(); } }
}
}
|