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 }
辛勤的耕种,才可以得到丰收!
原文地址:https://www.cnblogs.com/yearHeaven/p/6741437.html