Spring 邮件接收发送实例

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

import com.lh.entity.MessageInfo;

public class EmailUtil {
	private JavaMailSender mailSender;//注入Spring E-mail抽象层的发送邮件对象
	public JavaMailSender getMailSender() {
		return mailSender;
	}
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}
	public  void doSend(MessageInfo msg){				
		SimpleMailMessage message = new SimpleMailMessage();//创建邮件对象		
		message.setFrom(msg.getFrom());//设置发送者地址
		message.setTo(msg.getTo());//设置多个接收者地址		
		message.setSubject(msg.getSubject());//设置主题
		message.setSentDate(msg.getSendDate());//设置发送时间
		message.setText(msg.getMsg());		//设置消息内容
		JavaMailSenderImpl sender = (JavaMailSenderImpl)mailSender;//邮件发送对象
		
		sender.setHost(msg.getServerHost());//设置邮件主机地址
		sender.setUsername(msg.getFrom());//设置邮箱用户名
		sender.setPassword(msg.getPassword());//设置密码	
		sender.send(message);//发送邮件
	}
}
/*********************MessageInfo****************/

import java.util.Date;

public class MessageInfo {
	private String serverHost="";
	private String password;
	private String from = "";
	private String [] to ;
	private Date sendDate;
	private String subject;
	private String msg="";
	private String bcc = "";
	private String cc="";
	
	public String getServerHost() {
		return serverHost;
	}
	public void setServerHost(String serverHost) {
		this.serverHost = serverHost;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getBcc() {
		return bcc;
	}
	public void setBcc(String bcc) {
		this.bcc = bcc;
	}
	public String getCc() {
		return cc;
	}
	public void setCc(String cc) {
		this.cc = cc;
	}
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}

	public String[] getTo() {
		return to;
	}
	public void setTo(String[] to) {
		this.to = to;
	}
	public Date getSendDate() {
		return sendDate;
	}
	public void setSendDate(Date sendDate) {
		this.sendDate = sendDate;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
}
 

 

你可能感兴趣的:(spring)