Package com. xxxxxx. oss. commons. mail;
Import java. util. date;
Import java. util. enumeration;
Import java. util. properties;
Import java. util. vector;
Import javax. Activation .*;
Import javax. Mail. authenticator;
Import javax. Mail. message;
Import javax. Mail. multipart;
Import javax. Mail. passwordauthentication;
Import javax. Mail. Session;
Import javax. Mail. Transport;
Import javax. Mail. Internet. internetaddress;
Import javax. Mail. Internet. mimebodypart;
Import javax. Mail. Internet. mimemessage;
Import javax. Mail. Internet. mimemultipart;
Import javax. Mail. Internet. mimeutility;
Public class mailutils {
Private Static string filename1;
String to = ""; // recipient
String to1 = ""; // CC
String from = ""; // sender
String host = ""; // SMTP host
String username = "";
String Password = "";
String filename = ""; // File Name of the Attachment
String subject = ""; // Email Subject
String content = ""; // body of the email
Vector file = new vector (); // a collection of attachment files
Public mailutils (){
}
/**
* <Br>
* Method Description: constructor that provides direct parameter input <br>
* Input parameters: <br>
* Return type:
*/
Public mailutils (string to, string to1, string from, string smtpserver,
String username, string password, string subject, string content ){
This. To =;
This. to1 = to1;
This. From = from;
This. Host = smtpserver;
This. Username = username;
This. Password = password;
This. Subject = subject;
This. content = content;
}
/**
* <Br>
* Method Description: Set the email server address <br>
* Input parameter: String host email server address name <br>
* Return type:
*/
Public void sethost (string host ){
This. Host = host;
}
/**
* <Br>
* Method Description: Set the logon server verification password <br>
* Input parameters: <br>
* Return type:
*/
Public void setpassword (string PWD ){
This. Password = PWD;
}
/**
* <Br>
* Method Description: Set the login server to verify the user <br>
* Input parameters: <br>
* Return type:
*/
Public void setusername (string USN ){
This. Username = USN;
}
/**
* <Br>
* Method Description: Set the email sending destination email address <br>
* Input parameters: <br>
* Return type:
*/
Public void setto (string ){
This. To =;
}
/**
* <Br>
* Method Description: Set the email sending destination (CC) email address <br>
* Input parameters: <br>
* Return type:
*/
Public void setto1 (string to1 ){
This. to1 = to1;
}
/**
* <Br>
* Method Description: Set the email sending source email address. <br>
* Input parameters: <br>
* Return type:
*/
Public void setfrom (string from ){
This. From = from;
}
/**
* <Br>
* Method Description: Set the email subject. <br>
* Input parameters: <br>
* Return type:
*/
Public void setsubject (string subject ){
This. Subject = subject;
}
/**
* <Br>
* Method Description: Set the email content. <br>
* Input parameters: <br>
* Return type:
*/
Public void setcontent (string content ){
This. content = content;
}
/**
* <Br>
* Method Description: converts a topic to Chinese. <br>
* Input parameter: String strtext <br>
* Return type:
*/
Public String transferchinese (string strtext ){
Try {
Strtext = mimeutility. encodetext (new string (strtext. getbytes (),
"Gb2312"), "gb2312", "B ");
} Catch (exception e ){
E. printstacktrace ();
}
Return strtext;
}
/**
* <Br>
* Method Description: add an attachment to the attachment combination. <br>
* Input parameters: <br>
* Return type:
*/
Public void attachfile (string fname ){
File. addelement (fname );
}
/**
* <Br>
* Method Description: send an email <br>
* Input parameters: <br>
* Return type: Boolean success is true, and vice versa.
*/
Public Boolean Sendmail (){
// Construct a mail session
Properties props = new properties ();
// Props. Put ("mail. Transport. Protocol", "SMTP ");
Props. Put ("mail. SMTP. Host", host );
Props. Put ("mail. SMTP. Auth", "true ");
// Props. Put ("mail. SMTP. Port", "25 ");
Session session = session. getdefaultinstance (props,
New authenticator (){
Public passwordauthentication getpasswordauthentication (){
Return new passwordauthentication (username, password );
}
});
// Session = session. getdefaultinstance (props );
// Session = session. getdefaultinstance (props, null );
Try {
// Construct the mimemessage and set the basic value
Session. setdebug (true );
System. setproperty ("mail. Mime. charset", "UTF-8 ");
Mimemessage MSG = new mimemessage (session );
// Mimemessage MSG = new mimemessage ();
MSG. setfrom (New internetaddress (from ));
MSG. addrecipients (message. recipienttype. To, internetaddress. parse (to1); // This can only send an email to a person
MSG. setrecipients (message. recipienttype. CC, internetaddress. parse ());
Subject = transferchinese (subject );
MSG. setsubject (subject );
// Construct multipart
Multipart MP = new mimemultipart ();
// Add the body to multipart
Mimebodypart mbpcontent = new mimebodypart ();
Mbpcontent. setcontent (content, "text/html; charset = gb2312 ");
// Add to mimemessage (multipart indicates the body)
MP. addbodypart (mbpcontent );
// Add an attachment to multipart
Enumeration efile = file. Elements ();
While (efile. hasmoreelements ()){
Mimebodypart mbpfile = new mimebodypart ();
Filename = efile. nextelement (). tostring ();
Filedatasource FDS = new filedatasource (filename );
Mbpfile. setdatahandler (New datahandler (FDS ));
Mbpfile. setfilename (mimeutility. encodetext (FDS. getname ()));
// Add to mimemessage (multipart represents the attachment)
MP. addbodypart (mbpfile );
}
File. removeallelements ();
// Add mimemessage to multipart
MSG. setcontent (MP );
MSG. setsentdate (new date ());
MSG. savechanges ();
// Send an email
Transport transport = session. gettransport ("SMTP ");
Transport. Connect (host, username, password );
Transport. sendmessage (MSG, MSG. getallrecipients ());
Transport. Close ();
} Catch (exception MEX ){
Mex. printstacktrace ();
// Exception EX = NULL;
// If (EX = Mex. getnextexception ())! = NULL ){
// Ex. printstacktrace ();
//}
Return false;
}
Return true;
}
/**
* @ Param ARGs
*/
// Public static void main (string [] ARGs ){
//
// Mailutils Sendmail = new mailutils ();
//
// Sendmail. sethost ("smtp.revenco.com ");
//
// Sendmail. setusername ("wangbinhui ");
//
// Sendmail. setpassword ("123456 ");
//
// Sendmail. setto ("yangchuanru@Revenco.com, zhumingjun@revenco.com ");
//
// Sendmail. setfrom ("wangbinhui@revenco.com ");
//
// Sendmail. setsubject ("Hello, this is a test! ");
//
// Sendmail. setcontent ("Hello, this is a test with multiple attachments! ");
//
// Filename1 = "D: \ create a text file. txt ";
//
// Sendmail. attachfile (filename1 );
// Sendmail. attachfile ("D: \ 21021014 .xlsx ");
//
//
// System. Out. println (sendmail. Sendmail ());
//
//}
}