Generate Mail locally out.eml

Source: Internet
Author: User
Tags format define base64 definition final format definition integer mail
Package dfh;

Import java.io.Serializable;
Import Java.util.Vector;

public class Mail implements Serializable {

/**
* Define the priority of the message. 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;

/**
* Define the sending format for the message. 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.*;
/**
* MIME-encodes the specified Mail object and returns the encoded result.
*/
public class Mailencoder {
Private mail mail;
The mail to IS 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 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 are 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", "Sender", "Recipient", "subject",
"Body", null, mail.high_priority, mail.html);
New Mailencoder (mail);
}
}

Package dfh;

Import java.io.*;
/**
* Provide attachment support, save an attachment name and data.
*/
public class attachment implements Serializable {
/**
* Attachment file name
*/
Private String FileName;
/**
* Attachment file Data
*/
Private byte[] data;

/**
* Read the contents of the file and set the file name and data given the local file.
*/
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);
}

/**
* Construct attachments based on known attachment file names and data.
*/
Public Attachment (String fileName, byte[] data) {
Setfilename (FileName);
Setfiledata (data);
}

Public attachment () {}

/**
* Returns the file name of the attachment.
*/
Public String GetFileName () {
return this.filename;
}

/**
* Set the attachment file name.
*/
public void Setfilename (String fileName) {
This.filename = FileName;
}

/**
* Returns the attachment data.
*/
Public byte[] Getfiledata () {
return this.data;
}

/**
* Set up attachment data.
*/
public void Setfiledata (byte[] data) {
This.data = data;
}

/**
* Returns the attachment description string.
*/
Public String toString () {
return GetFileName () + "(" + getfiledata (). length + "byte");
}

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.