/*
In Java, I often see people asking how to use javamail to send emails? How do I receive emails? How to access Multiple folders. The problem is scattered, and the reply of history has already been drowned in the ocean of problems.
I have worked on a Java project before, including the webmail function. I have been exploring javamail for Java implementation for a while, and I have finally learned a little bit. I often see this problem in the Forum, So I post some of my experiences and hope to help you.
This article only describes how to use javamail to send emails, including SMTP authentication, email attachment sending, and HTML content emails.
Other implementations related to multi-Mailbox, receiving POP3 mail and IMAP will be introduced in subsequent articles.
The following program needs: javamail, JAF package, J2EE. jar contains the preceding two packages. We recommend that you install j2sdkee or directly copy J2EE. jar to the JBuilder library or the system classpath.
*/
Package com. Me. util. mail;
/**
* @ Author zhangkun aistill@msn.com
* @ Version 1.0
*/
Import java. util .*;
Import javax. Mail .*;
Import javax. Mail. Internet .*;
Import java. util. date;
Import javax. Activation .*;
Import java. Io .*;
Import com. Me. util .*;
Public class Sendmail {
Private mimemessage mimemsg; // mime email object
Private session; // email Session Object
Private Properties props; // System Properties
Private Boolean needauth = false; // whether SMTP authentication is required
Private string username = ""; // SMTP authentication username and password
Private string Password = "";
Private multipart MP; // multipart object, mail content, title, attachments, and other content are added to it, and then the mimemessage object is generated.
/**
*
*/
Public Sendmail (){
Setsmtphost (getconfig. mailhost); // if no email server is specified, retrieve it from the getconfig class
Createmimemessage ();
}
Public Sendmail (string SMTP ){
Setsmtphost (SMTP );
Createmimemessage ();
}
/**
* @ Param hostname string
*/
Public void setsmtphost (string hostname ){
System. Out. println ("set system attributes: mail. SMTP. Host =" + hostname );
If (props = NULL) props = system. getproperties (); // obtain the system property object
Props. Put ("mail. SMTP. Host", hostname); // set the SMTP host
}
/**
* @ Return Boolean
*/
Public Boolean createmimemessage ()
{
Try {
System. Out. println ("prepare to get the mail session object! ");
Session = session. getdefainstance instance (props, null); // get the mail Session Object
}
Catch (exception e ){
System. Err. println ("An error occurred while obtaining the mail session object! "+ E );
Return false;
}
System. Out. println ("prepare to create a mime mail object! ");
Try {
Mimemsg = new mimemessage (session); // create a mime mail object
MP = new mimemultipart ();
Return true;
}
Catch (exception e ){
System. Err. println ("An error occurred while creating the mime mail object! "+ E );
Return false;
}
}
/**
* @ Param need Boolean
*/
Public void setneedauth (Boolean need ){
System. Out. println ("set SMTP Identity Authentication: mail. SMTP. Auth =" + need );
If (props = NULL) props = system. getproperties ();
If (need ){
Props. Put ("mail. SMTP. Auth", "true ");
} Else {
Props. Put ("mail. SMTP. Auth", "false ");
}
}
/**
* @ Param name string
* @ Param pass string
*/
Public void setnamepass (string name, string pass ){
Username = Name;
Password = pass;
}
/**
* @ Param mailsubject string
* @ Return Boolean
*/
Public Boolean setsubject (string mailsubject ){
System. Out. println ("set email subject! ");
Try {
Mimemsg. setsubject (mailsubject );
Return true;
}
Catch (exception e ){
System. Err. println ("An error occurred while setting the email subject! ");
Return false;
}
}
/**
* @ Param mailbody string
*/
Public Boolean setbody (string mailbody ){
Try {
Bodypart BP = new mimebodypart ();
BP. setcontent ("<meta http-equiv = Content-Type content = text/html; charset = gb2312>" + mailbody, "text/html; charset = gb2312 ");
MP. addbodypart (BP );
Return true;
}
Catch (exception e ){
System. Err. println ("an error occurred when setting the mail body! "+ E );
Return false;
}
}
/**
* @ Param name string
* @ Param pass string
*/
Public Boolean addfileaffix (string filename ){
System. Out. println ("Add email attachment:" + filename );
Try {
Bodypart BP = new mimebodypart ();
Filedatasource fileds = new filedatasource (filename );
BP. setdatahandler (New datahandler (fileds ));
BP. setfilename (fileds. getname ());
MP. addbodypart (BP );
Return true;
}
Catch (exception e ){
System. Err. println ("Add email attachment:" + filename + "error! "+ E );
Return false;
}
}
/**
* @ Param name string
* @ Param pass string
*/
Public Boolean setfrom (string from ){
System. Out. println ("set the sender! ");
Try {
Mimemsg. setfrom (New internetaddress (from); // you can specify the sender.
Return true;
}
Catch (exception E)
{Return false ;}
}
/**
* @ Param name string
* @ Param pass string
*/
Public Boolean setto (string ){
If (to = NULL) return false;
Try {
Mimemsg. setrecipients (message. recipienttype. To, internetaddress. parse ());
Return true;
}
Catch (exception E)
{Return false ;}
}
/**
* @ Param name string
* @ Param pass string
*/
Public Boolean setcopyto (string copyto)
{
If (copyto = NULL) return false;
Try {
Mimemsg. setrecipients (message. recipienttype. CC, (Address []) internetaddress. parse (copyto ));
Return true;
}
Catch (exception E)
{Return false ;}
}
/**
* @ Param name string
* @ Param pass string
*/
Public Boolean sendout ()
{
Try {
Mimemsg. setcontent (MP );
Mimemsg. savechanges ();
System. Out. println ("sending an email ....");
Session mailsession = session. getinstance (props, null );
Transport transport = mailsession. gettransport ("SMTP ");
Transport. Connect (string) props. Get ("mail. SMTP. Host"), username, password );
Transport. sendmessage (mimemsg, mimemsg. getrecipients (message. recipienttype. ));
// Transport. Send (mimemsg );
System. Out. println ("sent successfully! ");
Transport. Close ();
Return true;
}
Catch (exception E)
{
System. Err. println ("failed to send email! "+ E );
Return false;
}
}
/**
* Just do it as this
*/
Public static void main (string [] ARGs ){
String mailbody = "<meta http-equiv = Content-Type content = text/html; charset = gb2312>" +
"<Div align = center> <a href = http://www.csdn.net> csdn </a> </div> ";
Sendmail themail = new Sendmail ("smtp.msn.com ");
Themail. setneedauth (true );
If (themail. setsubject ("title") = false) return;
If (themail. setbody (mailbody) = false) return;
If (themail. setto ("gates@msn.com") = false) return;
If (themail. setfrom ("bill@msn.com") = false) return;
If (themail. addfileaffix ("C: // boot. ini") = false) return;
Themail. setnamepass ("user", "password ");
If (themail. sendout () = false) return;
}
} |