本地生成邮件out.eml

80酷酷网    80kuku.com

  package dfh;

import java.io.Serializable;
import java.util.Vector;

public class Mail implements Serializable {

/**
* 定义邮件的优先级. Mail priority definition.
*/
public transient static final int LOW_PRIORITY = 5;
public transient static final int NORMAL_PRIORITY = 3;
public transient static final int HIGH_PRIORITY = 1;

/**
* 定义邮件的发送格式. Sending format definition.
*/
public transient static final int TEXT = 1;
public transient static final int HTML = 2;

/**
* since 2.0
*/
public int priority = NORMAL_PRIORITY;
public int format = TEXT;

/**
* The DNS server name.
*/
public String server;
public String from;
public String to;
public String subject;
public String body;
public Vector attachments;// Attachment files

public Mail(String server, String from, String to, String subject,
String body, Vector attachments, int priority, int format) {
this.server = server;
this.from = from;
this.to = to;
this.subject = subject;
this.body = body;
this.attachments = attachments;
this.priority = priority;
this.format = format;
}
}

package dfh;

import sun.misc.*;

import javax.activation.MimetypesFileTypeMap;
import javax.mail.Message;

import java.security.MessageDigest;
import java.io.*;
import java.util.*;
/**
* 对指定的 Mail 对象进行 MIME 编码并返回编码结果.
*/
public class MailEncoder {
private Mail mail;
// The mail to be encoded
private MessageDigest digest;
// Used to genarate a boundary string
private BASE64Encoder encoder = new BASE64Encoder();
// Encode content to BASE64 character

public MailEncoder(Mail mail) {
this.mail = mail;
try {
digest = MessageDigest.getInstance("MD5");// MD5 is 16 bit message digest
// Debug the encoder
FileWriter out = new FileWriter("out.eml");
out.write( encode() );
out.close();
}catch(Exception ex) {
ex.printStackTrace();
}
}

public String encode() {
String result = "";
byte[] data;
digest.update( ( (new Random()).nextInt() + "").getBytes() );
data = digest.digest();// Get a romdom integer and get it's digest
String mainBoundary = "****MAIN_BOUNDARY****"+ byte2hex(data);
// Main boundary
String subBoundary = "****SUB_BOUNDARY****" + byte2hex(data);
// Sub boundary
result += "To:" + encodeText(mail.to) + "\r\n"
+ "From:" + encodeText(mail.from) + "\r\n"
// Now we only support chinese
+ "Subject: " + encodeText(mail.subject) + "\r\n";
// The mail priority
if(mail.priority == Mail.LOW_PRIORITY)
{
result += "X-Priority: 5\r\n";
result += "X-MSMail-Priority: Low\r\n";
}
else if(mail.priority == Mail.HIGH_PRIORITY)
{
result += "X-Priority: 1\r\n";
result += "X-MSMail-Priority: High\r\n";
}
else if(mail.priority == Mail.NORMAL_PRIORITY)
{
result += "X-Priority: 3\r\n";
result += "X-MSMail-Priority: Normal\r\n";
}

result += "MIME-Version: 1.0\r\n"
+ "Content-Type: multipart/mixed;\r\n"
+ "\tboundary=\"" + mainBoundary + "\"\r\n"
+ "\r\n"
+ "This is a multi-part message in MIME format.\r\n"
+ "\r\n"
// Above is mail's header
+ "--" + mainBoundary + "\r\n"
// The mail's text body
+ "Content-Type: multipart/alternative;\r\n"
+ "\tboundary=\"" + subBoundary + "\"\r\n"
+ "\r\n"
+ "--" + subBoundary + "\r\n";
// Mail format(text or HTML)
if(mail.format == Mail.TEXT) {
result += "Content-Type: text/plain;\r\n";
} else if(mail.format == Mail.HTML) {
result += "Content-Type: text/html;\r\n";
}
result += "\tcharset=\"gb2312\"\r\n"
+ "Content-Transfer-Encoding: base64\r\n"
+ "\r\n"
+ encoder.encode( mail.body.getBytes() ) + "\r\n"
+ "\r\n"
+ "--" + subBoundary + "--\r\n"// End of sub boundary
+ "\r\n";
// Start attach files
if( mail.attachments != null )
for( int i = 0; i < mail.attachments.size(); i++ ) {
Attachment attachment = (Attachment)mail.attachments.elementAt(i);
result += "--" + mainBoundary + "\r\n"
+ "Content-Type:" + getMimeType(attachment.getFileName()) + ";\r\n"
+ "\tname=\"" + encodeText(attachment.getFileName()) + "\"\r\n"
+ "Content-Transfer-Encoding: base64\r\n"
+ "Content-Disposition: attachment;\r\n"
+ "\tfilename=\"" + encodeText(attachment.getFileName()) + "\"\r\n"
+ "\r\n"
+ encoder.encode( attachment.getFileData() ) + "\r\n"
+ "\r\n";
}

result += "--" + mainBoundary + "--";
// End of main boundary and the mail
return result;
}

private String getMimeType(String fileName)
{
MimetypesFileTypeMap map = new MimetypesFileTypeMap();
return map.getContentType(fileName);
}

/**
* Convert the bytes to hexadecimal string.
*/
public String byte2hex(byte[] b)
{
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++)
{
stmp=( Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) hs = hs + "0" + stmp;
else hs = hs + stmp;
}
return hs.toUpperCase();
}

public String encodeText(String text) {
return encodeText(text, "gb2312");
}

public String encodeText(String text, String encoding) {
boolean isAllAscii = true;
for(int i = 0; i < text.length(); i++) {
if(text.charAt(i) <= ' ' || text.charAt(i) >= '~') {
isAllAscii = false;
break;
}
}
if(isAllAscii) return text;

try {
byte[] bytes = text.getBytes();
if(encoding == null || encoding.length() == 0) {
encoding = "gb2312";
}
bytes = text.getBytes(encoding);
return "=?" + encoding + "?B?" + encoder.encode(bytes)
+ "?=";
} catch(Exception ex) {
return text;
}
}

public static void main(String[] args) {
Mail mail = new Mail("server", "发件人", "收件人", "主题",
"正文", null, Mail.HIGH_PRIORITY, Mail.HTML);
new MailEncoder(mail);
}
}

package dfh;

import java.io.*;
/**
* 提供附件支持, 保存一个附件的名字和数据.
*/
public class Attachment implements Serializable {
/**
* 附件文件名
*/
private String fileName;
/**
* 附件文件数据
*/
private byte[] data;

/**
* 给定本地文件读取文件内容并设置文件名和数据.
*/
public Attachment(String fileName) {
try {
FileInputStream in = new FileInputStream(fileName);
data = new byte[in.available()];
in.read(data);
in.close();
File file = new File(fileName);
fileName = file.getAbsolutePath();
}catch(Exception ex){
System.out.println("Not a valid attachment file.");
ex.printStackTrace();
data = null;
}
char separator = File.separatorChar;
if( fileName.lastIndexOf(separator) >= 0 )
setFileName( fileName.substring(fileName.lastIndexOf(separator) + 1) );
else setFileName( fileName );
}

/**
* 根据已知附件文件名和数据构造附件.
*/
public Attachment(String fileName, byte[] data) {
setFileName(fileName);
setFileData(data);
}

public Attachment() {}

/**
* 返回附件文件名.
*/
public String getFileName() {
return this.fileName;
}

/**
* 设置附件文件名.
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}

/**
* 返回附件数据.
*/
public byte[] getFileData() {
return this.data;
}

/**
* 设置附件数据.
*/
public void setFileData(byte[] data) {
this.data = data;
}

/**
* 返回附件描述字符串.
*/
public String toString() {
return getFileName() + "(" + getFileData().length + " 字节)";
}

}



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: