java發送qq郵件
阿新 • • 發佈:2017-05-07
java.net 技術分享 div nal todo auto style not page
1.需要一個額外的jar::https://java.net/projects/javamail/pages/Home
下載javax.mail.jar包
2.對發送的賬號開啟SMTP------並獲取授權碼----
3.這段代碼已通過:----我的授權改了,不要用
1 /** 2 * 3 */ 4 package com.breaver.bean; 5 6 import java.util.Properties; 7 8 import javax.mail.Message; 9 import javax.mail.Message.RecipientType; 10 importjavax.mail.MessagingException; 11 import javax.mail.Session; 12 import javax.mail.Transport; 13 import javax.mail.internet.AddressException; 14 import javax.mail.internet.InternetAddress; 15 import javax.mail.internet.MimeMessage; 16 17 import com.sun.org.glassfish.external.probe.provider.annotations.ProbeProvider;18 19 /** 20 * @author zzf 21 * 22 */ 23 public class Sendmail1 { 24 25 /** 26 * @param args 27 */ 28 public static void main(String[] args)throws AddressException,MessagingException { 29 // TODO Auto-generated method stub 30 Properties properties = new Properties();31 properties.put("mail.transport.protocol", "smtp"); 32 properties.put("mail.smtp.host", "smtp.qq.com"); 33 properties.put("mail.smtp.port", "465"); 34 properties.put("mail.smtp.auth", "true"); 35 properties.put("mail.smtp.ssl.enable", "true"); 36 properties.put("mail.debug", "true"); 37 38 Session session = Session.getInstance(properties); 39 Message message = new MimeMessage(session); 40 message.setFrom(new InternetAddress("[email protected]")); 41 message.setRecipients(RecipientType.TO, new InternetAddress[]{ 42 new InternetAddress("[email protected]")}); 43 message.setSubject("title"); 44 message.setText("hello world"); 45 Transport transport = session.getTransport(); 46 transport.connect("[email protected]", "asassasasas"); 47 transport.sendMessage(message,message.getAllRecipients()); 48 } 49 50 }
java發送qq郵件