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>
时间: 2024-08-10 02:11:49

java邮件的相关文章

java-基于JavaMail的Java邮件发送

1.基于JavaMail的Java邮件发送:简单邮件发送 2.基于JavaMail的Java邮件发送:复杂邮件发送

Java邮件发送与屏幕截屏

前几天七夕情人节孤独寂寞的程序猿闲来没事,花了一两个小时写了个小Demo主要实现Java的Mail发送功能和桌面截屏功能. 首先让我们先看看Java sendMail邮件发送和桌面屏幕截屏功能是怎么实现的基础知识. 一.Java  SendMail邮件发送 首先让我们来看看邮件发送的原理图: JavaMail 是一套sun 提供开发邮件收发程序API,JavaMail编写程序就是邮件客户端程序(和outlook.foxmail功能类似) * JavaMail开发需要类库 javamail API

新版本Java邮件发送类

之前曾经做过几个邮件发送类,有兴趣可以查阅前面的帖子. 使用过程中,发现一些不便之处,并作出了改进,将改进后的版本发布如下: 基类及其附属类共三个:(下载地址:http://pan.baidu.com/s/1bn1VkUN) import java.io.File; import java.util.Date; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; impo

Java邮件发送

package com.expai.test; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.Session; i

JAVA邮件发送的简单实现

JAVA邮件发送的简单实现 JAVA邮件发送的简单实现 JAVA MAIL是利用现有的邮件账户发送邮件的工具,比如说,我在网易注册一个邮箱账户,通过JAVA Mail的操控,我可以不亲自登录网易邮箱,让程序自动的使用网易邮箱发送邮件.这一机制被广泛的用在注册激活和垃圾邮件的发送等方面. JavaMail可以到http://www.oracle.com/technetwork/java/javamail/index-138643.html进行下载,并将mail.jar添加到classpath即可.

java邮件发送(含附件)

1. [代码]java邮件发送(含附件)疯狂的IT人站长整理的:利用Java发送邮件(含附件)的例子:1.邮件发送的配置propertity文件内容如下:(utils.properties文件放在src下面)emailsmtp=smtp.qq.com[email protected]emailpass=******2.读取配置文件的类文件(ReadPropertity.java)import java.io.IOException;import java.util.Properties;publ

基于JavaMail的Java邮件发送:简单邮件发送

http://blog.csdn.net/xietansheng/article/details/51673073 http://www.cnblogs.com/codeplus/archive/2011/10/30/2229391.html http://blog.csdn.net/ghsau/article/details/17839983 ******************** 电子邮件的应用非常广泛,例如在某网站注册了一个账户,自动发送一封欢迎邮件,通过邮件找回密码,自动批量发送活动信

java邮件发送(以163邮箱为例)

1.首先应该开通163邮箱的smtp和pop3,得到授权码 2.其次建立一个web项目,否则需要倒jar包mail.jar 3.创建一个类 4.注意:邮件内容必须为正式话语,否则系统会认为是垃圾邮件而拒收,报错541DT public static void main(String[] args) throws MessagingException { Properties prop=new Properties(); prop.put("mail.host","smtp.1

linux服务器报警之利用Java邮件报警

在这里分享下服务器如何利用Java邮件报警 前提:服务器先安装配置好java环境 一.JAVA环境安装 [[email protected] ~]# tar -zxvf jdk-7u75-linux-x64.gz -C /usr/local [[email protected] ~]# vi /etc/profile export JAVA_HOME=/usr/local/jdk1.7.0_75 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=

java邮件客户端

/*** *邮件VO **/package net.jk.util.email.vo; import java.util.Date; import java.util.List; import net.jk.app.model.App_emailfile; public class App_email { private String title; // 主题 private String fromaddr; // 发件人 private String toaddr; // 收件人 privat