标签:cte mail 创建 注册 文件 utf-8 images str 会话
一、邮件协议
1 package cn.itcast.base64;
2
3 import org.junit.Test;
4 import sun.misc.BASE64Decoder;
5 import sun.misc.BASE64Encoder;
6 import java.io.IOException;
7
8 public class Dome1 {
9 @Test
10 public void fun1() throws IOException {
11 //BASE64编码
12 String s = "Username";
13 BASE64Encoder encoder = new BASE64Encoder();
14 s = encoder.encode(s.getBytes("UTF-8"));
15 System.out.println(s);
16 //BASE64解码
17 BASE64Decoder decoder = new BASE64Decoder();
18 byte[] bytes = decoder.decodeBuffer(s);
19 System.out.println(new String(bytes,"utf-8"));
20 }
21 }
使用apache commons组件中的codec包下的Base64这个类来完成BASE64加密和解密。
1 import org.apache.commons.codec.binary.Base64;
2
3 public class Base64Utils {
4 public static String encode(String s) {
5 return encode(s, "utf-8");
6 }
7
8 public static String decode(String s) {
9 return decode(s, "utf-8");
10 }
11
12 public static String encode(String s, String charset) {
13 try {
14 byte[] bytes = s.getBytes(charset);
15 bytes = Base64.encodeBase64(bytes);
16 return new String(bytes, charset);
17 } catch (Exception e) {
18 throw new RuntimeException(e);
19 }
20 }
21
22 public static String decode(String s, String charset) {
23 try {
24 byte[] bytes = s.getBytes(charset);
25 bytes = Base64.decodeBase64(bytes);
26 return new String(bytes, charset);
27 } catch (Exception e) {
28 throw new RuntimeException(e);
29 }
30 }
31 }
$ telnet smtp.163.com 25
from:<chengcheng@163.com>to:<itcast_cxf@sina.com>subject: 你好hi,我是chengcheng。.
Authenticator auth = new Authenticator() {public PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication(“用户名”, “密码”);//用户名和密码}};
Session session = Session.getInstance(prop, auth);
Transport.send(msg);//发送邮件
1 package javamail;
2
3 import org.junit.Test;
4 import javax.mail.Authenticator;
5 import javax.mail.PasswordAuthentication;
6 import javax.mail.Session;
7 import javax.mail.Transport;
8 import javax.mail.internet.*;
9 import java.io.File;
10 import java.util.Properties;
11
12 public class Demo1 {
13 @Test
14 public void fun1() throws Exception{
15 //1、得到session
16 Properties props = new Properties();
17 props.setProperty("mail.host","smtp.163.com");//邮箱主机
18 props.setProperty("mail.smtp.auth","true");//设置邮箱验证
19
20 javax.mail.Authenticator auth = new Authenticator() {
21 @Override
22 protected PasswordAuthentication getPasswordAuthentication() {
23 return new PasswordAuthentication("****","*****");//用户名与密码
24 }
25 };
26 Session session = Session.getInstance(props,auth);
27
28 //2、创建MimeMessage
29 MimeMessage msg = new MimeMessage(session);
30 msg.setFrom(new InternetAddress("*****@163.com"));//设置发件人
31 msg.setRecipients(MimeMessage.RecipientType.TO,"******@qq.com");//设置多个收件人
32 // msg.setRecipients(MimeMessage.RecipientType.CC,"***@126.com");//设置抄送
33 // msg.setRecipients(MimeMessage.RecipientType.BCC,"***@126.com");//设置暗送
34
35 msg.setSubject("这是一封测试邮件");
36 msg.setContent("测试邮件","text/html;charset=utf-8");
37 //3、发邮件
38 Transport.send(msg);
39 }
40 /*
41 * 带附件邮件
42 * */
43 @Test
44 public void fun2() throws Exception{
45 //1、得到session
46 Properties props = new Properties();
47 props.setProperty("mail.host","smtp.163.com");
48 props.setProperty("mail.smtp.auth","true");
49
50 javax.mail.Authenticator auth = new Authenticator() {
51 @Override
52 protected PasswordAuthentication getPasswordAuthentication() {
53 return new PasswordAuthentication("gdwuguibin","1990926");
54 }
55 };
56 Session session = Session.getInstance(props,auth);
57
58 //2、创建MimeMessage
59 MimeMessage msg = new MimeMessage(session);
60 msg.setFrom(new InternetAddress("gdwuguibin@163.com"));//设置发件人
61 msg.setRecipients(MimeMessage.RecipientType.TO,"1037345628@qq.com");//设置多个收件人
62
63 msg.setSubject("这是一封带附件测试邮件");
64 /*
65 * 当发送包含附件的邮件时,邮件体就为多部件形式
66 * 1、创建一个多部件内容,MimeMultipart
67 * MimeMultipart就是一个集合,用来装载多个主体部件
68 * 2、需要创建两个主体部件,一个是文本内容的,另一个是附件的
69 * 主体部件叫MimeBodyPart
70 * 3、把MimeMultipart设置给MimeMessage的内容
71 * */
72 MimeMultipart list = new MimeMultipart();//创建多部分主体
73 //创建MimeBodyPart
74 MimeBodyPart part1 = new MimeBodyPart();
75 //设置主体部件的内容
76 part1.setContent("这是一封带附件的测试邮件","text/html;charset=utf-8");
77 //把主体部件添加到集合中
78 list.addBodyPart(part1);
79 //创建MimeBodyPart
80 MimeBodyPart part2 = new MimeBodyPart();
81 part2.attachFile(new File("/Users/Mac/Downloads/img/2.jpg"));//设置附件的内容
82 part2.setFileName(MimeUtility.encodeText("海参.jpg"));//设置显示的文件名称,其中encodeText用来处理中文乱码问题
83 list.addBodyPart(part2);
84
85 msg.setContent(list);//把它设置给邮件作为邮件的内容
86 //3、发邮件
87 Transport.send(msg);
88 }
89 }
Java读取properties配置文件时,中文乱码解决方法:http://blog.csdn.net/u012743772/article/details/49153699
标签:cte mail 创建 注册 文件 utf-8 images str 会话
原文地址:http://www.cnblogs.com/gdwkong/p/7660877.html