java编写基于命令行形式,[原]JavaMail 模拟了命令行形式编写发送端

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:");

if (this.getResult() == 250) {

System.out.println("mail from:");

// TODO

} else {

System.err.println("bind adrres is error");

}

sendCMD("RCPT 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();

}

}

作者:chengchanglun 发表于2012-4-11 17:03:44 原文链接

阅读:106 评论:0 查看评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、创建类实现基本对象和他们关系的管理,包括学生、教学班、课程、成绩、教师等。使用集合框架存储所有对象。学生至少包含学号、姓名、性别等信息。教学班至少包含教师、课程名字、总人数、教学班号、开课学期等信息。课程至少包含课程编号、课程名字等信息。教师至少包含教师编号、姓名等信息。 2、随机生成学生,数量不少于100。一个教学班有一个教师上一门课程,教学班的学生数量不少于20。课程数量不少于3门。教师数量不少于6个。一门课至少有两个老师上课。每个学生选择至少选择3门课程。一个学生在一个教学班上一门课,考试后取得一个成绩。一门课的成绩构成有4部分构成,包括平时成绩、期中考试、实验成绩和期末考试成绩,然后计算出总成绩。成绩随机生成,均为整数。 3、分阶段模拟教学过程。例如执行一个命令,可以生成一个教学班的所有学生的平时成绩。第一步,生成初始化数据,包括教师,学生、课程,教学班等;第二步,学生选课,随机进行,为每门课程的教学班安排学生。第三步,获得平时成绩,获得期中成绩,获得实验成绩,获得期末成绩,最后计算总成绩。 4、能够显示一个教学班级的学生,可以根据学号排序,可以根据成绩排序。可以通过名字查询成绩,可以按照各科成绩和总成绩进行排名显示,可以统计各课程学生成绩的分数段分布。 5、可以实现自己的扩展功能。注意类和类之间的关系。充分利用继承,多态等特性,使用上抽象类,接口,泛型,内部类等设计元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值