Java使用Spring發郵件

yb25 9年前發布 | 2K 次閱讀 Java FTP

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 {

@Autowired
private MailSender mailSender;
@Autowired
private 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);

}

}</pre>

用了spring就得配置xml,配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans&quot; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:p="http://www.springframework.org/schema/p&quot; xmlns:aop="http://www.springframework.org/schema/aop&quot; xmlns:context="http://www.springframework.org/schema/context&quot; xmlns:jee="http://www.springframework.org/schema/jee&quot; xmlns:task="http://www.springframework.org/schema/task&quot; xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd&quot; >

<context:component-scan base-package="cn.outofmemory.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></pre>

如果你要用Gmail發郵件,需要確認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
最后是測試代碼:

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");

}

}</pre>

 本文由用戶 yb25 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!