JavaMail 模拟了命令行形式编写发送端

13 篇文章 0 订阅

最简单的发送端:

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 = "gpodalian_edu@163.com";

		sm.username = "gpodalian_edu@163.com";

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

		sm.to = "gpodalian_edu@163.com";

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

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

		sm.init();

		sm.auth();

		sm.sendDate();
	}
}


 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值