Use Gmail to send emails with javamail

Source: Internet
Author: User

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 );
}
}

 

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.