java邮件

3 篇文章 0 订阅

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="xxxxxxx@163.com"></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[] { "123456@qq.com" },
				new String[] { "12345678@qq.com" }, "fdasfasfjlsdjflasjfld");
		mailUtil.simpleTextMailByFreeMarker("frist mail", new String[] { "123456@qq.com" },
				new String[] { "12345678@qq.com" }, "安静下来");
	}

	public MailUtil getMailUtil() {
		return mailUtil;
	}

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

}

freemaker模板

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值