使用spring-email发邮件

package com.gumtreenet.util;

import java.util.Properties;

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

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

/**
* 发送email的工具
*
* @author 李义星
* @version 0.1
* @datetime 2011-03-30
*/
public class EmailUtil {
/**
* 发送html格式的email
*/
public static void sendEmail(Email email) {
// 建立发送者
JavaMailSenderImpl emailSender = new JavaMailSenderImpl();
MimeMessage mailMessage = emailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,
"utf-8");//同时设置了编码
Properties emailProperties = new Properties();

emailSender.setHost(email.getHost());
emailSender.setUsername(email.getUserName());
emailSender.setPassword(email.getPassword());
emailSender.setDefaultEncoding("utf-8");

if (email.isAuth()) {
emailProperties.setProperty("mail.smtp.auth", "true");
emailSender.setJavaMailProperties(emailProperties);
}

try {
messageHelper.setFrom(email.getFrom());
messageHelper.setTo(email.getTo());
messageHelper.setSubject(email.title);
messageHelper.setText(email.getText(), true);
} catch (MessagingException e) {
throw new RuntimeException(e);
}

// 发送消息
emailSender.send(mailMessage);
}

/**
* 发送纯文本格式的email
*/
public static void sendEmailText(Email email) {
// 建立发送者
JavaMailSenderImpl emailSender = new JavaMailSenderImpl();
SimpleMailMessage mailMessage = new SimpleMailMessage();
Properties emailProperties = new Properties();

emailSender.setHost(email.getHost());
emailSender.setUsername(email.getUserName());
emailSender.setPassword(email.getPassword());
emailSender.setDefaultEncoding("utf-8");

if (email.isAuth()) {
emailProperties.setProperty("mail.smtp.auth", "true");
emailSender.setJavaMailProperties(emailProperties);

}

mailMessage.setFrom(email.getFrom());
mailMessage.setTo(email.getTo());
mailMessage.setSubject(email.title);
mailMessage.setText(email.getText());

// 发送消息
emailSender.send(mailMessage);
}

/**
* 发送email时需要的信息
*
* @author 李义星
* @version 0.1
* @datetime 2011-03-30
*/
public static class Email {
private String host = "mail.gumtreenet.com"; // email发送服务器
private String userName = "[email protected]";
private String password = "*******************";
private String[] to = new String[] { "[email protected]",
"[email protected]", "[email protected]" };
private String from = "[email protected]";
private String title;
private String text;
private boolean auth = false;

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String[] getTo() {
return to;
}

public void setTo(String[] to) {
this.to = to;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public boolean isAuth() {
return auth;
}

public void setAuth(boolean auth) {
this.auth = auth;
}
}
}

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