Skip to content

Commit 2effb94

Browse files
committed
Java-QQ邮箱发送邮件
Signed-off-by: chenhaoxiang <[email protected]>
1 parent 548a660 commit 2effb94

20 files changed

+274
-0
lines changed

myJavaMainDemo/.classpath

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7.0_04"/>
5+
<classpathentry kind="lib" path="src/cn/hncu/lib/activation.jar"/>
6+
<classpathentry kind="lib" path="src/cn/hncu/lib/java-mail-1.4.4.jar"/>
7+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
8+
<classpathentry kind="output" path="bin"/>
9+
</classpath>

myJavaMainDemo/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>myJavaMainDemo</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Sun Aug 21 01:06:01 CST 2016
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.6
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.6

myJavaMainDemo/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
JavaMail-smtp协议发送邮件!
2+
861 Bytes
Binary file not shown.
806 Bytes
Binary file not shown.
4.17 KB
Binary file not shown.
55 KB
Binary file not shown.
Binary file not shown.
363 KB
Binary file not shown.
2.61 KB
Binary file not shown.

myJavaMainDemo/imgs/1.jpg

6.8 KB
Loading

myJavaMainDemo/imgs/2.jpg

7.64 KB
Loading

myJavaMainDemo/imgs/中文1.jpg

20.2 KB
Loading
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.hncu;
2+
3+
import sun.misc.BASE64Encoder;
4+
5+
public class EncodeDemo {
6+
7+
public static void main(String[] args) {
8+
BASE64Encoder be = new BASE64Encoder();
9+
10+
String name = "chenhaoxiang0117";
11+
String pwd = "chenhaoxiang52**";
12+
name = be.encode(name.getBytes());
13+
pwd = be.encode(pwd.getBytes());
14+
//英文可以不指定编码
15+
System.out.println(name);
16+
System.out.println(pwd);
17+
}
18+
19+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package cn.hncu;
2+
3+
import java.util.Properties;
4+
5+
import javax.activation.DataHandler;
6+
import javax.activation.FileDataSource;
7+
import javax.mail.Authenticator;
8+
import javax.mail.PasswordAuthentication;
9+
import javax.mail.Session;
10+
import javax.mail.Transport;
11+
import javax.mail.internet.InternetAddress;
12+
import javax.mail.internet.MimeBodyPart;
13+
import javax.mail.internet.MimeMessage;
14+
import javax.mail.internet.MimeMessage.RecipientType;
15+
import javax.mail.internet.MimeMultipart;
16+
import javax.mail.internet.MimeUtility;
17+
18+
import org.junit.Test;
19+
20+
import com.sun.mail.util.MailSSLSocketFactory;
21+
22+
public class SendMailDemo {
23+
24+
25+
@Test//发送没有附件的邮件
26+
public void send1() throws Exception{
27+
//跟smtp服务器建立一个连接
28+
Properties p = new Properties();
29+
// 设置邮件服务器主机名
30+
p.setProperty("mail.host", "smtp.qq.com");//指定邮件服务器,默认端口 25
31+
// 发送服务器需要身份验证
32+
p.setProperty("mail.smtp.auth", "true");//要采用指定用户名密码的方式去认证
33+
// 发送邮件协议名称
34+
p.setProperty("mail.transport.protocol", "smtp");
35+
36+
// 开启SSL加密,否则会失败
37+
MailSSLSocketFactory sf = new MailSSLSocketFactory();
38+
sf.setTrustAllHosts(true);
39+
p.put("mail.smtp.ssl.enable", "true");
40+
p.put("mail.smtp.ssl.socketFactory", sf);
41+
42+
// 开启debug调试,以便在控制台查看
43+
//session.setDebug(true);也可以这样设置
44+
//p.setProperty("mail.debug", "true");
45+
46+
// 创建session
47+
Session session = Session.getDefaultInstance(p, new Authenticator() {
48+
@Override
49+
protected PasswordAuthentication getPasswordAuthentication() {
50+
//用户名可以用QQ账号也可以用邮箱的别名
51+
PasswordAuthentication pa = new PasswordAuthentication("chenhaoxiang0117", "jnjtrhojpxswbdab");
52+
// 后面的字符是授权码,用qq密码不行!!
53+
return pa;
54+
}
55+
});
56+
57+
session.setDebug(true);//设置打开调试状态
58+
59+
for (int i = 0; i <1; i++) {//发送几封邮件
60+
//声明一个Message对象(代表一封邮件),从session中创建
61+
MimeMessage msg = new MimeMessage(session);
62+
//邮件信息封装
63+
//1发件人
64+
msg.setFrom(new InternetAddress("[email protected]"));
65+
//2收件人
66+
msg.setRecipient(RecipientType.TO, new InternetAddress(
67+
68+
//3邮件内容:主题、内容
69+
msg.setSubject("这是我用Java发来的邮件QQ....");
70+
//msg.setContent("Hello, 今天没下雨!!!", "text/plain;charset=utf-8");//纯文本
71+
msg.setContent(
72+
"Hello <a href='http://www.baidu.com?id=ddd'>你好,快乐吗?<a/>",
73+
"text/html;charset=utf-8");//发html格式的文本
74+
//发送动作
75+
Transport.send(msg);
76+
}
77+
}
78+
79+
@Test//发送含附件的邮件
80+
public void send2() throws Exception{
81+
//跟smtp服务器建立一个连接
82+
Properties p = new Properties();
83+
// 开启debug调试,以便在控制台查看
84+
p.setProperty("mail.debug", "true");
85+
p.setProperty("mail.host", "smtp.sina.com");//指定邮件服务器,默认端口 25
86+
p.setProperty("mail.smtp.auth", "true");//要采用指定用户名密码的方式去认证
87+
// 发送邮件协议名称
88+
p.setProperty("mail.transport.protocol", "smtp");
89+
90+
// 开启SSL加密,否则会失败
91+
MailSSLSocketFactory sf = new MailSSLSocketFactory();
92+
sf.setTrustAllHosts(true);
93+
p.put("mail.smtp.ssl.enable", "true");
94+
p.put("mail.smtp.ssl.socketFactory", sf);
95+
96+
// 创建session
97+
Session session = Session.getInstance(p);
98+
99+
// 通过session得到transport对象
100+
Transport ts = session.getTransport();
101+
102+
// 连接邮件服务器:邮箱类型,帐号,授权码代替密码(更安全)
103+
ts.connect("smtp.qq.com", "619699629", "jnjtrhojpxswbdab");
104+
// 后面的字符是授权码,不能用qq密码
105+
106+
//声明一个Message对象(代表一封邮件),从session中创建
107+
MimeMessage msg = new MimeMessage(session);
108+
//邮件信息封装
109+
//1发件人
110+
msg.setFrom( new InternetAddress("[email protected]") );
111+
//2收件人
112+
msg.setRecipient(RecipientType.TO, new InternetAddress("[email protected]") );
113+
//3邮件内容:主题、内容
114+
msg.setSubject("这是我用Java发来的邮件--带附件的....");
115+
116+
//添加附件部分
117+
//邮件内容部分1---文本内容
118+
MimeBodyPart body0 = new MimeBodyPart(); //邮件中的文字部分
119+
body0.setContent("这是两张<font color='red'>图片</font>....","text/html;charset=utf-8");
120+
121+
//邮件内容部分2---附件1
122+
MimeBodyPart body1 = new MimeBodyPart(); //附件1
123+
body1.setDataHandler( new DataHandler( new FileDataSource("./imgs/1.jpg")) );//./代表项目根目录下
124+
125+
body1.setFileName( MimeUtility.encodeText("中文1.jpg") );//中文附件名,解决乱码
126+
127+
//邮件内容部分3---附件2
128+
MimeBodyPart body2 = new MimeBodyPart(); //附件2
129+
body2.setDataHandler( new DataHandler( new FileDataSource("./imgs/2.jpg")) );
130+
body2.setFileName("2.jpg");
131+
132+
//把上面的3部分组装在一起,设置到msg中
133+
MimeMultipart mm = new MimeMultipart();
134+
mm.addBodyPart(body0);
135+
mm.addBodyPart(body1);
136+
mm.addBodyPart(body2);
137+
msg.setContent(mm);
138+
139+
// 发送邮件
140+
ts.sendMessage(msg,msg.getAllRecipients());
141+
ts.close();
142+
}
143+
}
55 KB
Binary file not shown.
Binary file not shown.
363 KB
Binary file not shown.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package cn.hncu;
2+
3+
import java.util.Properties;
4+
5+
import javax.mail.Message;
6+
import javax.mail.Session;
7+
import javax.mail.Transport;
8+
import javax.mail.internet.InternetAddress;
9+
import javax.mail.internet.MimeMessage;
10+
11+
import com.sun.mail.util.MailSSLSocketFactory;
12+
13+
/**
14+
* JavaMail发送邮件:前提是QQ邮箱里帐号设置要开启POP3/SMTP协议
15+
*/
16+
public class sendqqMail {
17+
18+
public static void main(String[] args) throws Exception {
19+
20+
Properties prop = new Properties();
21+
// 开启debug调试,以便在控制台查看
22+
prop.setProperty("mail.debug", "true");
23+
// 设置邮件服务器主机名
24+
prop.setProperty("mail.host", "smtp.qq.com");
25+
// 发送服务器需要身份验证
26+
prop.setProperty("mail.smtp.auth", "true");
27+
// 发送邮件协议名称
28+
prop.setProperty("mail.transport.protocol", "smtp");
29+
30+
// 开启SSL加密,否则会失败
31+
MailSSLSocketFactory sf = new MailSSLSocketFactory();
32+
sf.setTrustAllHosts(true);
33+
prop.put("mail.smtp.ssl.enable", "true");
34+
prop.put("mail.smtp.ssl.socketFactory", sf);
35+
36+
// 创建session
37+
Session session = Session.getInstance(prop);
38+
39+
// 通过session得到transport对象
40+
Transport ts = session.getTransport();
41+
// 连接邮件服务器:邮箱类型,帐号,授权码代替密码(更安全)
42+
ts.connect("smtp.qq.com", "619***629", "jnjt***bdab");
43+
// 后面的字符是授权码,用qq密码失败了
44+
45+
// 创建邮件
46+
Message message = createSimpleMail(session);
47+
// 发送邮件
48+
ts.sendMessage(message, message.getAllRecipients());
49+
ts.close();
50+
}
51+
52+
/**
53+
* @Method: createSimpleMail
54+
* @Description: 创建一封只包含文本的邮件
55+
*/
56+
public static MimeMessage createSimpleMail(Session session)
57+
throws Exception {
58+
// 创建邮件对象
59+
MimeMessage message = new MimeMessage(session);
60+
// 指明邮件的发件人
61+
message.setFrom(new InternetAddress("[email protected]"));
62+
// 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
63+
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
64+
65+
// 邮件的标题
66+
message.setSubject("JavaMailQQ邮件测试");
67+
// 邮件的文本内容
68+
message.setContent("JavaMail发送邮件成功!", "text/html;charset=UTF-8");
69+
// 返回创建好的邮件对象
70+
return message;
71+
}
72+
}

0 commit comments

Comments
 (0)