最简单的发送端:

package com.ccl.mail;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

import sun.misc.BASE64Encoder;

/**
 * 
 * @author changlun.cheng
 * @since 2012/4/11 12:08 PM
 */

public class SendMail {

	public String server = "";
	public int port = 25;
	public String from = "";
	public String username = "";
	public String password = "";
	public String to = "";
	public String subject = "";
	public String content = "";

	Socket s = null;
	BufferedReader in = null;
	BufferedWriter out = null;
	BASE64Encoder encode;

	/**
	 * 初始化
	 * 
	 * @throws UnknownHostException
	 * @throws IOException
	 */
	public void init() throws UnknownHostException, IOException {

		s = new Socket(server, port);// get connection

		encode = new BASE64Encoder();

		in = new BufferedReader(new InputStreamReader(s.getInputStream()));

		out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

		if (this.getResult() == 220) {
			System.out.println("connection ok.");
		} else {
			System.err.println("connection to failed.");
		}

		this.sendCMD("HELO " + server);
		if (this.getResult() == 250)
			System.out.println("service is register.");
		else {
			System.err.println("to register fail.");

		}
	}

	/**
	 * auth the username and password
	 * 
	 * @throws IOException
	 */
	public void auth() throws IOException {

		sendCMD("AUTH LOGIN");

		if (this.getResult() == 334) {
			System.out
					.println("user is auth,palese send username and password");
		} else {
			System.err.println("auth fail.");
		}

		sendCMD(encode.encode(username.getBytes()));
		if (this.getResult() == 334) {
			System.out.println("auth username.");
		} else {
			System.err.println("username is error.");
		}

		sendCMD(encode.encode(password.getBytes()));
		if (this.getResult() == 235)
			System.out.println("password right.");
		else {
			System.err.println("password is error.");
		}

	}

	public void sendDate() throws IOException {

		sendCMD("MAIL FROM:<" + from + ">");

		if (this.getResult() == 250) {
			System.out.println("mail from:");
			// TODO
		} else {
			System.err.println("bind adrres is error");
		}

		sendCMD("RCPT TO:<" + to + ">");

		if (this.getResult() == 250) {
			// TODO
			System.out.println("rcpt to:");
		} else
			System.err.println("bind adrres is error");

		sendCMD("DATA");
		if (this.getResult() == 354) {
			System.out.println("data");
			// TODO
		} else {

			System.err.println("could't send this data");
		}
		sendCMD("FROM: " + from);
		sendCMD("TO: " + to);
		sendCMD("Subject: " + subject);
		sendCMD(content);
		sendCMD(".");

		if (this.getResult() == 250) {
			System.out.println("success send over ");
		} else {
			System.out.println("send data error");
		}
		sendCMD("QUIT");
		if (this.getResult() == 221) {
			System.out.println("quit.");
		} else {
			System.err.println("quit error");
		}

	}

	private int getResult() throws IOException {

		String line = in.readLine();
		System.out.println("from service msg:"+line);
		System.out.println(line.split("\\s")[0]);
		int status = 0;
		if (line != null) {

			StringTokenizer st = new StringTokenizer(line, " ");

			status = Integer.parseInt(st.nextToken());

		}

		return status;
	}

	private void sendCMD(String cmd) throws IOException {

		out.write(cmd);
		out.newLine();
		out.flush();
	}

	public static void main(String[] args) throws Exception {

		SendMail sm = new SendMail();

		sm.server = "smtp.163.com";

		sm.port = 25;

		sm.from = "[email protected]";

		sm.username = "[email protected]";

		sm.password = "**************";

		sm.to = "[email protected]";

		sm.subject = "mail test topic \n";// 此处要换行,否则发送没有内容

		sm.content = "想了解更多,请 点击\n  http://blog.csdn.net/chengchanglun/article/details/7450421";

		sm.init();

		sm.auth();

		sm.sendDate();
	}
}


 



            
   
   
   
   
作者:chengchanglun 发表于2012-4-11 17:03:44 原文链接
阅读:106 评论:0 查看评论