e-mail based on Java mail to achieve a simple QQ mailbox

Source: Internet
Author: User

Just learn Java mail-related knowledge, first write down this blog, easy to read the study later.

-----------------------------The first step to turn on the SMTP service

Open the SMTP service in the account set up in the QQ mailbox

Complete validation

Get the Authorization code (used when the code is implemented later)

-----------------------------Second Step environment configuration

Download third-party libraries

Https://github.com/javaee/javamail/releases

-----------------------------The third step of code implementation

 PackageCom.core;Importjava.security.GeneralSecurityException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importjava.util.Properties;Importjavax.mail.Address;ImportJavax.mail.Message;Importjavax.mail.MessagingException;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeMessage;Importcom.sun.mail.util.MailSSLSocketFactory; Public classMailtool { Public Static voidMain (string[] args)throwsmessagingexception, generalsecurityexception {Properties props=NewProperties (); //turn on debug debuggingProps.setproperty ("Mail.debug", "true"); //The sending server requires authenticationProps.setproperty ("Mail.smtp.auth", "true"); //set the mail server host nameProps.setproperty ("Mail.host", "smtp.qq.com"); //Send mail Agreement nameProps.setproperty ("Mail.transport.protocol", "SMTP"); //turn on SSL encryption, or it will failMailsslsocketfactory SF =Newmailsslsocketfactory (); Sf.settrustallhosts (true); Props.put ("Mail.smtp.ssl.enable", "true"); Props.put ("Mail.smtp.ssl.socketFactory", SF); //Create sessionSession session =session.getinstance (props); //Create a messageMessage msg =NewMimeMessage (session); //Set TitleMsg.setsubject ("test Message"); //Edit ContentStringBuilder Builder =NewStringBuilder (); Builder.append ("This is a Java mail test message \ n"); Builder.append ("This is the second line."); Builder.append ("\ n Time" +getstringdate ()); //Set ContentMsg.settext (builder.tostring ()); //e-mail address sentMsg.setfrom (NewInternetAddress ("Own Mailbox @qq.com")); //get Transport objects by sessionTransport Transport =Session.gettransport (); //Connect mail server: Mailbox type, account number, authorization code instead of password (more secure)Transport.connect ("smtp.qq.com", "own Mailbox @qq.com", "Authorization Code"); //Send mailTransport.sendmessage (MSG,NewAddress[] {NewInternetAddress ("Target mailbox @qq.com") });    Transport.close (); }        /*** Get current time *@returnString*/     Public StaticString getstringdate () {Date currenttime=NewDate (); SimpleDateFormat Formatter=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String datestring=Formatter.format (currenttime); returndatestring; }}

-----------------------------Fourth Step effect show

-----------------------------The fifth step recommended

Java Send mail most complete example

Java based on JavaMail implementation to QQ mailbox Send mail

e-mail based on Java mail to achieve a simple QQ mailbox

Related Article

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.