首页 > 代码库 > java邮件

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 = http://www.mamicode.com/"mailUtil")>
配置文件

<?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=http://www.mamicode.com/"smtp.163.com">>
</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=http://www.mamicode.com/"classpath:/template">>
测试

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>



java邮件