使用Spring的Mail组件发送邮件

          明天就回家,晚上也没什么事情,弄个SpringMail组件发邮件,不说废话,直接上代码

 

package com.iflytek.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;

/**
 * @author xdwang
 * 
 * @create Apr 28, 2013 9:38:58 PM
 * 
 * @email:[email protected]
 * 
 * @description Spring Mail组件发送邮件
 * 
 */
public class SendMailBySpringMailDemo {

	/**
	 * @descrption 创建邮件发送者
	 * @author xdwang
	 * @create Apr 28, 201310:42:15 PM
	 * @return JavaMailSender
	 */
	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("[email protected]");
		javaMailSender.setPassword("xxxxxxxx");
		javaMailSender.setHost("smtp.gmail.com");
		javaMailSender.setPort(465);
		javaMailSender.setDefaultEncoding("UTF-8");
		return javaMailSender;
	}

	/**
	 * @descrption SimpleMailMessage发送简单邮件
	 * @author xdwang
	 * @create Apr 28, 201310:42:33 PM
	 */
	public static void sendSimpleMailMessage() {
		JavaMailSender javaMailSender = createJavaMailSender();

		SimpleMailMessage mailMessage = new SimpleMailMessage();
		mailMessage.setFrom("[email protected]");// 发送者,可选的
		mailMessage.setTo("[email protected]");//接受者  
		mailMessage.setSubject("Hello,xdwang");//主题
		mailMessage.setText("哈哈,明天回家咯" + new Date());//邮件内容

		javaMailSender.send(mailMessage);
	}

	/**
	 * @descrption MimeMessage发送MIME类型邮件
	 * @author xdwang
	 * @create Apr 28, 201310:42:40 PM
	 * @throws MessagingException
	 */
	public static void sendMimeMessage() throws MessagingException {
		JavaMailSender javaMailSender = createJavaMailSender();

		MimeMessage mimeMessage = javaMailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
		// 可选的,可以用来修改显示给接收者的名字
		// helper.setFrom("[email protected]", "xxxxxxxx");
		helper.setTo("[email protected]");
		helper.setSubject("this is test");
		helper.setText("<h1>Hello</h1>", true);

		javaMailSender.send(mimeMessage);
	}

	/**
	 * @descrption 发送含附件的邮件
	 * @author xdwang
	 * @create Apr 28, 201310:42:46 PM
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException
	 * @throws MalformedURLException
	 */
	public static void sendMimeMessageAttachment() throws MessagingException, UnsupportedEncodingException,
			MalformedURLException {
		JavaMailSender javaMailSender = createJavaMailSender();

		MimeMessage mimeMessage = javaMailSender.createMimeMessage();
		// 第二个参数设置为true,表示允许添加附件
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setTo("[email protected]");
		helper.setSubject("发送含图片附件的邮件");
		// 第二个参数为true表示需要内容为HTML格式
		helper.setText("<h1>Hello</h1>", true);
		// 需要对文件名进行转码
		helper.addAttachment(MimeUtility.encodeText("xdwang.jpg"), new File("E:\\软件大赛.jpg"));
		helper.addAttachment(MimeUtility.encodeText("baidu.gif"), new URLDataSource(new URL(
				"http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif")));

		javaMailSender.send(mimeMessage);
	}

	/**
	 * @descrption 发送HTML格式含图片的邮件
	 * @author xdwang
	 * @create Apr 28, 201310:42:56 PM
	 * @throws MessagingException
	 * @throws MalformedURLException
	 */
	public static void sendMimeMessageInline() throws MessagingException, MalformedURLException {
		JavaMailSender javaMailSender = createJavaMailSender();

		MimeMessage mimeMessage = javaMailSender.createMimeMessage();
		// 第二个参数设置为true,表示允许添加附件
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setTo("[email protected]");
		helper.setSubject("发送含图片附件的邮件");
		// 第二个参数为true表示需要内容为HTML格式
		helper.setText("<h1>Hello</h1><br><img src='cid:fileId'><img src='cid:abc'>", true);
		helper.addInline("fileId", new File("E:\\软件大赛.jpg"));
		helper.addInline("abc", new UrlResource("http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.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.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">
	<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="smtp.gmail.com"></property>
		<property name="port" value="465"></property>
		<property name="username" value="[email protected]"></property>
		<property name="password" value="xxxxxxxx"></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>

 

你可能感兴趣的:(java,spring,mail)