The code is similar to the one in Netease's mailbox, except that the smtp server address is different, and the code for "enable SSL encryption" needs to be added.
package com.wjl.mail.utils; import java.security.GeneralSecurityException; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.sun.mail.util.MailSSLSocketFactory; /** * Tools for sending emails * QQ mailboxes send messages to each other */ public class MailUtils { private static String userName = "XXXX";//QQ mailbox account for sending emails private static String password = "XXXX";//Authorization code private static String userName2 = "XXXXX";//QQ mailbox account for receiving mail private static String port = "465";//465、587 /** * This method is used to send mail * @param to: who to email * **/ public static void sendMain(String to) throws AddressException, MessagingException, GeneralSecurityException{ //1. Create a connection object and connect to the mailbox server Properties props = new Properties(); //Enable debug debugging props.setProperty("mail.debug", "true"); //The stmp server needs to perform authentication, that is, the verification of the username and password, so as to pass the verification props.setProperty("mail.smtp.auth", "true"); // send mail protocol name props.setProperty("mail.transport.protocol", "smtp"); //Set the mail server hostname props.setProperty("mail.host", "smtp.qq.com");//Set as the outgoing server of qq: (do not use smtp.exmail.qq.com) //Set the port number (this configuration can be written or not) props.setProperty("mail.smtp.port", port); //Authorization code props.setProperty("mail.smtp.password",password); //Enable SSL encryption, otherwise it will fail MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); //Authenticator: Authentication information Session session = Session.getInstance(props, new Authenticator(){ @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName,password);//Use it to send emails to other accounts } }); //2, create the mail object Message message = new MimeMessage(session); //2.1 Set the sender message.setFrom(new InternetAddress(userName)); //2, 2 set the recipient message.setRecipient(RecipientType.TO, new InternetAddress(to)); //2.3 Subject of the email message.setSubject("Test message"); //2.4 The body of the email (ie the content of the email) message.setContent("Test mail:javaMail-QQ mailbox test","text/html;charset=utf-8"); //3. Send email Transport trans = session.getTransport(); //connect to mail server trans.connect(userName, password); //send email trans.sendMessage(message, message.getAllRecipients()); //close the connection trans.close(); //Transport.send(message); (both ways are possible) System.out.println("Successfully sent"); } public static void main(String[] args) { try { sendMain(userName2); } catch (Exception e) { e.printStackTrace (); } } }
Code to enable SSL encryption:
//Enable SSL encryption, otherwise it will fail MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf);
Add a QQ email account to FoxMail to view emails:
Attention :
1. The mailbox must open SMTP, obtain the authorization code (how to obtain it? Click me ), and use the latest authorization code to send emails.
2. The code to enable SSL encryption must be added, otherwise an error will be reported.
3. When adding a QQ mailbox account in Foxmail, an authorization code (not a QQ mailbox password) must be used, and "SSL" must be checked, otherwise the account will fail to be added.
There are too many problems encountered in the process of getting QQ mailbox, the next article will write about the problem, so I won't talk about it here.