How to configure Java mail to send mail using QQ mailbox

Source: Internet
Author: User
Tags mailmessage

Recently I have been tossing the next Java in the use of Mai to send QQ mail

1.QQ Mailbox Settings

1.1 Go to QQ mailbox--set-up account--set up as

  

2.foxmail settings (because I want to use it to receive mail)

2.1 Refer to the official settings to http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=371

PS: Fill in the email password is a separate password: need to note that the SSL link to check; SMTP port is 465

Code configuration in 3.Java

3.1 Send mail configuration code

//Send Mailbox Verification        Try{Properties prop=NewProperties (); Prop.setproperty ("Mail.transport.protocol", "SMTP"); Prop.setproperty ("Mail.smtp.host", "smtp.qq.com"); Prop.setproperty ("Mail.smtp.auth", "true"); Prop.put ("Mail.smtp.port", "25"); Prop.setproperty ("Mail.debug", "true"); Authenticator Authenticator=NewPopauthenticator ("[Email protected]", "4444444"); //Create a sessionSession session =session.getinstance (Prop,authenticator); //fill out an envelope to writeMessage msg =NewMimeMessage (session); Msg.setfrom (NewInternetAddress ("[Email protected]")); Msg.setrecipient (Recipienttype.to,Newinternetaddress (User.getemail ())); Msg.setsubject (User.getusername ()+ "Activate mailbox!"); Msg.settext (User.getusername ()+ "Hello, please go to this address to activate your account: http://www.estore.com/ActiveServlet?activecode=" +User.getactivecode ()); //Verify user name password Send messageTransport Transport =Session.gettransport ();//transport.connect ("[Email protected]", "4444444");transport.send (msg); }         
View Code

3.2 Auxiliary Classes

 Public classPopauthenticatorextendsAuthenticator {String userName=NULL; String Password=NULL;  PublicPopauthenticator () {} PublicPopauthenticator (string Username, string password) { This. UserName =username;  This. Password =password; }        protectedpasswordauthentication getpasswordauthentication () {return Newpasswordauthentication (userName, password); }}
View Code

3.3 If you want to send HTML, you can refer to the following code:

MimeMessage MailMessage =Newmimemessage (sendmailsession); Mailmessage.setfrom (NewInternetAddress ("[Email protected]")); //The Message.RecipientType.TO property indicates that the recipient's type is toMailmessage.setrecipient (Message.RecipientType.TO,Newinternetaddress (to)); Mailmessage.setsubject (Subject,"UTF-8"); Mailmessage.setsentdate (NewDate ()); //The Minimultipart class is a container class that contains objects of type MimeBodyPartMultipart Mainpart =NewMimemultipart (); //Create a MimeBodyPart that contains HTML contentBodyPart HTML =NewMimeBodyPart (); Html.setcontent (Content.trim (),"Text/html; Charset=utf-8 ");        Mainpart.addbodypart (HTML);        Mailmessage.setcontent (Mainpart); Transport.send (mailmessage);
View Code

How to configure Java mail to send mail using QQ mailbox

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.