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>



發佈了59 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章