当前位置: 移动技术网 > IT编程>开发语言>Java > Java 邮件发送

Java 邮件发送

2019年05月08日  | 移动技术网IT编程  | 我要评论
先导入邮件的jar 包 mail.jar424.6 KB mail.jar424.6 KB 1 package com.project.c3p0demo32.tools; 2 3 import javax.mail.Session; 4 import javax.mail.internet.Inte ...
先导入邮件的jar 包
 1 package com.project.c3p0demo32.tools;
 2  
 3 import javax.mail.session;
 4 import javax.mail.internet.internetaddress;
 5 import javax.mail.internet.mimemessage;
 6 import javax.mail.internet.mimemessage.recipienttype;
 7 import java.util.properties;
 8 import javax.mail.message;
 9 import javax.mail.messagingexception;
10 import javax.mail.transport;
11 import javax.mail.internet.addressexception;
12  
13 public class sendmail {
14     //发送的邮箱地址(该邮件由谁发送)
15     public final static string sender ="";
16     //钥匙,有了钥匙邮件系统才能知道你是有权限用该邮箱发送邮件的
17     public final static string key="";
18     public static void main(string[] args){       
19         try {
20             new sendmail().send("", "邮件测试", "你已经成功购买了girlcloset中的商品!");
21         } catch (addressexception e) {
22             e.printstacktrace();
23         } catch (messagingexception e) {
24             e.printstacktrace();
25         }
26  
27     }
28     /**
29      *
30      * @param receiver 邮件接收方
31      * @param title 邮件标题
32      * @param content 邮件正文
33      * @throws addressexception
34      * @throws messagingexception
35      */
36     public void send(string receiver, string title, string content) throws addressexception,messagingexception {
37         properties properties = new properties();
38  
39         properties.put("mail.transport.protocol", "smtp");// 连接协议        
40  
41         properties.put("mail.smtp.host", "smtp.qq.com");// 主机名        
42  
43         properties.put("mail.smtp.port", 465);// 端口号        
44  
45         properties.put("mail.smtp.auth", "true");        
46  
47         properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接  ---一般都使用        
48  
49         properties.put("mail.debug", "true");//设置是否显示debug信息  true 会在控制台显示相关信息        
50  
51         //得到回话对象        
52  
53         session session = session.getinstance(properties);        
54  
55         // 获取邮件对象        
56  
57         message message = new mimemessage(session);        
58  
59         //设置发件人邮箱地址       
60  
61         message.setfrom(new internetaddress(sender));       
62  
63         //设置收件人地址       
64         message.setrecipients(recipienttype.to,new internetaddress[] { new internetaddress(receiver) });       
65  
66         //设置邮件标题        
67  
68         message.setsubject(title);        
69  
70         //设置邮件内容        
71  
72         message.settext(content);       
73  
74         //得到邮差对象        
75  
76         transport transport = session.gettransport();        
77  
78         //连接自己的邮箱账户        
79  
80         transport.connect(sender, key);//密码为刚才得到的授权码        
81  
82         //发送邮件        
83         transport.sendmessage(message, message.getallrecipients());
84     }
85  
86 }

 

 
 
 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网