發送郵件工具類html
package com.dayuanit.util; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.internet.MimeMessage; import java.util.Properties; /** * 使用spring工具類 */ public class EmailUtilSpring { private static String host = "smtp.qq.com"; //這裏是qq郵箱 ,能夠設置成其餘郵箱 private static String username = "2*******1@qq.com"; //發送者郵箱 private static String password ="yyo******no";//得到郵箱的受權碼 private static final int port = 465; //測試 public static void main(String[] args) { sendText("wen******@qq.com","ttt","vvvv"); } //指定接受郵箱、標題及內容的方法 public static boolean sendText(String email[], String title, String text) { try { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // 設定mail server senderImpl.setHost(host); senderImpl.setDefaultEncoding("UTF-8"); // 創建郵件消息 SimpleMailMessage mailMessage = new SimpleMailMessage(); // 設置收件人,寄件人 用數組發送多個郵件 // String[] array = new String[] // {"sun111@163.com","sun222@sohu.com"}; // mailMessage.setTo(array); mailMessage.setTo(email); mailMessage.setFrom(username); mailMessage.setSubject(title); mailMessage.setText(text); senderImpl.setUsername(username); // 根據本身的狀況,設置username senderImpl.setPassword(password); // 根據本身的狀況, 設置password Properties prop = new Properties(); prop.put("mail.smtp.auth", "true"); // 將這個參數設爲true,讓服務器進行認證,認證用戶名和密碼是否正確 prop.put("mail.smtp.timeout", "25000"); prop.setProperty("mail.smtp.port", Integer.toString(port));//設置端口 prop.setProperty("mail.smtp.socketFactory.port", Integer.toString(port));//設置ssl端口 prop.setProperty("mail.smtp.socketFactory.fallback", "false"); prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); senderImpl.setJavaMailProperties(prop); // 發送郵件 // senderImpl.send(mailMessage); senderImpl.send(mailMessage); System.out.println(" 郵件發送成功.. "); return true; } catch (Exception e) { e.printStackTrace(); return false; } } //發送HTML郵件 public static boolean sendHtml(String email[], String title, String html) { try { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // 設定mail server senderImpl.setHost(host); senderImpl.setDefaultEncoding("UTF-8"); // 創建郵件消息,發送簡單郵件和html郵件的區別 MimeMessage mailMessage = senderImpl.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage); // 設置收件人,寄件人 messageHelper.setTo(email); messageHelper.setFrom(username); messageHelper.setSubject(title); // true 表示啓動HTML格式的郵件 messageHelper.setText(html, true); senderImpl.setUsername(username); // 根據本身的狀況,設置username senderImpl.setPassword(password); // 根據本身的狀況, 設置password Properties prop = new Properties(); prop.put("mail.smtp.auth", "true"); // 將這個參數設爲true,讓服務器進行認證,認證用戶名和密碼是否正確 prop.put("mail.smtp.timeout", "25000"); prop.setProperty("mail.smtp.port", Integer.toString(port));//設置端口 prop.setProperty("mail.smtp.socketFactory.port", Integer.toString(port));//設置ssl端口 prop.setProperty("mail.smtp.socketFactory.fallback", "false"); prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); senderImpl.setJavaMailProperties(prop); // 發送郵件 senderImpl.send(mailMessage); System.out.println("郵件發送成功.."); return true; } catch (Exception e) { e.printStackTrace(); return false; } } public static boolean sendText(String email, String title, String text) { return sendText(new String[]{email}, title, text); } public static boolean sendHtml(String email, String title, String html) { return sendHtml(new String[]{email}, title, html); } }
在pom.xml文件中加上郵件的jar包java
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.5</version> </dependency>
獲郵箱的許可碼方法spring
一、打開qq郵箱,點擊設置apache
二、選擇帳號數組
三、點擊開啓pop3/smtp服務服務器
四、按步驟發送驗證(驗證方法有多種,可是個人只有一種)socket
五、保存該受權碼,設置到工具類便可工具