开发前要先下载mail.jar,如果用Myeclipse开发会有冲突,因myeclipse中自带有mail.jar,但不好用,解决办法:删除相关mail类,如我的删除方法是找到myeclipse安装目录D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.6.0.me201007292038\data\libraryset\EE_5\javaee.jar用7-zip压缩软件打开,删除下面的mail文件夹
邮箱用户名密码
1 package com.tg.email;
2
3 import javax.mail.Authenticator;
4 import javax.mail.PasswordAuthentication;
5
6 public class MailAuthenticator extends Authenticator {
7
8 public static String MAIL_USER = "所用Email地址,如cc@163.com";
9 public static String MAIL_PASSWORD = "123456"; //密码,不用说了吧
10 protected PasswordAuthentication getPasswordAuthentication() {
11 return new PasswordAuthentication(MAIL_USER, MAIL_PASSWORD);
12 }
13
14 }
邮件发送类
1 package com.tg.email;
2
3 import java.io.IOException;
4 import java.util.Date;
5 import java.util.Properties;
6 import javax.mail.AuthenticationFailedException;
7 import javax.mail.Authenticator;
8 import javax.mail.Message;
9 import javax.mail.MessagingException;
10 import javax.mail.Multipart;
11 import javax.mail.Session;
12 import javax.mail.Transport;
13 import javax.mail.internet.InternetAddress;
14 import javax.mail.internet.MimeBodyPart;
15 import javax.mail.internet.MimeMessage;
16 import javax.mail.internet.MimeMultipart;
17
18 public class SendMail {
19 private String mailTo = null; //邮件接收者
20 private String mailReply = null; //邮件回复地址
21 private String smtpHost = null; //SMTP服务器
22 private boolean debug = false;
23 private String subject; //邮件主题
24 private String msgContent; //邮件内容
25 private String messageContentMimeType = "text/html;charset=gb2312"; //邮件内容格式
26
27 /**
28 * 填充邮件相关信息Method
29 * @param session
30 * @param msg
31 * @throws IOException
32 * @throws MessagingException
33 */
34 private void fillMail(Session session, MimeMessage msg) throws IOException,
35 MessagingException {
36
37 Multipart mPart = new MimeMultipart(); //模拟信息所需邮件内容方法的参数
38 if (mailReply != null) {
39 msg.setFrom(new InternetAddress(mailReply));
40 System.out.println("发送人Mail地址:" + mailReply);
41 } else {
42 System.out.println("没有指定发送人邮件地址!");
43 return;
44 }
45 if (mailTo != null) {
46 InternetAddress[] address = InternetAddress.parse(mailTo);
47 msg.setRecipients(Message.RecipientType.TO, address);
48 } else {
49 System.out.println("没有指定收件人邮件地址!");
50 return;
51 }
52 msg.setSubject(subject);
53 InternetAddress[] replyAddress = { new InternetAddress(mailReply) };
54 msg.setReplyTo(replyAddress);
55
56 MimeBodyPart mBodyContent = new MimeBodyPart(); //设置内容编码和内容
57 if (msgContent != null) {
58 mBodyContent.setContent(msgContent, messageContentMimeType);
59 }else{
60 mBodyContent.setContent("", messageContentMimeType);
61 }
62 mPart.addBodyPart(mBodyContent);
63 msg.setContent(mPart);
64 msg.setSentDate(new Date());
65 }
66
67
68 @SuppressWarnings("static-access")
69 public int sendMail() throws IOException,MessagingException{
70
71 Properties props = System.getProperties(); //JavaMail需要Properties来创建一个session对象,其属性值就是发送邮件的主机
72 props.put("mail.smtp.host", smtpHost);
73 props.put("mail.smtp.auth", "true");
74
75 Authenticator auth= new MailAuthenticator();
76 Session session=Session.getInstance(props,auth); //创建session对象
77 session.setDebug(debug);
78 MimeMessage msg=new MimeMessage(session); //创建模拟邮件信息
79 Transport trans= null; //创建发送对象
80 fillMail(session, msg);
81
82 try {
83 trans=session.getTransport("smtp");
84 try {
85
86 trans.connect(smtpHost,MailAuthenticator.MAIL_USER, MailAuthenticator.MAIL_PASSWORD);
87
88 } catch (AuthenticationFailedException e) {
89 e.printStackTrace();
90 System.out.println("连接邮件服务器错误:");
91 return 3;
92 } catch (MessagingException e) {
93 e.printStackTrace();
94 System.out.println("连接邮件服务器错误:");
95 return 3;
96 }
97 trans.send(msg); //发送邮件
98 trans.close();
99 } catch (MessagingException e) {
100 System.out.println("发送邮件失败:");
101 e.printStackTrace();
102 }finally{
103 try {
104 if (trans!=null&&trans.isConnected()) {
105 trans.close();
106 }
107 } catch (Exception e) {
108 System.out.println(e.toString());
109 }
110 }
111 System.out.println("发送邮件成功!");
112 return 0;
113 }
114
115 public SendMail(String smtpHost,String replyAddress,String mailTo,String subject,String content){
116 this.smtpHost=smtpHost;
117 this.mailReply=replyAddress;
118 this.mailTo=mailTo;
119 this.subject=subject;
120 this.msgContent=content;
121 }
122
123 public SendMail(){}
124
125 /**
126 * main方法测试
127 * @param args
128 */
129 public static void main(String[] args) {
130 SendMail sm=new SendMail();
131 sm.setSmtpHost("smtp.163.com");
132 sm.setMailReply("tg_mail@163.com");
133 sm.setMailTo("471164275@qq.com");
134 sm.setMsgContent("测试邮件,请注意查收,如有问题,请联系开发人员!");
135 sm.setSubject("邮件标题");
136 try {
137 sm.sendMail();
138 } catch (IOException e) {
139 e.printStackTrace();
140 } catch (MessagingException e) {
141 e.printStackTrace();
142 }
143 }
144
145
146 public String getMailTo() {
147 return mailTo;
148 }
149
150 public void setMailTo(String mailTo) {
151 this.mailTo = mailTo;
152 }
153
154 public String getSmtpHost() {
155 return smtpHost;
156 }
157
158 public void setSmtpHost(String smtpHost) {
159 this.smtpHost = smtpHost;
160 }
161
162 public boolean isDebug() {
163 return debug;
164 }
165
166 public void setDebug(boolean debug) {
167 this.debug = debug;
168 }
169
170 public String getSubject() {
171 return subject;
172 }
173
174 public void setSubject(String subject) {
175 this.subject = subject;
176 }
177
178 public String getMsgContent() {
179 return msgContent;
180 }
181
182 public void setMsgContent(String msgContent) {
183 this.msgContent = msgContent;
184 }
185
186 public String getMessageContentMimeType() {
187 return messageContentMimeType;
188 }
189
190 public void setMessageContentMimeType(String messageContentMimeType) {
191 this.messageContentMimeType = messageContentMimeType;
192 }
193
194 public void setMailReply(String mailReply) {
195 this.mailReply = mailReply;
196 }
197
198 public String getMailReply() {
199 return mailReply;
200 }
201
202 }
203