使用Spring的Mail组件发送邮件
不想过多的做描述了,简单介绍一下,然后上代码,使用程序员标准语言进行沟通。
代码运行版本为Spring3.0.5,mail相关的包在org.springframework.context.support-3.0.5.RELEASE.jar这个JAR里,一下代码使用Gmail邮箱作为发送者,需要额外添加一个配置。OK,其他内容详见代码。(其实写在这是防止以后忘了,还要到网上到处搜)
package com.zywang.springmail;import java.io.File;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import java.util.Date;import java.util.Properties;import javax.activation.URLDataSource;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeUtility;import org.apache.commons.mail.EmailException;import org.springframework.core.io.UrlResource;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;public class SendMailBySpringMailDemo {/** * 创建邮件发送者 */private static JavaMailSender createJavaMailSender() {/*//从网上找的参考,说要添加这么多,实际测试了下,不需要这么多个Properties properties = new Properties();properties.setProperty("mail.debug", "true");properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");properties.setProperty("mail.smtp.socketFactory.fallback", "false");properties.setProperty("mail.smtp.socketFactory.port", "465");properties.setProperty("mail.smtp.port", "465");properties.setProperty("mail.smtp.auth", "true"); */Properties properties = new Properties();properties.setProperty("mail.debug", "true");//是否显示调试信息(可选)properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");properties.setProperty("mail.smtp.auth", "true");JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();javaMailSender.setJavaMailProperties(properties);javaMailSender.setUsername("zywang@gmail.com");javaMailSender.setPassword("111111");javaMailSender.setHost("smtp.gmail.com");javaMailSender.setPort(465);javaMailSender.setDefaultEncoding("UTF-8");return javaMailSender;}/** * 发送简单邮件 * SimpleMailMessage */public static void sendSimpleMailMessage() {JavaMailSender javaMailSender = createJavaMailSender();SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setFrom("zywang@gmail.com");//可选的mailMessage.setTo("zywang@iflysse.com");mailMessage.setSubject("Hello,你好啊");mailMessage.setText("那就随便说说吧" + new Date());javaMailSender.send(mailMessage);}/** * 发送MIME类型邮件 * MimeMessage */public static void sendMimeMessage() throws MessagingException {JavaMailSender javaMailSender = createJavaMailSender();MimeMessage mimeMessage = javaMailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);//可选的,可以用来修改显示给接收者的名字//helper.setFrom("zywang@gmail.com", "ZYWANG");helper.setTo("zywang@iflysse.com");helper.setSubject("这就是个实验");helper.setText("<h1>你好</h1>", true);javaMailSender.send(mimeMessage);}/** * 发送含附件的邮件 */public static void sendMimeMessageAttachment() throws MessagingException, UnsupportedEncodingException, MalformedURLException{JavaMailSender javaMailSender = createJavaMailSender();MimeMessage mimeMessage = javaMailSender.createMimeMessage();//第二个参数设置为true,表示允许添加附件MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);helper.setTo("zywang@iflysse.com");helper.setSubject("发送含图片附件的邮件");//第二个参数为true表示需要内容为HTML格式helper.setText("<h1>你好</h1>", true);//需要对文件名进行转码helper.addAttachment(MimeUtility.encodeText("王.png"), new File("d:\\王.png"));helper.addAttachment(MimeUtility.encodeText("Apache网站图标.gif"), new URLDataSource(new URL("http://www.apache.org/images/asf_logo_wide.gif")));javaMailSender.send(mimeMessage);}/** * 发送HTML格式含图片的邮件 */public static void sendMimeMessageInline() throws MessagingException, MalformedURLException{JavaMailSender javaMailSender = createJavaMailSender();MimeMessage mimeMessage = javaMailSender.createMimeMessage();//第二个参数设置为true,表示允许添加附件MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);helper.setTo("zywang@iflysse.com");helper.setSubject("发送含图片附件的邮件");//第二个参数为true表示需要内容为HTML格式helper.setText("<h1>你好</h1><br><img src='cid:fileId'><img src='cid:abc'>", true);helper.addInline("fileId", new File("d:\\王.png"));helper.addInline("abc", new UrlResource("http://www.apache.org/images/asf_logo_wide.gif"));javaMailSender.send(mimeMessage);}public static void main(String[] args) throws EmailException, UnsupportedEncodingException, MessagingException, MalformedURLException {//sendSimpleMailMessage();//sendMimeMessage();//sendMimeMessageAttachment();//sendMimeMessageInline();}}通常使用的时候只需要在配置文件中定义JavaMailSender,然后注入到需要使用的类中即可,上面的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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:jms="http://www.springframework.org/schema/jms"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"><bean id="javaMailSender" value="smtp.gmail.com"></property><property name="port" value="465"></property><property name="username" value="zywang2@gmail.com"></property><property name="password" value="111111"></property><property name="defaultEncoding" value="UTF-8"></property><property name="javaMailProperties"><props><prop key="mail.debug">true</prop><prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop><prop key="mail.smtp.auth">true</prop></props></property></bean></beans>
?