首先maven:
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.1</version> </dependency>
用戶名密碼驗證:
1 public class MailAuthenticator extends Authenticator{ 2 3 //郵箱賬號 4 private String username; 5 6 //郵箱密碼 7 private String password; 8 9 public MailAuthenticator(String username,String password){ 10 this.username=username; 11 this.password=password; 12 } 13 14 @Override 15 protected PasswordAuthentication getPasswordAuthentication() { 16 return new PasswordAuthentication(username, password); 17 } 18 19 public String getUsername() { 20 return username; 21 } 22 public void setUsername(String username) { 23 this.username = username; 24 } 25 public String getPassword() { 26 return password; 27 } 28 public void setPassword(String password) { 29 this.password = password; 30 } 31 }
1 public class SimpleMailSender { 2 private final transient Properties props = System.getProperties(); 3 private transient MailAuthenticator authenticator; 4 private transient Session session; 5 6 public SimpleMailSender(final String smtpHostName, final String username, 7 final String password){ 8 try { 9 init(username, password, smtpHostName); 10 } catch (NoSuchProviderException e) { 11 // TODO Auto-generated catch block 12 e.printStackTrace(); 13 } 14 } 15 public SimpleMailSender(final String username, final String password) throws NoSuchProviderException { 16 //通過郵箱地址解析出smtp服務器,對大多數郵箱都管用 17 final String smtpHostName = "smtp." + username.split("@")[1]; 18 init(username, password, smtpHostName); 19 20 } 21 private void init(String username, String password, String smtpHostName) throws NoSuchProviderException { 22 // 初始化props 23 props.put("mail.transport.protocol", "smtp"); 24 props.put("mail.smtp.auth", "true"); 25 //qq是smtp.qq.com 26 props.put("mail.smtp.host", smtpHostName); 27 //ssl郵箱端口 28 props.put("mail.smtp.socketFactory.port", 465);//465 29 //開啟ssl 30 props.put("mail.smtp.starttls.enable","true"); 31 // 驗證 32 authenticator = new MailAuthenticator(username, password); 33 // 創建session 34 session = Session.getInstance(props, authenticator); 35 session.setDebug(true); 36 // Transport transport = session.getTransport(); 37 // try { 38 // transport.connect("smtp.qq.com", 25, "[email protected]", "llg9004_d"); 39 // } catch (MessagingException e) { 40 // // TODO Auto-generated catch block 41 // e.printStackTrace(); 42 // } 43 } 44 public void send(String recipient, String subject, Object content) 45 throws AddressException, MessagingException { 46 // 創建mime類型郵件 47 final MimeMessage message = new MimeMessage(session); 48 // 設置發信人 49 message.setFrom(new InternetAddress(authenticator.getUsername())); 50 // 設置收件人 51 message.setRecipient(RecipientType.TO, new InternetAddress(recipient)); 52 // 設置主題 53 message.setSubject(subject); 54 // 設置郵件內容 55 System.out.println(); 56 message.setContent(content.toString(), "text/html;charset=utf-8"); 57 // 發送 58 Transport.send(message); 59 } 60 // public void send(String recipient, SimpleMail mail) 61 // throws AddressException, MessagingException { 62 // send(recipient, mail.getSubject(), mail.getContent()); 63 // } 64 65 /** 66 * 1.重點現在客戶端掉http端口25,騰訊好像不讓掉了,必須https調用 67 * 2.騰訊使用這種方式連接的話,需要重新申請獨立密碼,不是qq郵箱的密碼,還需開啟pop/smtp功能 68 * 3.用騰訊的郵局發郵件好像有限制,發送過多,好像直接給你連接斷開了 69 * 4.如果需要做發郵件的功能的話,最好自己搭建郵局 70 * @param args 71 * @throws AddressException 72 * @throws MessagingException 73 */ 74 public static void main(String[] args) throws AddressException, MessagingException { 75 SimpleMailSender sms= new SimpleMailSender("[email protected]", 76 "dfvasdasdasd"); 77 //發送過多的話會斷開連接 78 // for(int i=0;i<100;i++){ 79 sms.send("[email protected]", "hello", "hello"); 80 // System.out.println("#######:"+i); 81 // } 82 } 83 }