參考資料:https://blog.csdn.net/zqz_zqz/article/details/80250654html
spring+javamailjava
依賴spring
<!-- javamail --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.4.RELEASE</version> </dependency>
配置文件api
applicationContent-mail.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <description>JavaMail配置文件</description> <!-- 加載屬性文件 --> <context:property-placeholder location="classpath*:properties/*.properties"/> <!-- 配置一個簡單郵件對象 --> <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="${mail.fromAddr}"></property> </bean> <!-- 郵件的發送對象 --> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.smtp_host}"></property> <property name="username" value="${mail.from_username}"></property> <property name="password" value="${mail.authorization_code}"></property> <property name="defaultEncoding" value="${mail.defaultEncoding}"></property> <!-- 郵件發送相關的配置信息 --> <property name="javaMailProperties" > <props> <prop key="mail.smtp.auth">${mail.smtp_auth}</prop> <prop key="mail.debug">${mail.debug}</prop> <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop> </props> </property> </bean> </beans>
屬性文件服務器
mail.properties #郵箱登陸名 mail.from_username=123456@qq.com/123456@163.com #郵箱POP3/SMTP受權碼 mail.authorization_code=qwertyuhgfvdcwre #郵箱帳戶 mail.fromAddr=123456@qq.com/123456@163.com #郵箱服務器 mail.smtp_host=smtp.qq.com/smtp.163.com #服務器是否驗證用戶的身份信息(普通郵件參數) mail.smtp_auth=true #打印debug信息 mail.debug=true #郵件延遲發送:毫秒 mail.smtp.timeout=0 #郵件字符編碼 mail.defaultEncoding=UTF-8 #郵件發送協議 mail.transport=smtp #郵件附件(附件郵件參數) mail.image.path=C:\\Users\\as\\Desktop\\11234rtgf.png
代碼session
代碼 @Resource private SimpleMailMessage mailMessage; @Resource private JavaMailSender mailSender; @Value("${mail.from_username}") private String from_username; @Value("${mail.authorization_code}") private String authorization_code; @Value("${mail.fromAddr}") private String fromAddr; @Value("${mail.smtp_host}") private String smtp_host; @Value("${mail.smtp_auth}") private String smtp_auth; @Value("${mail.transport}") private String transport; @Value("${mail.image.path}") private String path;
demo1app
// 發送註冊普通郵件(spring + javamail.jar + mail.properties) new Thread(new Runnable() { String subject = "郵件主題"; String content = "郵件內容"; @Override public void run() { mailMessage.setTo(model.getUserinfo().getEmail()); // 郵件接收人 mailMessage.setSubject(subject); // 郵件主題 mailMessage.setText(content); // 郵件內容 mailSender.send(mailMessage); // 發送郵件 } }).start();
demo2ide
// 發送附件郵件(spring + javamail.jar + mail.properties) new Thread(new Runnable() { @Override public void run() { String subject = "郵件主題"; String content = "<html><head></head><body><h1>附件郵件</h1><a href='#'>超連接</a><img src=cid:image></body></html>"; // 郵件內容,帶有附件 try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(fromAddr); // 郵件發送方 helper.setSubject(subject); // 郵件主題 helper.setText(content, true); // 郵件內容與有幾件內容解析方式:true(html),false(text) helper.setTo(model.getUserinfo().getEmail()); // 郵件接收方 FileSystemResource resource = new FileSystemResource(path); // 郵件附件 // "image" == (src=cid:xxx) helper.addInline("image", resource); // 方式附件郵件 mailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } } }).start();
javamail編碼
jar包spa
dsn.jar
imap.jar
mailapi.jar
pop3.jar
smtp.jar
MailUtil
package com.erp.star.shine.utils; import java.util.Map; import java.util.Properties; import javax.mail.Address; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; public class MailUtil { public static void sendMessage(Map<String, String> map) throws Exception{ Properties props = new Properties(); props.put("mail.smtp.host", map.get("smtp_host"));//指定郵件的發送服務器地址 // props.put("mail.smtp.host", "smtp.163.com");//指定郵件的發送服務器地址 props.put("mail.smtp.auth", map.get("smtp_auth"));//服務器是否要驗證用戶的身份信息 // props.put("mail.smtp.auth", "true");//服務器是否要驗證用戶的身份信息 Session session = Session.getInstance(props);//獲得Session session.setDebug(true);//表明啓用debug模式,能夠在控制檯輸出smtp協議應答的過程 //建立一個MimeMessage格式的郵件 MimeMessage message = new MimeMessage(session); //設置發送者 Address fromAddress = new InternetAddress(map.get("fromAddr"));//郵件地址 // Address fromAddress = new InternetAddress("xxx@163.com");//郵件地址 message.setFrom(fromAddress);//設置發送的郵件地址 //設置接收者 Address toAddress = new InternetAddress(map.get("toAddr"));//郵件地址 message.setRecipient(RecipientType.TO, toAddress);//設置接收者的地址 //設置郵件的主題 message.setSubject(map.get("subject")); //設置郵件的內容 message.setText(map.get("content")); //保存郵件 message.saveChanges(); //獲得發送郵件的對象 Transport transport = session.getTransport(map.get("transport")); // Transport transport = session.getTransport("smtp"); //鏈接到服務器上 transport.connect(map.get("fromAddr"),map.get("from_username"),map.get("mail.authorization_code")); // transport.connect("smtp.163.com","username","password"); //發送 transport.sendMessage(message, message.getAllRecipients()); //關閉通道 transport.close(); } }
demo3
// 發送普通郵件(javamail.jar + MailUtil.java + mail.properties) new Thread(new Runnable() { String subject = "郵件主題"; String content = "郵件內容"; @Override public void run() { Map<String, String> map = new HashMap<String, String>(); map.put("fromAddr", fromAddr); map.put("toAddr", model.getUserinfo().getEmail()); map.put("subject", subject); map.put("content", content); map.put("from_username", from_username); map.put("authorization_code", authorization_code); map.put("smtp_host", smtp_host); map.put("smtp_auth", smtp_auth); map.put("transport", transport); try { MailUtil.sendMessage(map); } catch (Exception e) { e.printStackTrace(); } } }).start();
*爲何是開線程發送郵件?
郵件發送的結果對主線程的繼續執行沒有必然關聯關係,若是在主線程發送郵件,1.影響程序效率,2.發送郵件若是異常,會致使主程序異常,顯然這並不符合常理