怎么发送中文邮件呢smtp,(怎么转换成BASE64)

05-03-29 hyseal
mport java.net.*;

import java.io.*;

import java.util.*;

import javax.naming.*;

import javax.naming.directory.*;

public class Smtp {

private static String END_FLAG = "\r\n";

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

String mx = "sohumx.sohu.com";

InetAddress addr = InetAddress.getByName(mx);

Socket socket = new Socket(addr, 25);

InputStream in = socket.getInputStream();

OutputStream out = socket.getOutputStream();

// 连接成功后服务器会响应:

response(in);

// 首先发送HELO命令:

send("HELO www.javasprite.com" + END_FLAG, out);

response(in);

// 在发信人与受信人的邮箱地址都要用< >包含

// 然后发送发件人地址:

send("MAIL FROM:<someone@somewhere.com>" + END_FLAG, out);

response(in);

// 设置收件人地址:

send("RCPT TO:<asklxf@sohu.com>" + END_FLAG, out);

response(in);

// 开始发送邮件正文:

send("DATA" + END_FLAG, out);

response(in);

send("From: someone@somewhere.com" + END_FLAG, out);

send("To: asklxf@sohu.com" + END_FLAG, out);

send("Subject: Test without smtp server" + END_FLAG, out);

send("Content-Type: text/html ;charset=\"gb2312\"" + END_FLAG, out);

send(END_FLAG + END_FLAG, out);

// 发送邮件正文,如果用中文,需要BASE64编码:

send("text message body!" + END_FLAG, out);

// 每行以\r\n结束,不可过长,可拆成多行。

// 以"\r\n.\r\n"作为结束标志:

send(END_FLAG + "." + END_FLAG, out);

response(in);

// 结束并确认发送:

send("QUIT" + END_FLAG, out);

response(in);

in.close();

out.close();

socket.close();

}

public static void response(InputStream in) throws Exception {

byte[] buffer = new byte[1024];

int n = in.read(buffer);

String s = new String(buffer, 0, n);

// 服务器会返回:### Text

// 具体含义见RFC821

System.out.println(s);

}

public static void send(String s, OutputStream out) throws Exception {

byte[] buffer = s.getBytes();

out.write(buffer);

// 不要忘了flush(),否则可能在缓冲区:

out.flush();

}

}

/////////////////////////////////////////

怎么用这个代码发送中文内容邮件呢

怎么用Base64进行编码呢,

请高手告诉我谢谢!!!

小弟不慎感激!!

helow
2005-05-07 17:13
去 download 一个BASE64包吧

猜你喜欢