spring mail 发送邮件

最近在项目中用到了发送邮件的功能(用户注册邮箱激活、用户密码重置邮箱获取验证码等等),所以写了一下java的邮件发送。

java mail

java mail是最早出现的java邮件发送,以下是它的使用方式(引入jar包:activation.jar、mail.jar)
package com.lingjuli.servlet;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class SendMail {
	private static String username = "[email protected]";
	private static String password = "xxx";
	private static String smtpServer = "smtp.163.com";//邮件协议
	private static String fromMailAddress = "[email protected]";
	private static String toMailAddress = "[email protected]";

	public static void main(String[] args) throws Exception {
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.host", smtpServer);
		// 获得邮件会话对象
		Session session = Session.getDefaultInstance(props,
				new SmtpAuthenticator(username, password));
		/** *************************************************** */
		// 创建MIME邮件对象
		MimeMessage mimeMessage = new MimeMessage(session);
		mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 发件人
		mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
				toMailAddress));// 收件人
		mimeMessage.setSubject("主题");
		mimeMessage.setSentDate(new Date());// 发送日期
		BodyPart mdp = new MimeBodyPart();// 新建一个存放信件内容的BodyPart对象
		mdp.setContent("测试java邮件发送", "text/html;charset= GB2312");// 设置发送邮件内容为HTML类型,并为中文编码
		Multipart mm = new MimeMultipart();
		mm.addBodyPart(mdp);
		mimeMessage.setContent(mm);
		mimeMessage.saveChanges();
		Transport.send(mimeMessage);// 发送邮件

	}

}
/**
 * Smtp认证
 */
class SmtpAuthenticator extends Authenticator {
	String username = null;
	String password = null;

	// SMTP身份验证
	public SmtpAuthenticator(String username, String password) {
		this.username = username;
		this.password = password;
	}

	public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(this.username, this.password);
	}

}


spring mail 

通过spring对java mail的上层封装,以下为使用方式

package com.zwx.utils;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailSender {
	/*
	private static String username = "[email protected]";
	private static String password = "xxx";
	private static String smtpServer = "smtp.163.com";
	private static String fromMailAddress = "[email protected]";
	private static String toMailAddress = "[email protected]";
	*/
	private static PropertiesLoader propertiesLoader = new PropertiesLoader("bdsc-web.properties");//读取配置文件
	public static void mailSimple(String toMailSAddress,String subject,String content) {
		// 发送器  
		JavaMailSenderImpl mailSender= new JavaMailSenderImpl();
		// 建立邮件消息,发送简单邮件和html邮件的区别
		MimeMessage mailMessage = mailSender.createMimeMessage();
		// 为防止乱码,添加编码集设置
		MimeMessageHelper messageHelper = null;
		try {
			//发送附件 则 参数为 multipart 为 ture
			messageHelper = new MimeMessageHelper(mailMessage,true,"UTF-8");
		} catch (MessagingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//设定mail server
		mailSender.setHost(propertiesLoader.getProperty("mail.smtp.host"));
		mailSender.setPort(propertiesLoader.getInteger("mail.smtp.port"));
		mailSender.setUsername(propertiesLoader.getProperty("mail.smtp.username"));
		mailSender.setPassword(propertiesLoader.getProperty("mail.smtp.password"));
		//建立邮件消息
		//设置收件人、寄件人
        try {
			messageHelper.setTo(toMailSAddress);
			messageHelper.setFrom(propertiesLoader.getProperty("mail.smtp.username"));
	        messageHelper.setSubject(subject);
	        messageHelper.setText(content,true);
	        //附件内容
	        messageHelper.addAttachment("附件1", new File("h:/test/abc.pdf"));
	        messageHelper.addAttachment("附件2", new File("h:/test/qwe.gif"));
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//发送邮件
        mailSender.send(mailMessage);
		System.out.println("邮件发送成功!");
		
	}
	public static void main(String[] args) {
		try {
			MailSender.mailSimple("[email protected]","qwe","qwe");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

bdsc-web.properties

#spring mail
mail.smtp.username = [email protected]
mail.smtp.password = xxx
mail.smtp.host = smtp.163.com
mail.smtp.auth = true
mail.smtp.timeout = 1000


你可能感兴趣的:(java,spring,mail,邮件发送)