Use jmail to send ssl mail

Source: Internet
Author: User
Tags ssl connection

Package org. Dreams. mail;

Import java. Io. bufferedreader;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java. util. date;
Import java. util. properties;

Import javax. Mail. authenticator;
Import javax. Mail. folder;
Import javax. Mail. message;
Import javax. Mail. messagingexception;
Import javax. Mail. passwordauthentication;
Import javax. Mail. sendfailedexception;
Import javax. Mail. Session;
Import javax. Mail. store;
Import javax. Mail. urlname;
Import javax. Mail. Internet. internetaddress;
Import javax. Mail. Internet. mimebodypart;
Import javax. Mail. Internet. mimemessage;
Import javax. Mail. Internet. mimemultipart;

Import com. Sun. Mail. SMTP. smtpaddressfailedexception;
Import com. Sun. Mail. SMTP. smtpaddresssucceededexception;
Import com. Sun. Mail. SMTP. smtpsendfailedexception;
Import com. Sun. Mail. SMTP. smtptransport;

Public class jmailsslsender {

Public static void main (string [] argv ){
System. setproperty ("javax.net. SSL. truststore", "C: // javalib // SSL // mail"); // key storage location
System. setproperty ("javax.net. SSL. truststorepassword", "password for opening the key file ");

String to = "recipient's address ";
String subject = "hello"; // mail title
String from = "courier address ";
String cc = "CC address ";
String BCC = NULL;
String url = NULL;

String mailhost = "smtp.xxx.com"; // email server address
String Mailer = "jmailsslsender ";
String file = NULL;
String protocol = NULL;
String host = "smtp.xxx.com ";
String user = "User Name ";
String Password = "password ";

String record = NULL; // name of folder in which to record mail
Boolean DEBUG = true;
Boolean verbose = true;
Boolean auth = true;
Boolean SSL = true;
Bufferedreader in = new bufferedreader (New inputstreamreader (system. In ));
Int optind;

Try {

Properties props = system. getproperties ();
If (mailhost! = NULL)
Props. Put ("mail. SMTP. Host", mailhost );

If (auth ){
If (SSL ){
Props. Put ("mail. smtps. Auth", "true"); // attributes used by the SSL connection
} Else {
Props. Put ("mail. SMTP. Auth", "true"); // attributes of normal connections
}

}

// Get a Session Object
Session session = session. getinstance (props, new authenticator (){
Protected passwordauthentication getpasswordauthentication (){
Return new passwordauthentication ("username", "password ");
}
});

If (Debug ){
Session. setdebug (true );
}

// Construct the message
Message MSG = new mimemessage (session );
If (from! = NULL)
MSG. setfrom (New internetaddress (from ));
Else
MSG. setfrom ();

MSG. setrecipients (message. recipienttype. To, internetaddress. parse (to, false ));
If (CC! = NULL)
MSG. setrecipients (message. recipienttype. CC, internetaddress. parse (CC, false ));
If (bcc! = NULL)
MSG. setrecipients (message. recipienttype. BCC, internetaddress. parse (BCC, false ));

MSG. setsubject (subject );

String text = "I am Liu! "; // Collect (in );

If (file! = NULL ){
// Attach the specified file.
// We need a multipart message to hold the attachment.
Mimebodypart mbp1 = new mimebodypart ();
Mbp1.settext (text );
Mimebodypart mbp2 = new mimebodypart ();
Mbp2.attachfile (File );
Mimemultipart MP = new mimemultipart ();
MP. addbodypart (mbp1 );
MP. addbodypart (mbp2 );
MSG. setcontent (MP );
} Else {
// If the desired charset is known, you can use
// Settext (text, charset)
MSG. settext (text );
}

MSG. setheader ("X-mailer", Mailer );
MSG. setsentdate (new date ());

// Send the thing off
/*
* The simple way to send a message is this:
*
Transport. Send (MSG );
*
* But we're re going to use some SMTP-specific features
* Demonstration purposes so we need to manage the transport
* Object explicitly.
*/
Smtptransport t = (smtptransport) Session. gettransport (SSL? "Smtps": "SMTP ");
Try {
If (auth)
T. Connect (mailhost, user, password );
Else
T. Connect ();

T. sendmessage (MSG, MSG. getallrecipients ());
} Finally {
If (verbose)
System. Out. println ("response:" + T. getlastserverresponse ());
T. Close ();
}

System. Out. println ("/nmail was sent successfully .");

// Keep a copy, if requested.

If (record! = NULL ){
// Get a store object
Store store = NULL;
If (URL! = NULL ){
Urlname urln = new urlname (URL );
Store = session. getstore (urln );
Store. Connect ();
} Else {
If (protocol! = NULL)
Store = session. getstore (Protocol );
Else
Store = session. getstore ();

// Connect
If (host! = NULL | user! = NULL | password! = NULL)
Store. Connect (host, user, password );
Else
Store. Connect ();
}

// Get record folder. Create if it does not exist.
Folder folder = store. getfolder (record );
If (Folder = NULL ){
System. Err. println ("can't get record folder .");
System. Exit (1 );
}
If (! Folder. exists ())
Folder. Create (Folder. holds_messages );

Message [] msgs = new message [1];
Msgs [0] = MSG;
Folder. appendmessages (MSGs );

System. Out. println ("mail was recorded successfully .");
}

} Catch (exception e ){
If (E instanceof sendfailedexception ){
Messagingexception SFE = (messagingexception) E;
If (SFE instanceof smtpsendfailedexception ){
Smtpsendfailedexception SSFE = (smtpsendfailedexception) SFE;
System. Out. println ("SMTP send failed :");
If (verbose)
System. Out. println (SSFE. tostring ());
System. Out. println ("command:" + SSFE. getcommand ());
System. Out. println ("retcode:" + SSFE. getreturncode ());
System. Out. println ("response:" + SSFE. getmessage ());
} Else {
If (verbose)
System. Out. println ("Send failed:" + SFE. tostring ());
}
Exception ne;
While (Ne = SFE. getnextexception ())! = NULL & ne instanceof messagingexception ){
SFE = (messagingexception) Ne;
If (SFE instanceof smtpaddressfailedexception ){
Smtpaddressfailedexception SSFE = (smtpaddressfailedexception) SFE;
System. Out. println ("address failed :");
If (verbose)
System. Out. println (SSFE. tostring ());
System. Out. println ("Address:" + SSFE. getaddress ());
System. Out. println ("command:" + SSFE. getcommand ());
System. Out. println ("retcode:" + SSFE. getreturncode ());
System. Out. println ("response:" + SSFE. getmessage ());
} Else if (SFE instanceof smtpaddresssucceededexception ){
System. Out. println ("address succeeded :");
Smtpaddresssucceededexception SSFE = (smtpaddresssucceededexception) SFE;
If (verbose)
System. Out. println (SSFE. tostring ());
System. Out. println ("Address:" + SSFE. getaddress ());
System. Out. println ("command:" + SSFE. getcommand ());
System. Out. println ("retcode:" + SSFE. getreturncode ());
System. Out. println ("response:" + SSFE. getmessage ());
}
}
} Else {
System. Out. println ("got exception:" + E );
If (verbose)
E. printstacktrace ();
}
}
}

Public static string collect (bufferedreader in) throws ioexception {
String line;
Stringbuffer sb = new stringbuffer ();
While (line = in. Readline ())! = NULL ){
SB. append (line );
SB. append ("/N ");
}
Return sb. tostring ();
}
}

Http://blog.csdn.net/kknd97/archive/2006/10/19/1340350.aspx

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.