Package practical;
Import java. security. Security;
Import java. util. properties;
Import javax. Mail. message;
Import javax. Mail. messagingexception;
Import javax. Mail. passwordauthentication;
Import javax. Mail. Session;
Import javax. Mail. Transport;
Import javax. Mail. Internet. internetaddress;
Import javax. Mail. Internet. mimemessage;
Public class googletest {
Private Static final string smtp_host_name = "smtp.gmail.com ";
Private Static final string smtp_port = "465 ";
Private Static final string emailmsgtxt = "Test message contents ";
Private Static final string emailsubjecttxt = "a test from Gmail ";
Private Static final string emailfromaddress = "something@gmail.com ";
Private Static final string ssl_factory = "javax.net. SSL. sslsocketfactory ";
Private Static final string [] sendto = {"someone@gmail.com "};
Public static void main (string ARGs []) throws exception {
Security. addprovider (New com.sun.net. SSL. Internal. SSL. provider ());
New googletest (). sendsslmessage (sendto, emailsubjecttxt,
Emailmsgtxt, emailfromaddress );
System. Out. println ("sucessfully sent mail to all users ");
}
Public void sendsslmessage (string recipients [], string subject,
String message, string from) throws messagingexception {
Boolean DEBUG = true;
Properties props = new properties ();
Props. Put ("mail. SMTP. Host", smtp_host_name );
Props. Put ("mail. SMTP. Auth", "true ");
Props. Put ("mail. debug", "true ");
Props. Put ("mail. SMTP. Port", smtp_port );
Props. Put ("mail. SMTP. socketfactory. Port", smtp_port );
Props. Put ("mail. SMTP. socketfactory. Class", ssl_factory );
Props. Put ("mail. SMTP. socketfactory. Fallback", "false ");
Session session = session. getdefaultinstance (props,
New javax. Mail. authenticator (){
Protected passwordauthentication getpasswordauthentication (){
Return new passwordauthentication ("something@gmail.com", "pass ");
}
});
Session. setdebug (Debug );
Message MSG = new mimemessage (session );
Internetaddress addressfrom = new internetaddress (from );
MSG. setfrom (addressfrom );
Internetaddress [] addressto = new internetaddress [recipients. Length];
For (INT I = 0; I <recipients. length; I ++ ){
Addressto [I] = new internetaddress (recipients [I]);
}
MSG. setrecipients (message. recipienttype. To, addressto );
// Setting the subject and content type
MSG. setsubject (subject );
MSG. setcontent (message, "text/plain ");
Transport. Send (MSG );
}
}