您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何在spring中使用maven發送郵件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
項目:
代碼塊
package com.baidu.action; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; /** * ----------------------------------------- * 文件: Email.java * 郵箱: fengemail04@sina.com * 描述: 發送Email工具類 * ----------------------------------------- */ public class Email { private MailSender mailSender; private SimpleMailMessage simpleMailMessage; /** * 方法名: sendMail * 參數名:@param subject 郵件主題 * 參數名:@param content 郵件主題內容 * 參數名:@param to 收件人Email地址 * 描述語: 發送郵件 */ public void sendMail(String subject, String content, String to) { simpleMailMessage.setSubject(subject); //設置郵件主題 simpleMailMessage.setTo(to); //設定收件人 simpleMailMessage.setText(content); //設置郵件主題內容 mailSender.send(simpleMailMessage); //發送郵件 } //Spring 依賴注入 public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) { this.simpleMailMessage = simpleMailMessage; } //Spring 依賴注入 public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } }
spring-smtp-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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <!-- 服務器 --> <property name="host" value="smtp.sina.com" /> <!-- 端口號 --> <property name="port" value="25" /> <!-- 用戶名 --> <property name="username" value="fengemail04@sina.com" /> <!-- 密碼 --> <property name="password" value="密碼需要在郵箱里設置,或者給郵箱權限開啟" /> <!-- SMTP服務器驗證 --> <property name="javaMailProperties"> <props> <!-- 驗證身份 --> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <!-- 目前我用過的EMAIL賬號都是網易的,下面列出網易的SMTP服務器名和端口號: 網易郵箱 SMTP服務器 SMTP端口 POP3服務器 POP3端口 @126.com smtp.126.com 25 pop3.126.com 110 @163.com smtp.163.com 25 pop3.163.com 110 @yeah.net smtp.yeah.net 25 pop3.yeah.net 110 --> <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"> <!-- 發件人email --> <property name="from" value="fengemail04@sina.com" /> </bean> <bean id="simpleMail" class="com.baidu.action.Email"> <property name="mailSender" ref="mailSender" /> <property name="simpleMailMessage" ref="simpleMailMessage" /> </bean> </beans>
EmailTest.java
package com.baidu.test; import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.baidu.action.Email; /** * ----------------------------------------- * 文件: EmailTest.java * 描述: Junit測試,運行將發送一封email * ----------------------------------------- */ public class EmailTest extends TestCase { public void testSendMail() { ApplicationContext context = new ClassPathXmlApplicationContext("spring-smtp-mail.xml"); Email mail = (Email)context.getBean("simpleMail"); mail.sendMail("標題", "內容", "fengemail05@sina.com"); //mail.sendMail("標題", "內容", "收件人郵箱"); } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.baidu.maven</groupId> <artifactId>mailtest02</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mailtest02 Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.13.RELEASE</version> </dependency> </dependencies> <build> <finalName>mailtest02</finalName> </build> </project>
看完上述內容,你們對如何在spring中使用maven發送郵件有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。