Tested with 163 mailbox and supports SSL connections. Send mail

Source: Internet
Author: User
Tags imap ssl connection

Example: Jack sends an e-mail to Rose.

public class SendMail {PublicStaticvoid Main (String[] (args) {Boolean ISSSL =TrueString host ="Smtp.163.com";int port =465;String from ="[Email protected]";String to ="[Email protected]";Boolean Isauth =TrueFinalString username ="[Email protected]";FinalString Password ="Jack"; Properties props =New Properties (); Props.put ("Mail.smtp.ssl.enable", Isssl); Props.put ("Mail.smtp.host", host); Props.put ("Mail.smtp.port", port); Props.put ("Mail.smtp.auth", Isauth); Session session = Session.getdefaultinstance (props,New Authenticator () {@OverrideProtected Passwordauthentication getpasswordauthentication () {ReturnNew Passwordauthentication (username, password); }        });try {Message message =New MimeMessage (session);            Message.setfrom ( new InternetAddress (from));            Message.addrecipient (Message.RecipientType.TO, new InternetAddress (to));            Message.setsubject ("subject");            Message.settext ("content");        Transport.send (message);        } catch (Addressexception e) {e.printstacktrace ();        } catch (Messagingexception e) {e.printstacktrace (); } System.out. println ("Send complete!    "); }}
Receive mail

Example: Rose receives a recent email.

Import Java.util.Date;Import java.util.Properties;Import Javax.mail.Folder;Import Javax.mail.Message;Import javax.mail.MessagingException;Import javax.mail.NoSuchProviderException;Import javax.mail.Session;Import Javax.mail.Store;public class FetchMail {PublicStaticvoid Main (String[] (args) {String protocol ="POP3";Boolean ISSSL =TrueString host ="Pop.163.com";int port =995;String username ="[Email protected]";String Password ="Rose"; Properties props =New Properties (); Props.put ("Mail.pop3.ssl.enable", Isssl); Props.put ("Mail.pop3.host", host); Props.put ("Mail.pop3.port", port); Session session = Session.getdefaultinstance (props); Store store =Null Folder folder =Nulltry {store = session.getstore (protocol); Store.connect (username, password); folder = Store.getfolder ("INBOX"); Folder.Open (folder.read_only);IntSize = Folder.getmessagecount (); Message message = Folder.getmessage (size);String from = Message.getfrom () [0].tostring ();String subject = Message.getsubject (); Date date = Message.getsentdate (); System.out.println "from:" + from); System.out. println ( "Subject:" + Subject); System.out. println ( "date:" + date);} catch (nosuchproviderexception e) {e.printstacktrace ();} catch (messagingexception e) {e.printstacktrace ();} finally {try {if (folder != null) {Folder.close (false);} if (store! = null) {store.close ();}} catch (messagingexception e) {e.printstacktrace ();}} System.out. println ( "reception complete! "); }} 

Common mail Protocols

Send mail: SMTP

Receive mail: POP3, IMAP

Common mail Configuration Items
Configuration Items Description
Mail.xxx.ssl.enable Whether to support SSL connection
Mail.xxx.host Mail server host Name
Mail.xxx.port Mail server port number
Mail.xxx.auth Whether to authenticate


Description: XXX represents the protocol name, for example: SMTP, POP3, and so on.

Default port number
Smtp POP3 Imap
Normal way 25 110 143
SSL mode 465 995 993
Send mail using Apache Commons email

Import Org.apache.commons.mail.Email;Import org.apache.commons.mail.EmailException;Import Org.apache.commons.mail.SimpleEmail;public class SendMail {PublicStaticvoid Main (String[] (args) {Boolean ISSSL =TrueString host ="Smtp.163.com";int port =465;string from =  "[email protected]"; string to =  "[email protected]"; string username =  "[email protected]"; string password = " Jack "; try {e-mail = new simpleemail (); Email.setsslonconnect ( ISSSL); Email.sethostname (host); Email.setsmtpport (port); Email.setauthentication (username, password); Email.setfrom (from); Email.addto (to); Email.setsubject (catch (emailexception e) {e.printstacktrace ();} System.out. println ( "sent complete! "); }}

Reference: http://commons.apache.org/proper/commons-email/userguide.html

Finish

Tested with 163 mailbox and supports SSL connections. Send mail

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.