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