Java send mail
163 email to 163 email, enterprise email, QQ email, etc
As mentioned in the last article java sends QQ mail , and Several ways to send mail ; in this article, we will talk about how to match all mail in the enhanced version.
The following Demo is to send a plain text email, but part of it is to send a formatted email. You can use it directly by releasing the comment part. You can send it to one person or multiple people.
package com.rn.common.util; import com.sun.mail.util.MailSSLSocketFactory; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @Description Send mail * @Author wuyu * @Date 2020-03-10 10:37 * @Version 1.0 */ public class SendEmail163 { public static void main(String[] args) { // configuration information Properties pro = new Properties(); pro.put("mail.smtp.host", "smtp.163.com"); pro.put("mail.smtp.auth", "true"); pro.put("mail.smtp.ssl.enable", "true"); try{ // SSL encryption MailSSLSocketFactory sf = null; sf = new MailSSLSocketFactory(); // Set up trust all hosts sf.setTrustAllHosts(true); pro.put("mail.smtp.ssl.socketFactory", sf); // According to the Session attribute of the message, construct a Session to send the message. Note that the user name cannot be suffixed, otherwise it is not the user name //It should also be noted that the password here is not the login password for normal use of the mailbox, but another special authorization code generated by the client MailAuthenticator authenticator = new MailAuthenticator("wuyu6945@163.com", "wuyu"); Session session = Session.getInstance(pro, authenticator); // Build mail information based on Session Message message = new MimeMessage(session); // Create mail sender address Address from = new InternetAddress("wuyu6945@163.com"); // Set sender of mail message message.setFrom(from); // Verify recipient email address /*List<String> toAddressList = new ArrayList<>(); toAddressList.add("liuruiting@feihe.com"); StringBuffer buffer = new StringBuffer(); if (!toAddressList.isEmpty()) { String regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; Pattern p = Pattern.compile(regEx); for (int i = 0; i < toAddressList.size(); i++) { Matcher match = p.matcher(toAddressList.get(i)); if (match.matches()) { buffer.append(toAddressList.get(i)); if (i < toAddressList.size() - 1) { buffer.append(","); } } } } String toAddress = buffer.toString();*/ String toAddress = "wuyu@runachina.com"; if (!toAddress.isEmpty()) { // Create the recipient address of the message Address[] to = InternetAddress.parse(toAddress); // Set mail recipient address message.setRecipients(Message.RecipientType.TO, to); // Mail theme message.setSubject("Ms. Wu(152452@163.com)adopt**Send mail"); // Mail container MimeMultipart mimeMultiPart = new MimeMultipart(); // Set HTML BodyPart bodyPart = new MimeBodyPart(); /*// Mail content String htmlText = "163 Send "; bodyPart.setContent(htmlText, "text/html;charset=utf-8"); mimeMultiPart.addBodyPart(bodyPart); // Add attachments List<String> fileAddressList = new ArrayList<String>(); fileAddressList.add("E:\\rain\\p\\xx.jpg"); if (fileAddressList != null) { BodyPart attchPart = null; for (int i = 0; i < fileAddressList.size(); i++) { if (!fileAddressList.get(i).isEmpty()) { attchPart = new MimeBodyPart(); // Attachment data source DataSource source = new FileDataSource(fileAddressList.get(i)); // Add attachment data source to message body attchPart.setDataHandler(new DataHandler(source)); // Set the attachment name as the original file name attchPart.setFileName(MimeUtility.encodeText(source.getName())); mimeMultiPart.addBodyPart(attchPart); } } } message.setContent(mimeMultiPart);*/ message.setText("java Mail test"); message.setSentDate(new Date()); // Save mail message.saveChanges(); // Send mail Transport.send(message); } }catch (Exception e){ e.printStackTrace(); } } }
How to apply for authorization code in e-mail
1. After logging in the email home page
2. Click Settings
3. Select POP3/SMTP/IMAP
4. Check POP3/SMTP service in the pop-up page
5. Set authorization code reminder will pop up, and click OK
6. Check start after setting authorization code position
7. After verifying the mobile number, enter the authorization code.
The authorization code is the client authorization code of the third party login.