對于使用Java發送電子郵件, JavaMail API是標準解決方案。 如官方網頁所述,“ JavaMail API提供了獨立于平臺和協議的框架來構建郵件和消息傳遞應用程序”。 必需的類包含在JavaEE平臺中,但是要在獨立的JavaSE應用程序中使用它,您必須從這里下載相應的JAR。
注意:除非您使用Java SE 6,否則還需要提供javax.activation包的JavaBeans激活框架(JAF)擴展。 我們建議您使用最新版本的JAF 1.1.1版本。 JAF包含在Java SE 6中。
不幸的是,JavaMail可能有點笨拙,并且難以配置和使用。 如果您已經為應用程序擁抱了Spring框架 ,那么您將很高興發現Spring提供了一個郵件抽象層。 如參考文檔所述,“ Spring框架提供了一個有用的實用程序庫,用于發送電子郵件,該電子郵件使用戶免受底層郵件系統的限制,并負責代表客戶端進行低級資源處理。” 太好了,現在讓我們看看如何利用這個庫。
首先,創建一個名為“ 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接口,用于定義發送簡單郵件的策略。 由于這只是一個接口,因此我們需要一個具體的實現,并以JavaMailSenderImpl的形式出現。 此類同時支持JavaMail MimeMessage和Spring SimpleMailMessage 。
我們將創建一個簡單的Spring服務,該服務將用于發送郵件。 一種方法將在現場創建SimpleMailMessage ,而另一種方法將使用預配置的默認消息。 該服務的源代碼如下:
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,其服務名稱為“ mailService”,由構造型Service批注標記。 使用的bean接線策略是自動裝配之一 ,因此使用了Autowired注釋。 注意,可以使用bean的名稱和類型來執行自動裝配。
引導容器的相應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配置文件。 不過,有些事情要提及:
- 聲明從其開始上下文掃描的基本軟件包,并將其設置為“ com.javacodegeeks.spring.mail”。
- 聲明了“ mailSender” bean,并適當設置了其一堆屬性(使用您自己的SMTP服務器配置屬性和憑據)。
- “ alertMailMessage”是預配置的Spring SimpleMailMessage,將使用“按名稱自動裝配”將其注入“ MailService”類中。
如果您希望使用Gmail的SMTP服務器,請確保正確配置了以下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
在開發階段,如果希望郵件客戶端生成信息性日志,請將“ mail.debug”設置為true。 但是,請記住在生產時將其設置為false,因為日志量可能會降低應用程序的性能和/或填充硬盤。
最后,我們創建一個類,該類將用于創建Spring容器并測試我們創建的簡單郵件服務。 源代碼如下:
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 。 我們在構造函數中傳遞Spring的XML文件位置,該類創建容器,實例化Bean并處理依賴項。 你不愛春天嗎? 接下來,我們使用“ getBean ”方法檢索“ MailService”服務的引用,并調用這兩個方法。
就這樣。 與往常一樣,您可以從此處下載完整的Eclipse項目。
- 依賴注入–手動方式
- 使用Spring Security保護GWT應用程序
- 帶有Spring和Maven教程的JAX–WS
- 建立自己的GWT Spring Maven原型
- 使用Spring AspectJ和Maven進行面向方面的編程
- Spring3 RESTful Web服務
- JBoss 4.2.x Spring 3 JPA Hibernate教程
翻譯自: https://www.javacodegeeks.com/2010/07/java-mail-spring-gmail-smtp.html