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