詳細Spring如何進行郵件發送
本文由樂字節Java架構課程獨家贊助播出java
建立 Maven 項目,在 pom.xml 文件中添加依賴spring
<!-- spring核心 jar 依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.4.RELEASE</version> </dependency> <!--spring 上下文環境 支持--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.4.RELEASE</version> <scope>test</scope> </dependency> <!-- Java Mail座標依賴 --> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency>
在 spring.xml 配置文件中設置郵件發送對應的bean標籤架構
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 開啓自動化掃描 --> <context:component-scan base-package="com.xxxx"/> <!-- 郵件發送器(提供了郵件發送接口、透明建立Java Mail的MimeMessage、及郵件發送的配置) --> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.163.com" /> <property name="port" value="25" /> <property name="defaultEncoding" value="utf-8"></property> <property name="username" value="用戶名"></property> <property name="password" value="密碼"></property> </bean> <!-- 普通文本郵件對象 --> <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="發件人的郵箱地址" /> <property name="subject" value="spring_mail" /> </bean> </beans>
定義接口ide
package com.xxxx.springMail; /** * 定義接口 */ public interface OrderManager { void placeOrder(); }
定義實現類測試
package com.xxxx.springMail; import org.springframework.mail.MailException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class SimpleOrderManager implements OrderManager { @Resource private JavaMailSenderImpl mailSender; @Resource private SimpleMailMessage templateMessage; @Override public void placeOrder() { // 獲取郵件對象 SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage); // 設置郵件收件人的郵箱地址 msg.setTo("收件人的郵箱地址"); // 設置郵件內容 msg.setText("Hello Spring Mail"); try{ // 發送郵件 this.mailSender.send(msg); } catch (MailException ex) { System.err.println(ex.getMessage()); } } }
/** * 發送郵件 * @param args */ public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml"); SimpleOrderManager simpleOrderManager = (SimpleOrderManager) ac.getBean("simpleOrderManager"); simpleOrderManager.placeOrder(); }
/** * 發送包含附件的郵件 * @param args * @throws MessagingException */ public static void main(String[] args) throws MessagingException { ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); JavaMailSender mailSender= (JavaMailSender) ac.getBean("mailSender"); MimeMessage message= mailSender.createMimeMessage(); message.setSubject("spring_mail"); // 郵件主題 // 建立帶有附件的消息幫組類 MimeMessageHelper helper = new MimeMessageHelper(message,true,"utf-8"); helper.setTo(new InternetAddress("收件人的郵箱地址"));//設置接收人 helper.setText("Thank you for ordering!"); // 郵件內容 helper.setFrom("發件人的郵箱地址"); // 發件人 // 設置附件 File file = new File("C:\\work\\郵件附件.txt"); // 添加附件 helper.addAttachment(file.getName(), file); // 發送郵件 mailSender.send(message); }