自己写的MailUtils类以及测试类

charlie_mei Base64 编码的编程使用 最后由 极客学院_吕布 于2016年10月09日回复

  • 1 回答
  • 1k 浏览

下面的代码包括了具体的`package`。

MailUtils类的实现如下:

package com.neil.java.mailutils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

import com.neil.java.base64.Base64Utils;

public class MailUtils {

	// 用户名
	static String sender = "cnsmtp01@163.com";
	// 邮件接受者
	// static String receiver = "1129915332@qq.com";
	// 密码
	static String password = "computer";
	// 邮件服务器地址
	static String mailServer = "smtp.163.com";
	// 邮件服务器端口
	static int mailPort = 25;

	// 防止new对象
	private MailUtils() {
	}

	/**
	 * 发送邮件
	 * 
	 * @param receiver
	 *            邮件接受者
	 * @param subject
	 *            邮件subject
	 * @param content
	 *            邮件内容
	 * @param contentType
	 *            邮件内容的contentType:text/plain 或者text/html
	 */
	public static void sendMail(String receiver, String subject, String content, String contentType) {

		// 将用户名和密码进行Base64编码
		String userBase64 = Base64Utils.encryptBase64(sender.substring(0, sender.indexOf("@")).getBytes());
		String passBase64 = Base64Utils.encryptBase64(password.getBytes());
		try {
			Socket socket = new Socket(mailServer, mailPort);
			InputStream inputStream = socket.getInputStream();
			OutputStream outputStream = socket.getOutputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
			PrintWriter writter = new PrintWriter(outputStream, true);
			System.out.println(reader.readLine());

			// HELO
			writter.println("HELO jikexueyuan");
			System.out.println(reader.readLine());

			// AUTH LOGIN >>>Base64
			writter.println("AUTH LOGIN");
			System.out.println(reader.readLine());
			writter.println(userBase64);
			System.out.println(reader.readLine());
			writter.println(passBase64);
			System.out.println(reader.readLine());

			// Set "MAIL FROM" and "RCPT TO"
			writter.println("MAIL FROM:<" + sender + ">");
			System.out.println(reader.readLine());
			writter.println("RCPT TO:<" + receiver + ">");
			System.out.println(reader.readLine());

			// Set "DATA"
			writter.println("DATA");
			System.out.println(reader.readLine());

			writter.println("SUBJECT:" + subject);
			writter.println("FROM:" + sender);
			writter.println("TO:" + receiver);
			writter.println("Content-Type:" + contentType);
			writter.println();
			writter.println(content);
			writter.println(".");
			writter.println("");
			System.out.println(reader.readLine());

			// 发送完毕了,和服务器拜拜
			writter.println("RSET");
			System.out.println(reader.readLine());
			writter.println("QUIT");
			System.out.println(reader.readLine());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

MailTest使用Junit4作测试,代码内容如下:

package com.neil.java.mailutils;

import org.junit.Test;

public class MailTest {

	@Test
	public void sendMail()
	{
		String reciever = "1129915332@qq.com";
		String subject ="你好,这是来自MailUtils的使用";
		String content ="<html><body><h1>Hello world</h1></body></html>";
		String contentType ="text/html;charset="gb2312"";
		MailUtils.sendMail(reciever,subject,content,contentType);
	}
}