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
| public static void main(String[] args) throws Exception {
final Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.sina.com"); props.put("mail.user", "xxxx@sina.com"); props.put("mail.password", "9a5fe076b8837621");
Authenticator authenticator = new Authenticator() {
@Override protected PasswordAuthentication getPasswordAuthentication() { String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; Session mailSession = Session.getInstance(props, authenticator); MimeMessage message = new MimeMessage(mailSession); InternetAddress form = new InternetAddress(props.getProperty("mail.user")); message.setFrom(form);
InternetAddress to = new InternetAddress("xxxx@qq.com"); message.setRecipient(Message.RecipientType.TO, to);
message.setSubject("测试邮件");
message.setContent("这是一封测试邮件", "text/html;charset=UTF-8");
Transport.send(message); System.out.println("发送结束"); }
|