SSH发送邮件

利用Spring开源包发邮件

 

包括:文本邮件、HTML邮件、图片邮件、附件邮件。

  用到Mail包: javax.mail.*

 

#邮件服务器地址
mail.smtp.host=smtp.163.com
#邮件服务器是否需要验证用户存在与否 ,使用“true”、“false”、“0”、“1” 
mail.smtp.auth=true
#默认服务器端用户名
name=***
#默认服务器端密码
password=***
#默认主机地址
from=zhangyueran@server
#默认昵称
nickname=***
 

 

<?xml version="1.0" encoding="GBK"?>
<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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<context:component-scan base-package="com.zhangyueran.mail" />

	<context:property-placeholder location="classpath:mail.properties" />
	<bean id="mailService" class="com.bwie.resume.account.service.MailService" init-method="init">
		<property name="mailInfo">
			<props>
				<prop key="mail.smtp.host">${mail.smtp.host}</prop>
				<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
				<prop key="name">${name}</prop>
				<prop key="password">${password}</prop>
				<prop key="from">${from}</prop>
				<prop key="nickname">${nickname}</prop>
			</props>
		</property>
	</bean>
</beans>

 

package com.zhangyueran.mail.service;

import javax.mail.Message;
import javax.mail.NoSuchProviderException;
import javax.mail.Transport;
import javax.mail.Session;

import java.util.Properties;

import javax.mail.internet.AddressException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
import javax.mail.MessagingException;

public class MailService {
	//得到Properties 
	private Properties mailInfo;

	private Session session;

	public void init() {
		// 建立会话  spring配置的初始化方法
		session = Session.getDefaultInstance(getMailInfo());
	}

	// 参数1.获取收件人2.标题3.内容
	public void sendMail(String to, String subjetc, String content) throws MessagingException, Exception {
		// 昵称转换中文编码
		String nickName = MimeUtility.encodeText("昵称");
		// 获取消息
		Message message = setMessage(to, subjetc, content, nickName);
		//登陆邮件服务器进行用户验证
		setTransport(message);
	}

	private void setTransport(Message message) throws NoSuchProviderException, MessagingException {
		Transport transport = session.getTransport("smtp");
		//连接
		transport.connect(getMailInfo().getProperty("host"), getMailInfo().getProperty("name"), getMailInfo().getProperty("password"));
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
	}

	private Message setMessage(String to, String subjetc, String content, String nickName) throws MessagingException, AddressException {
		// 建立消息
		Message message = new MimeMessage(session);
		// 设置发件人
		message.setFrom(new InternetAddress(nickName + "<" + getMailInfo().getProperty("from") + ">"));
		// 收件人
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
		// 邮件主题
		message.setSubject(subjetc);
		// 邮件内容(text/html)
		message.setContent(content, "text/html;charset = gbk");
		// 保存邮件内容
		message.saveChanges();
		return message;
	}

	public void setMailInfo(Properties mailInfo) {
		this.mailInfo = mailInfo;
	}

	public Properties getMailInfo() {
		return mailInfo;
	}
}

 

package com.zhangyueran.mail.Service;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MailServiceTest {

	@Test
	public void send() throws Throwable {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
		MailService mailService = context.getBean(MailService.class);
		mailService.sendMail("收件人邮箱***", "标题", "内容");
		context.close();
	}
}

 

 

你可能感兴趣的:(java,spring)