spring smtp_使用Spring使用Java发送电子邮件– GMail SMTP服务器示例
spring smtp
對于使用Java發(fā)送電子郵件, JavaMail API是標(biāo)準(zhǔn)解決方案。 如官方網(wǎng)頁所述,“ JavaMail API提供了獨立于平臺和協(xié)議的框架來構(gòu)建郵件和消息傳遞應(yīng)用程序”。 必需的類包含在JavaEE平臺中,但是要在獨立的JavaSE應(yīng)用程序中使用它,您必須從這里下載相應(yīng)的JAR。
注意:除非您使用的是Java SE 6,否則還需要提供javax.activation包的JavaBeans Activation Framework(JAF)擴展。 我們建議您使用最新版本的JAF 1.1.1版本。 JAF包含在Java SE 6中。
不幸的是,JavaMail可能有點笨拙,并且難以配置和使用。 如果您已經(jīng)為應(yīng)用程序擁抱了Spring框架 ,您將很高興發(fā)現(xiàn)Spring提供了一個郵件抽象層。 如參考文檔所述,“ Spring框架提供了一個有用的實用程序庫,用于發(fā)送電子郵件,該電子郵件使用戶免受底層郵件系統(tǒng)的限制,并負責(zé)代表客戶端進行低級資源處理。” 太好了,現(xiàn)在讓我們看看如何利用這個庫。
首先,創(chuàng)建一個名為“ SpringMailProject”的新Eclipse項目。 在項目的類路徑中,確保包括以下庫(請注意,使用的是3.0.2.RELEASE Spring版本):
- mail.jar(JavaMail的核心類)
- org.springframework.asm-3.0.2.RELEASE.jar
- org.springframework.beans-3.0.2.RELEASE.jar
- org.springframework.context.support-3.0.2.RELEASE.jar
- org.springframework.core-3.0.2.RELEASE.jar
- org.springframework.expression-3.0.2.RELEASE.jar
- com.springsource.org.apache.commons.logging-1.1.1.jar
注意1:需要Apache的commons-logging庫,該庫包含在classpath中
注意2:如果您使用的是Java 1.5或更早版本,則還需要Java激活框架。
我們將使用MailSender ,這是一個Spring接口,用于定義發(fā)送簡單郵件的策略。 由于這只是一個接口,因此我們需要一個具體的實現(xiàn),它以JavaMailSenderImpl的形式出現(xiàn)。 此類同時支持JavaMail MimeMessage和Spring SimpleMailMessage 。
我們將創(chuàng)建一個簡單的Spring服務(wù),該服務(wù)將用于發(fā)送郵件。 一種方法將在現(xiàn)場創(chuàng)建SimpleMailMessage ,而另一種方法將使用預(yù)配置的默認消息。 該服務(wù)的源代碼如下:
package com.javacodegeeks.spring.mail;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailSender; import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Service;@Service("mailService") public class MailService {@Autowiredprivate MailSender mailSender;@Autowiredprivate SimpleMailMessage alertMailMessage;public void sendMail(String from, String to, String subject, String body) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(from);message.setTo(to);message.setSubject(subject);message.setText(body);mailSender.send(message);}public void sendAlertMail(String alert) {SimpleMailMessage mailMessage = new SimpleMailMessage(alertMailMessage);mailMessage.setText(alert);mailSender.send(mailMessage);}}該類只是一個POJO,其服務(wù)名稱為“ mailService”,由構(gòu)造型Service批注標(biāo)記。 使用的bean接線策略是自動裝配的一種,因此使用了Autowired注釋。 注意,可以使用bean的名稱和類型來執(zhí)行自動裝配。
引導(dǎo)容器的相應(yīng)Spring 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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" ><context:component-scan base-package="com.javacodegeeks.spring.mail" />??? <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="host" value="smtp.gmail.com"/><property name="port" value="25"/><property name="username" value="myusername@gmail.com"/><property name="password" value="mypassword"/><property name="javaMailProperties"><props><!-- Use SMTP transport protocol --><prop key="mail.transport.protocol">smtp</prop><!-- Use SMTP-AUTH to authenticate to SMTP server --><prop key="mail.smtp.auth">true</prop><!-- Use TLS to encrypt communication with SMTP server --><prop key="mail.smtp.starttls.enable">true</prop><prop key="mail.debug">true</prop></props></property></bean><bean id="alertMailMessage" class="org.springframework.mail.SimpleMailMessage"><property name="from">??????????? <value>myusername@gmail.com</value></property><property name="to">??????????? <value>myusername@gmail.com</value></property><property name="subject" value="Alert - Exception occurred. Please investigate"/></bean></beans>這是一個非常簡單的Spring配置文件。 不過,有些事情要提:
- 聲明將從其開始上下文掃描的基本軟件包,并將其設(shè)置為“ com.javacodegeeks.spring.mail”。
- 聲明了“ mailSender” bean,并適當(dāng)設(shè)置了其一堆屬性(使用您自己的SMTP服務(wù)器配置屬性和憑據(jù))。
- “ alertMailMessage”是預(yù)先配置的Spring SimpleMailMessage,將使用“按名稱自動裝配”將其注入“ MailService”類中。
如果您希望使用Gmail的SMTP服務(wù)器,請確保正確配置了以下JavaMail屬性:
host=smtp.gmail.com port=25 username=your-gmail-username password=your-gmail-password mail.transport.protocol=smtp mail.smtp.auth=true mail.smtp.starttls.enable=true在開發(fā)階段,如果希望郵件客戶端生成信息日志,請將“ mail.debug”設(shè)置為true。 但是,請記住在生產(chǎn)時將其設(shè)置為false,因為日志量可能會降低應(yīng)用程序的性能和/或填充硬盤。
最后,我們創(chuàng)建一個類,該類將用于創(chuàng)建Spring容器并測試我們創(chuàng)建的簡單郵件服務(wù)。 源代碼如下:
package com.javacodegeeks.spring.mail;import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext;public class MailServiceTest {public static void main(String[] args) {ApplicationContext context = new FileSystemXmlApplicationContext("conf/spring.xml");MailService mailService = (MailService) context.getBean("mailService");mailService.sendMail("myusername@gmail.com", "myusername@gmail.com", "Testing123", "Testing only \n\n Hello Spring Email Sender");mailService.sendAlertMail("Exception occurred");}}FileSystemXmlApplicationContext類用作ApplicationContext 。 我們在構(gòu)造函數(shù)中傳遞Spring的XML文件位置,該類創(chuàng)建容器,實例化Bean并處理依賴項。 你不愛春天嗎? 接下來,我們使用“ getBean ”方法檢索“ MailService”服務(wù)的引用,并調(diào)用這兩個方法。
就這樣。 與往常一樣,您可以從此處下載完整的Eclipse項目。
相關(guān)文章 :- 依賴注入–手動方式
- 使用Spring Security保護GWT應(yīng)用程序的安全
- 具有Spring和Maven教程的JAX–WS
- 建立自己的GWT Spring Maven原型
- 使用Spring AspectJ和Maven進行面向方面的編程
- Spring 3 RESTful Web服務(wù)
- JBoss 4.2.x Spring 3 JPA Hibernate教程
翻譯自: https://www.javacodegeeks.com/2010/07/java-mail-spring-gmail-smtp.html
spring smtp
總結(jié)
以上是生活随笔為你收集整理的spring smtp_使用Spring使用Java发送电子邮件– GMail SMTP服务器示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线下报名 | YOCSEF TDS:深度
- 下一篇: vsomeip源码梳理 -- Event