JavaMail技術實現郵件發送轉【】

 1 1.導入2個jar包,mail.jar,activation.jar
 2 2.導入的jar包與myeclipse中自帶的javaee 中的javaee.jar中的javax.activation包及javax.mail衝突,
 3 解決辦法以下:
 4 在myeclipse中,點擊window-preference-搜索框中輸入lib,選中Library Sets,在右側選擇Javaee.jar-Add JAR/ZIP,而後選擇用壓縮程序打開,選擇要移除的包,點擊壓縮程序上方的刪除便可!
 5 代碼以下:
 6 package test;
 7 
 8 
 9 import javax.mail.Message;
10 import javax.mail.MessagingException;
11 import javax.mail.PasswordAuthentication;
12 import javax.mail.Session;
13 import javax.mail.Transport;
14 import javax.mail.internet.AddressException;
15 import javax.mail.internet.InternetAddress;
16 import javax.mail.internet.MimeMessage;
17 import javax.mail.internet.MimeMessage.RecipientType;
18 
19 
20 import javax.mail.Authenticator;
21 import java.util.Properties;
22 /**
23  * 發送郵件
24  * @author admin
25  *
26  */
27 public class JavaMail
28 {
29 public static void main(String[] args) throws AddressException, MessagingException
30 { 
31 Properties props = new Properties();
32 //須要爲props設置發送的主機和是否須要認證
33 // props.setProperty("mail.host", "localhost");//鏈接的服務器
34 props.setProperty("mail.host", "smtp.163.com");//鏈接的服務器
35 props.setProperty("mail.smtp.auth", "true");//是否須要認證
36 
37 
38 
39 
40 //建立一個對象Session
41 Session session = Session.getInstance(props, new Authenticator()
42 {
43 
44 
45 @Override
46 protected PasswordAuthentication getPasswordAuthentication()
47 {
48 return new PasswordAuthentication("yuanhenglizhen110@163.com", "wy5776402287");
49 
50 
51 }
52 
53 });
54 
55 //建立一個郵件的對象
56 Message message = new MimeMessage(session);
57 
58 //設置發件人
59 message.setFrom(new InternetAddress("yuanhenglizhen110@163.com"));
60 //設置收件人
61 message.setRecipient(RecipientType.TO, new InternetAddress("995937121@qq.com"));
62 //設置郵件的主題
63 message.setSubject("一封激活郵件");
64 //設置郵件的正文
65 
66 // message.setContent("激活郵件!", "text/plan");
67 message.setContent("<a href='http://www.baidu.com'>xxx,這是一封激活郵件!</a>", "text/html;charset=UTF-8");//加入超連接
68 //發送
69 Transport.send(message);
70 
71 
72 }
73 }
相關文章
相關標籤/搜索