java邮件

Java代码

package com.zy.mail;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;

import freemarker.template.Template;
import freemarker.template.TemplateException;

@Component(value = "mailUtil")
public class MailUtil {
	private static Log log = LogFactory.getLog(MailUtil.class);
	// freeMaker配置
	private FreeMarkerConfigurer freeMarkerConfigurer;
	private JavaMailSenderImpl javaMailSenderImpl;

	/**
	 * 
	 * @param subject
	 *            主题
	 * @param to
	 *            发送人
	 * @param cc
	 *            抄送人
	 * @param text
	 *            邮件文本内容
	 */
	public void simpleTextMail(final String subject, final String[] to,
			final String[] cc, final String text) {
		MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
		try {
			MimeMessageHelper messageHelper = new MimeMessageHelper(
					mimeMessage, true);
			messageHelper.setFrom(javaMailSenderImpl.getUsername());
			messageHelper.setTo(to);
			messageHelper.setCc(cc);
			messageHelper.setSubject(subject);
			messageHelper.setText(text);
			javaMailSenderImpl.send(mimeMessage);
			System.out.println("Send mail successfully!");

		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 利用freemaker模板制作邮件
	 * @param subject 主题
	 * @param to 发送人
	 * @param cc 抄送
	 * @param text 内容
	 */
	public void simpleTextMailByFreeMarker(final String subject,
			final String[] to, final String[] cc, final String text) {
		MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
		try {
			MimeMessageHelper messageHelper = new MimeMessageHelper(
					mimeMessage, true);
			Template template = null;
			try {
				template = freeMarkerConfigurer.getConfiguration().getTemplate(
						"mail.flt");
				Map model = new HashMap();
				model.put("text", text);
				try {
					String content = FreeMarkerTemplateUtils
							.processTemplateIntoString(template, model);
					messageHelper.setFrom(javaMailSenderImpl.getUsername());
					messageHelper.setTo(to);
					messageHelper.setCc(cc);
					messageHelper.setSubject(subject);
					messageHelper.setText(content, true);//设置成html
					javaMailSenderImpl.send(mimeMessage);
					System.out.println("Send mail successfully!");

				} catch (TemplateException e) {
					e.printStackTrace();
				}
				System.out.println(template);
			} catch (IOException e) {
				log.error(e.getMessage(),e);
			}

		} catch (MessagingException e) {
			log.error(e.getMessage(),e);
		}

	}

	public FreeMarkerConfigurer getFreeMarkerConfigurer() {
		return freeMarkerConfigurer;
	}

	@Autowired
	public void setFreeMarkerConfigurer(
			FreeMarkerConfigurer freeMarkerConfigurer) {
		this.freeMarkerConfigurer = freeMarkerConfigurer;
	}

	public JavaMailSenderImpl getJavaMailSenderImpl() {
		return javaMailSenderImpl;
	}

	@Autowired
	public void setJavaMailSenderImpl(JavaMailSenderImpl javaMailSenderImpl) {
		this.javaMailSenderImpl = javaMailSenderImpl;
	}

}

配置文件

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="javaMailSenderImpl" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<!--  邮件发送服务设置 -->
		<property name="host"  value="smtp.163.com"></property>
		<property name="username" value="[email protected]"></property>
		<property name="password" value="password"></property>
		<property name="defaultEncoding" value="UTF-8" />
	</bean>
</beans>
</pre><pre name="code" class="html"><?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="classpath:/template"></property>
		<property name="freemarkerSettings">
			<props>
				<prop key="template_update_delay">0</prop>
				<prop key="default_encoding">UTF-8</prop>
				<prop key="locale">zh_CN</prop>
			</props>
	</property>
	</bean>
</beans>

测试

package mail;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.zy.mail.MailUtil;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "../applicationContext.xml" })
public class MailUtilTest extends AbstractJUnit4SpringContextTests {

	private MailUtil mailUtil;

	@Test
	public void testSimpleTextMail() {
		mailUtil.simpleTextMail("frist mail", new String[] { "[email protected]" },
				new String[] { "[email protected]" }, "fdasfasfjlsdjflasjfld");
		mailUtil.simpleTextMailByFreeMarker("frist mail", new String[] { "[email protected]" },
				new String[] { "[email protected]" }, "安静下来");
	}

	public MailUtil getMailUtil() {
		return mailUtil;
	}

	@Autowired
	public void setMailUtil(MailUtil mailUtil) {
		this.mailUtil = mailUtil;
	}

}

freemaker模板

<html>
<head>
<title>测试邮件</title>
</head>
${text}
</body>
</html>



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。