JavaMail开发2手动发送邮件

Base64编码

  SUN公司提供了一个Base64算法,可以将用户名和密码进行加密。

  BASE64Encoder encoder = new BASE64Encoder();

1 加密:
2 encoder.encode(username.getBytes());
3 encoder.encode(password.getBytes());

Base64解码

  SUN公司提供了一个Base64算法,可以将用户名和密码进行解密。

  BASE64Decoder decoder = new BASE64Decoder();

1 解密:
2 new String(decoder.decodeBuffer(username))
3 new String(decoder.decodeBuffer(password))

  Base64编码和解码案例:

import java.io.IOException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

//	使用Mail专用的Base64编码和解码
public class Demo1 {

	public static void main(String[] args) {

//		一、编码
		System.out.println("编码");
		//明文		
		String username = "hacket";
		String password = "123";
		System.out.println("明文:"+username+":"+password);
		BASE64Encoder encoder = new BASE64Encoder();
//		密文
		username = encoder.encode(username.getBytes());
		password = encoder.encode(password.getBytes());
		System.out.println("密文:"+username+":"+password);
		/*明文:xiao:123
		密文:eGlhbw==:MTIz*/
		System.out.println("==============");
//		二、解码
		BASE64Decoder base64Decoder = new BASE64Decoder();
		try {
			byte[] buf1 = base64Decoder.decodeBuffer(username);
			byte[] buf2 = base64Decoder.decodeBuffer(password);
			username = new String(buf1,"utf-8");
			password = new String(buf2,"utf-8");
			System.out.println("解码后:"+username+":"+password);
			System.out.println("==============");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 手工演示电子邮件的发送

  1、相同域中:

 1 发送端: aaa@hacket.cn --→ bbb@hacket.cn
 2     明文:aaa:123
 3     密文:YWFh:MTIz
 4     ==============
 5     telnet 127.0.0.1 25<回车>  --(SMTP服务器端口))
 6     ehlo hacket<回车>
 7     auth login<回车>
 8     YWFh<回车>
 9     MTIz<回车>
10     mail from:<aaa@hacket.cn><回车>
11     rcpt to:<bbb@hacket.cn><回车>
12     data<回车> 
13     from:aaa@hacket.cn<回车>           
14     to:bbb@hacket.cn<回车> 
15     subject:mySubject<回车>
16     Hello! How are you doing!This is my first email!<回车>
17     .<回车>
18     quit<回车>
19     
20 相同域中  
21 接收端:bbb@hacket.cn
22     明文:bbb:456
23     密文:YmJi:NDU2
24     ==============
25     telnet 127.0.0.1 110
26     user bbb
27     pass 456
28     stat
29     list 2
30     retr 2
31     quit

相同域发送端:

 

相同域接收端:

 2、不同域中:

 1 不同域中
 2  发送端
 3     明文:aaa@hacket.cn:123
 4     密文:YWFhQGhhY2tldC5jbg==:MTIz
 5     ==============================
 6     telnet 127.0.0.1 25
 7     ehlo hacket
 8     auth login
 9     YWFhQGhhY2tldC5jbg==
10     MTIz
11     mail from:<aaa@hacket.cn>
12     rcpt to:<bbb@qq.cn>
13     data
14     from:aaa@hacket.cn        
15     to:bbb@qq.cn
16     subject:mySubject2
17     Hello! This is a different domain!!
18     .
19     quit
20  
21  不同域中
22  接收端
23     明文:bbb@qq.cn:456
24     密文:YmJiQHFxLmNu:NDU2
25     ======================
26     telnet 127.0.0.1 110
27     user bbb@qq.cn
28     pass 456
29     stat
30     list 2
31     retr 2
32     quit

不同域-发送端:

 

不同域-接收端:

 SMTP协议

•ehlo 主机名
•auth login //经过base64编码后的用户名和密码
•mail from:<aaa@zhaojun.com>
•rcpt to:<bbb@zhaojun.com>
•data   .号代表邮件内容的结束 --data表示数据的开始,.表示数据的结束
•quit
 
POP3协议
•user<SP>username<CRLF>
•pass<SP>password<CRLF> 
•stat<CRLF> 返回邮箱的统计信息(字节数)
•list<SP>[msg#]<CRLF>返回某一封邮件的统计信息
•retr<SP>msg#<CRLF>  最重要的一个命令
•quit<CRLF>
•使用POP3协议手工接收邮件

 手工发送邮件代码:

 1 import java.io.BufferedReader;
 2 import java.io.InputStreamReader;
 3 import java.io.OutputStream;
 4 import java.net.Socket;
 5 
 6 //客户端发送邮件
 7 public class Demo2 {
 8     public static void main(String[] args) throws Exception{
 9         
10         //参数一:表示易邮服务器位于的IP地址
11         //参数二:SMTP邮件发送服务器的固定端口
12         Socket s = new Socket("127.0.0.1",25);
13         
14         //构造行字符读入流
15         BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
16         //构造字节输出流
17         OutputStream os = s.getOutputStream();
18         
19         System.out.println(br.readLine());
20         os.write("ehlo zhaojun\r\n".getBytes());
21         System.out.println(br.readLine());
22         System.out.println(br.readLine());
23         
24         os.write("auth login\r\n".getBytes());
25         System.out.println(br.readLine());
26         
27         os.write("YWFhQHpoYW9qdW4uY29t\r\n".getBytes());//用户名
28         System.out.println(br.readLine());
29         os.write("MTIzNDU2\r\n".getBytes());//密码
30         System.out.println(br.readLine());
31         
32         os.write("mail from:<aaa@zhaojun.com>\r\n".getBytes());
33         System.out.println(br.readLine());
34         
35         os.write("rcpt to:<bbb@qq.com>\r\n".getBytes());
36         System.out.println(br.readLine());
37         
38         os.write("data\r\n".getBytes());
39         System.out.println(br.readLine());
40         
41         os.write("from:aaa@zhaojun.com\r\n".getBytes());
42         os.write("to:bbb@qq.com\r\n".getBytes());
43         os.write("subject:test\r\n".getBytes());
44         os.write("Hello! How are you doing!\r\n".getBytes());
45         os.write(".\r\n".getBytes());
46         System.out.println(br.readLine());
47         
48         os.write("quit\r\n".getBytes());
49         
50         //关闭流
51         os.close();
52         br.close();
53         
54     }
55 }
原文地址:https://www.cnblogs.com/hacket/p/3043888.html