标签:style blog http color java os io ar
以gmail为例,注意:
1 package com.mkyong.common; 2 3 import java.util.Properties; 4 5 import javax.mail.Message; 6 import javax.mail.MessagingException; 7 import javax.mail.PasswordAuthentication; 8 import javax.mail.Session; 9 import javax.mail.Transport; 10 import javax.mail.internet.InternetAddress; 11 import javax.mail.internet.MimeMessage; 12 13 public class SendMailTLS { 14 15 public static void main(String[] args) { 16 17 final String username = "username@gmail.com"; 18 final String password = "password"; 19 20 Properties props = new Properties(); 21 props.put("mail.smtp.auth", "true"); 22 props.put("mail.smtp.starttls.enable", "true"); 23 props.put("mail.smtp.host", "smtp.gmail.com"); 24 props.put("mail.smtp.port", "587"); 25 26 Session session = Session.getInstance(props, 27 new javax.mail.Authenticator() { 28 protected PasswordAuthentication getPasswordAuthentication() { 29 return new PasswordAuthentication(username, password); 30 } 31 }); 32 33 try { 34 35 Message message = new MimeMessage(session); 36 message.setFrom(new InternetAddress("from-email@gmail.com")); 37 message.setRecipients(Message.RecipientType.TO, 38 InternetAddress.parse("to-email@gmail.com")); 39 message.setSubject("Testing Subject"); 40 message.setText("Dear Mail Crawler," 41 + "\n\n No spam to my email, please!"); 42 43 Transport.send(message); 44 45 System.out.println("Done"); 46 47 } catch (MessagingException e) { 48 throw new RuntimeException(e); 49 } 50 } 51 }
1 package com.mkyong.common; 2 3 import java.util.Properties; 4 import javax.mail.Message; 5 import javax.mail.MessagingException; 6 import javax.mail.PasswordAuthentication; 7 import javax.mail.Session; 8 import javax.mail.Transport; 9 import javax.mail.internet.InternetAddress; 10 import javax.mail.internet.MimeMessage; 11 12 public class SendMailSSL { 13 public static void main(String[] args) { 14 Properties props = new Properties(); 15 props.put("mail.smtp.host", "smtp.gmail.com"); 16 props.put("mail.smtp.socketFactory.port", "465"); 17 props.put("mail.smtp.socketFactory.class", 18 "javax.net.ssl.SSLSocketFactory"); 19 props.put("mail.smtp.auth", "true"); 20 props.put("mail.smtp.port", "465"); 21 22 Session session = Session.getDefaultInstance(props, 23 new javax.mail.Authenticator() { 24 protected PasswordAuthentication getPasswordAuthentication() { 25 return new PasswordAuthentication("username","password"); 26 } 27 }); 28 29 try { 30 31 Message message = new MimeMessage(session); 32 message.setFrom(new InternetAddress("from@no-spam.com")); 33 message.setRecipients(Message.RecipientType.TO, 34 InternetAddress.parse("to@no-spam.com")); 35 message.setSubject("Testing Subject"); 36 message.setText("Dear Mail Crawler," + 37 "\n\n No spam to my email, please!"); 38 39 Transport.send(message); 40 41 System.out.println("Done"); 42 43 } catch (MessagingException e) { 44 throw new RuntimeException(e); 45 } 46 } 47 }
java mail 邮件发送实例【搬】,布布扣,bubuko.com
标签:style blog http color java os io ar
原文地址:http://www.cnblogs.com/ronguo/p/3918419.html