关于使用javamail包发送邮件和邮件编码乱码解决方法
1. 在发送正文时指定正文编码:
在发送邮件时使用
MimeBodyPart body = new MimeBodyPart();
body.setContent(content, "text/html;charset=GB2312");
注意此时的content编码必须是所指定的编码格式。
2. 在设置邮件标题时也要指定标题的编码:
MimeMultipart mmp=new MimeMultipart();
mmp.setSubject(subject, "GB2312");
同上也要求subject的编码和指定的编码一致。
3. 发送正文时也可以在header中指定传输编码:
body.setHeader("Content-Transfer-Encoding", "base64"); // 指定使用base64编码
4. 例子:
[java] view
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class MailSender {
public static void main(String[] args) {
try {
String host = "staff.tixa.com"; // smtp主机
String username = "sample@staff.tixa.com"; // 认证用户名
String password = "sample"; // 认证密码
String from = "例子<sample@staff.tixa.com>"; // 发送者
String to = "toOne@staff.tixa.com, toAnother@staff.tixa.com"; // 接受者,用“,”分隔
String subject = "测试例子";
String content = "仅仅是个供测试的例子。";
// 建立session
Properties prop = new Properties();
prop.put("mail.smtp.host", host);
prop.put("mail.smtp.auth", "true"); //是否需要认证
Session session = Session.getDefaultInstance(prop, null);
// 创建MIME邮件对象
MimeMessage mimeMsg = new MimeMessage(session);
MimeMultipart mp = new MimeMultipart();
// 设置信息
mimeMsg.setFrom(new InternetAddress(from));
mimeMsg.setSubject(subject, "GB2312"); // !!!注意设置编码
mimeMsg.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(to));
// 设置正文
BodyPart body = new MimeBodyPart();
body.setContent(content, "text/plain;charset=GB2312"); // !!!注意设置编码
mp.addBodyPart(body);
mimeMsg.setContent(mp);
// 发送邮件
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
transport.close();
}
catch(Exception exp) {
exp.printStackTrace();
}
} import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class MailSender {
public static void main(String[] args) {
try {
String host = "staff.tixa.com"; // smtp主机
String username = "sample@staff.tixa.com"; // 认证用户名
String password = "sample"; // 认证密码
String from = "例子<sample@staff.tixa.com>"; // 发送者
String to = "toOne@staff.tixa.com, toAnother@staff.tixa.com"; // 接受者,用“,”分隔
String subject = "测试例子";
String content = "仅仅是个供测试的例子。";
// 建立session
Properties prop = new Properties();
prop.put("mail.smtp.host", host);
prop.put("mail.smtp.auth", "true"); //是否需要认证
Session session = Session.getDefaultInstance(prop, null);
// 创建MIME邮件对象
MimeMessage mimeMsg = new MimeMessage(session);
MimeMultipart mp = new MimeMultipart();
// 设置信息
mimeMsg.setFrom(new InternetAddress(from));
mimeMsg.setSubject(subject, "GB2312"); // !!!注意设置编码
mimeMsg.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(to));
// 设置正文
BodyPart body = new MimeBodyPart();
body.setContent(content, "text/plain;charset=GB2312"); // !!!注意设置编码
mp.addBodyPart(body);
mimeMsg.setContent(mp);
// 发送邮件
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
transport.close();
}
catch(Exception exp) {
exp.printStackTrace();
}
}
|
本文介绍如何使用JavaMail包发送邮件,并提供了解决邮件正文及标题编码乱码的方法,包括设置正文编码、邮件标题编码及使用Base64编码等。
1024

被折叠的 条评论
为什么被折叠?



