1 package com.shihuc.demo;
2
3 import java.util.*;
4 import java.io.*;
5 import javax.mail.*;
6 import javax.mail.internet.*;
7 import javax.activation.*;
8
9 public class SendEmail
10 {
11 //收件人邮箱地址
12 private String to;
13 //发件人邮箱地址
14 private String from;
15 //SMTP服务器地址
16 private String smtpServer;
17 //登录SMTP服务器的用户名
18 private String username ;
19 //登录SMTP服务器的密码
20 private String password ;
21 //邮件主题
22 private String subject;
23 //邮件正文
24 private String content;
25 //记录所有附件文件的集合
26 List attachments = new ArrayList();
27 //无参数的构造器
28 public SendEmail()
29 {
30 }
31
32 public SendEmail(String to , String from , String smtpServer
33 , String username , String password
34 , String subject , String content)
35 {
36 this.to = to;
37 this.from = from;
38 this.smtpServer = smtpServer;
39 this.username = username;
40 this.password = password;
41 this.subject = subject;
42 this.cOntent= content;
43 }
44 //to属性的setter方法
45 public void setTo(String to)
46 {
47 this.to = to;
48 }
49 //from属性的setter方法
50 public void setFrom(String from)
51 {
52 this.from = from;
53 }
54 //smtpServer属性的setter方法
55 public void setSmtpServer(String smtpServer)
56 {
57 this.smtpServer = smtpServer;
58 }
59 //username属性的setter方法
60 public void setUsername(String username)
61 {
62 this.username = username;
63 }
64 //password属性的setter方法
65 public void setPassword(String password)
66 {
67 this.password = password;
68 }
69 //subject属性的setter方法
70 public void setSubject(String subject)
71 {
72 this.subject = subject;
73 }
74 //content属性的setter方法
75 public void setContent(String content)
76 {
77 this.cOntent= content;
78 }
79 //把邮件主题转换为中文
80 public String transferChinese(String strText)
81 {
82 try
83 {
84 strText = MimeUtility.encodeText(new String(strText.getBytes() , "GB2312"), "GB2312", "B");
85 }
86 catch(Exception e)
87 {
88 e.printStackTrace();
89 }
90 return strText;
91 }
92 //增加附件,将附件文件名添加的List集合中
93 public void attachfile(String fname)
94 {
95 attachments.add(fname);
96 }
97 //发送邮件
98 public boolean send()
99 {
100 //创建邮件Session所需的Properties对象
101 Properties props = new Properties();
102 props.put("mail.smtp.host" , smtpServer);
103 props.put("mail.smtp.auth" , "true");
104 props.put("mail.smtp.debug" , "true");
105 //创建Session对象
106 Session session = Session.getDefaultInstance(props
107 //以匿名内部类的形式创建登录服务器的认证对象
108 , new Authenticator()
109 {
110 public PasswordAuthentication getPasswordAuthentication()
111 {
112 return new PasswordAuthentication(username, password);
113 }
114 });
115 try
116 {
117 //构造MimeMessage并设置相关属性值
118 MimeMessage msg = new MimeMessage(session);
119 //设置发件人
120 msg.setFrom(new InternetAddress(from));
121 //设置收件人
122 InternetAddress[] addresses = {new InternetAddress(to)};
123 msg.setRecipients(Message.RecipientType.TO , addresses);
124 //设置邮件主题
125 subject = transferChinese(subject);
126 msg.setSubject(subject);
127 //构造Multipart
128 Multipart mp = new MimeMultipart();
129 //向Multipart添加正文
130 MimeBodyPart mbpCOntent= new MimeBodyPart();
131 mbpContent.setText(content);
132 //将BodyPart添加到MultiPart中
133 mp.addBodyPart(mbpContent);
134 //向Multipart添加附件
135 //遍历附件列表,将所有文件添加到邮件消息里
136 for(String efile : attachments)
137 {
138 MimeBodyPart mbpFile = new MimeBodyPart();
139 //以文件名创建FileDataSource对象
140 FileDataSource fds = new FileDataSource(efile);
141 //处理附件
142 mbpFile.setDataHandler(new DataHandler(fds));
143 mbpFile.setFileName(fds.getName());
144 //将BodyPart添加到MultiPart中
145 mp.addBodyPart(mbpFile);
146 }
147 //清空附件列表
148 attachments.clear();
149 //向Multipart添加MimeMessage
150 msg.setContent(mp);
151 //设置发送日期
152 msg.setSentDate(new Date());
153 //发送邮件
154 Transport.send(msg);
155 }
156 catch (MessagingException mex)
157 {
158 mex.printStackTrace();
159 return false;
160 }
161 return true;
162 }
163 public static void main(String[] args)
164 {
165 SendEmail sendMail = new SendEmail();
166 sendMail.setSmtpServer("smtp.163.com");
167 //此处设置登录的用户名
168 sendMail.setUsername("你自己合法的163邮箱");
169 //此处设置登录的密码,这个密码非常重要,它不是邮箱账户的登录密码,而是应用密码,需要登录邮箱,在设置中开启此应用
170 //否则很可能出现如下错误:
171 //javax.mail.AuthenticationFailedException: 550 User has no permission
172 sendMail.setPassword("你自己的应用密码");
173 //设置收件人的地址
174 sendMail.setTo("你希望将邮件发给的对方的邮件地址");
175 //设置发送人地址
176 sendMail.setFrom("你自己的合法的163邮箱");
177 //设置标题
178 sendMail.setSubject("测试邮件标题!");
179 //设置内容
180 sendMail.setContent("你好这是一个带多附件的测试邮件!");
181 //粘贴附件
182 sendMail.attachfile("/opt/attachment.jpg");
183 if (sendMail.send())
184 {
185 System.out.println("---邮件发送成功---");
186 }
187 }
188 }
1
2 javax.mail
3 mail
4 1.4.4
5
1 javax.mail.AuthenticationFailedException: 550 User has no permission
2
3 at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
4 at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
5 at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
6 at javax.mail.Service.connect(Service.java:317)
7 at javax.mail.Service.connect(Service.java:176)
8 at javax.mail.Service.connect(Service.java:125)
9 at javax.mail.Transport.send0(Transport.java:194)
10 at javax.mail.Transport.send(Transport.java:124)
11 at com.shihuc.demo.SendEmail.send(SendEmail.java:154)
12 at com.shihuc.demo.SendEmail.main(SendEmail.java:181)
13 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
15 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
16 at java.lang.reflect.Method.invoke(Method.java:606)
17 at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
这个错误,我折腾了很长时间,百度上找了很久,都找不到,google查看的信息,都是基于gmail的问题(535错误码,其实类似163的这个500的错误),最后,自己研究了下163邮箱上的功能菜单,摸索着修改,最终解决了这个问题!