SMTP邮件发送的客户端实现(带服务器验证)(Java)

1 篇文章 0 订阅

简介

带服务器身份验证的Auth SMTP实验

运行结果:

在这里插入图片描述
在这里插入图片描述

完整代码:AuthSender.java

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import sun.misc.BASE64Encoder;

public class AuthSender {
	public static void main(String[] args) throws Exception {
		Date dDate = new Date();
		DateFormat dFormat = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,
				Locale.US);
		String command = null;

		/** 与邮件服务器建立TCP连接. */
		//在""中填入smtp服务器和正确端口
		// e.g. Socket socket = new Socket("mails.163.com",25);
		Socket socket = new Socket("mails.tsinghua.edu.cn", 25);

		/** 创建BufferedReader每次读入一行信息. */
		InputStream is = socket.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);

		/** 读入系统的欢迎信息. */
		String response = br.readLine();
		System.out.println(response);
		// 
		int code = 220;	
		if (!response.startsWith(Integer.toString(code))) {
			throw new Exception(code + " reply not received from server.");
		}

		/** 取得socket输出流的引用. */
		OutputStream os = socket.getOutputStream();

		/** 发送 HELO 命令并取得服务器响应. 
		 *	其中\r\n为回车符,每个命今都必需以它们结尾. */
		//填入命令
		command = "HELO mails.tsinghua.edu.cn\r\n";		
		System.out.print(command);
		os.write(command.getBytes("US-ASCII"));
		response = br.readLine();
		System.out.println(response);
		code = 250;
		if (!response.startsWith(Integer.toString(code))) {
			throw new Exception(code + " reply not received from server.");
		}
		
		/**
		 *发送Auth Login请求验证 
		 *需要发送BASE64编码的用户名和密码
		 */
		command = "Auth Login\r\n";		
		System.out.print(command);
		os.write(command.getBytes("US-ASCII"));
		response = br.readLine();
		System.out.println(response);
		// 服务器响应334时继续输入用户名和密码
		code = 334; 
		if (!response.startsWith(Integer.toString(code))) {
			throw new Exception(code + " reply not received from server.");
		}
		//发送用户名和密码
		String username = "guoyy18@mails.tsinghua.edu.cn";
		BASE64Encoder encoder = new BASE64Encoder();
		//BASE64编码
		String username_encoded = encoder.encode(username.getBytes()) + "\r\n";
		os.write(username_encoded.getBytes("US-ASCII"));
		response = br.readLine();
		System.out.println(response);
		// 服务器响应334时继续输入密码
		code = 334;
		if (!response.startsWith(Integer.toString(code))) {
			throw new Exception(code + " reply not received from server.");
		} 
		String password = "Gyy20011207";
		String password_encoded = encoder.encode(password.getBytes()) + "\r\n";
		os.write(password_encoded.getBytes("US-ASCII"));
		response = br.readLine();
		System.out.println(response);
		//响应开头为235表示验证成功
		code = 235; 
		if (!response.startsWith(Integer.toString(code))) {
			throw new Exception(code + " reply not received from server.");
		} 
		 
		
		

		/** 发送 MAIL FROM 命令. */
		command = "MAIL FROM:<guoyy18@mails.tsinghua.edu.cn>\r\n";
		System.out.print(command);
		os.write(command.getBytes("US-ASCII"));
		response = br.readLine();
		System.out.println(response);
		code = 250;
		if (!response.startsWith(Integer.toString(code))) {
			throw new Exception(code + " reply not received from server.");
		}

		/** 发送 RCPT TO 命令. */
		command = "RCPT TO:<guoyy18@126.com>\r\n";
		System.out.print(command);
		os.write(command.getBytes("US-ASCII"));
		response = br.readLine();
		System.out.println(response);
		 code = 250; 
		if (!response.startsWith(Integer.toString(code))) {
			throw new Exception(code + " reply not received from server.");
		}

		/** 发送 DATA 命令. */
		command = "DATA\r\n";
		System.out.print(command);
		os.write(command.getBytes("US-ASCII"));
		response = br.readLine();
		System.out.println(response);
		 code = 354;
		if (!response.startsWith(Integer.toString(code))) {
			throw new Exception(code + " reply not received from server.");
		}

		/** 自动写入当前的日期 */
		String date = "DATE: " + dFormat.format(dDate) + "\r\n";
		System.out.print(date);
		os.write(date.getBytes("US-ASCII"));
		String str = "";
		// 邮件中显示的发件人地址
		str = "From:" + "guoyy18@mails.tsinghua.edu.cn" + "\r\n";
		System.out.print(str);
		os.write(str.getBytes("US-ASCII"));
		// 邮件中显示的收件人地址
		str = "To:" + "guoyy18@126.com" + "\r\n";
		System.out.print(str);
		os.write(str.getBytes("US-ASCII"));

		/** 发送邮件內容. */
		//填入Subject内容.
		str = "SUBJECT:" + "Host your website on GitHub pages" + "\r\n\r\n";
		System.out.print(str);
		os.write(str.getBytes("US-ASCII"));
		//填入邮件正文内容.
		str = "Build a portfolio in minutes\r\n"
				+ "Host your personal, organization, or project pages directly from a GitHub repository. Learn how to use GitHub Pages and our site generator to launch your new site, fast.\r\n"
				+ "\r\n"
				+ "	\r\n"
				+ "GitHub Pages Guide\r\n"
				+ "Give your projects or portfolio a home with GitHub Pages. Publish Pages with a Jekyll theme or design, build, and launch your own website right from a GitHub repository.\r\n"
				+ "\r\n"
				+ "Get started with Pages\r\n"
				+ "	\r\n"
				+ "Easily create your website\r\n"
				+ "\r\n"
				+ "\r\n"
				+ "It’s a lot of work to create, contribute to, and maintain projects—but sharing them doesn’t have to be. Our personal website generator guides you through building a custom profile featuring your contributions, interests, and experience. Best of all, it only takes a few minutes.\r\n"
				+ "\r\n"
				+ "Build your site\r\n"
				+ "Need help?\r\n"
				+ "Let us help you do your best work—visit GitHub Help or contact us." + "\r\n";
		System.out.print(str);
		os.write(str.getBytes("US-ASCII"));

		/** 以.作为邮件内容的结束符 */
		str = ".\r\n";
		System.out.print(str);
		os.write(str.getBytes("US-ASCII"));
		response = br.readLine();
		System.out.println(response);
		 code = 250;
		if (!response.startsWith(Integer.toString(code))) {
			throw new Exception(code + " reply not received from server.");
		}

		/** 发送 QUIT 命令. */
		command = "QUIT\r\n";
		System.out.print(command);
		os.write(command.getBytes("US-ASCII"));
		response = br.readLine();
		System.out.println(response);
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值