首页 > 技术文章 > java发送qq邮件

zeigongzi 2017-05-07 16:01 原文

来源:qq zone

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 import javax.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("1147057783@qq.com"));
41         message.setRecipients(RecipientType.TO, new InternetAddress[]{
42                 new InternetAddress("3453539748@qq.com")});
43         message.setSubject("title");
44         message.setText("hello world");
45         Transport transport = session.getTransport();
46         transport.connect("1147057783@qq.com", "asassasasas");
47         transport.sendMessage(message,message.getAllRecipients());
48     }
49 
50 }

 

推荐阅读