配置邮件服务mail

ApplicationContext.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.xsd"
>
    <!-- 配置MailSender -->

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com" />
        <property name="port" value="465" />
        <property name="username" value="livebookstore" />
        <property name="password" value="LiVe-BoOkStOrE" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
            </props>
        </property>
    </bean>

</beans>

package example.chapter9;

import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class Main {

    public static void main(String[] args) throws Exception {
        // 直接使用JavaMail API:
        Properties props = new Properties();
        // 设置SMTP服务器:
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.auth", "true");
        // 使用SSL安全连接:
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        send(
            props,
            "livebookstore", // Username
            "LiVe-BoOkStOrE", // Password
            "[email protected]", // From
            new String[] {"[email protected]"}, // To
            "A test mail", // Subject
            "测试使用JavaMail API发送邮件" // Text
        );
//-----------------------------------------------------------------Spring发送文本文件----------------------------------------
        // 使用Spring提供的MailSender:
        ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
        JavaMailSender mailSender = (JavaMailSender)context.getBean("mailSender");
        // 创建一个纯文本邮件:
        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setFrom("[email protected]");
        mail.setTo("[email protected]");
        mail.setSubject("Another test mail");
        mail.setText("测试使用Spring MailSender发送邮件");
        // 发送:
        mailSender.send(mail);

//-----------------------------------------------------------------Spring发送带两个附件的邮件----------------------------------------
        // 发送Mime邮件:
        MimeMessage mime = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mime, true, "UTF-8");
        helper.setFrom("[email protected]");
        helper.setTo("[email protected]");
        helper.setSubject("Test mime mail");
        // 设定为HTML格式:
        helper.setText("<html><body>访问Live在线书店:<br>"
                + "<a href='http://www.livebookstore.net' target='_blank'>"
                + "<img src='cid:logo'></a></body></html>",
                true);
        helper.addInline("logo", new ClassPathResource("logo.gif"));
        helper.addAttachment("freebsd.gif", new ClassPathResource("freebsd.gif"));
        // 发送:
        mailSender.send(mime);
    }
//-------------------------------------------------------------普通邮件调用的方法-----------------------------------------------------
    public static void send(Properties props, final String username, final String password, String from, String[] to, String subject, String text) throws MessagingException {
        Session session = Session.getInstance(props, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        session.setDebug(true);
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setSubject(subject);
        message.setText(text);
        message.setSentDate(new Date());
        Address[] addressTo = new Address[to.length];
        for(int i=0; i<to.length; i++) {
            addressTo[i] = new InternetAddress(to[i]);
        }
        message.setRecipients(Message.RecipientType.TO, addressTo);
        message.saveChanges();
        Transport.send(message, addressTo);
    }

}



你可能感兴趣的:(spring,.net,xml,FreeBSD,Gmail)