Java Mail

 

public class EmailSender implements Runnable{
	
	private Log log = LogFactory.getLog(this.getClass());
	
	private String from;
	private String to;
	private String subject;
	private String content;
	
	private MailSender mailSender;
	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}
	
	public EmailSender(){
	}
	
	public EmailSender(String from, String to, String subject, String content){
		this.from = from;
		this.to = to;
		this.subject = subject;
		this.content = content;
	}
	
	

	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 String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Override
	public void run() {
		try {
			MimeMessage msg = ((JavaMailSenderImpl)mailSender).createMimeMessage();  
			MimeMessageHelper helper = new MimeMessageHelper(msg, true, "GB2312");
			helper.setTo(to);
			helper.setFrom(from); 
			helper.setSubject(subject); 
			helper.setText(content, true); 
			((JavaMailSenderImpl)mailSender).send(msg);
		} catch (Exception e) {
			log.error("邮件发送失败", e);
		}
	}
	
	public void send(){
		Thread mailThread = new Thread(this);
		mailThread.start();
	}
	
}

 

	<!-- 邮件发送 -->
	<bean id="mailSender"
		class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="mail.sina.com"></property>
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
				<prop key="mail.smtp.port">465</prop>
				<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
				<prop key="mail.smtp.socketFactory.fallback">false</prop>
				<prop key="mail.smtp.timeout">25000</prop>
			</props>
		</property>
		<property name="username" value="sy"></property>
		<property name="password" value="sy123"></property>
	</bean>
	
	<bean name="emailSender" class="com.st.core.utils.EmailSender" scope="prototype">
	    <property name="mailSender" ref="mailSender"/>
	    <property name="from" value="[email protected]"/>
	    <property name="subject" value="[管理系统异常提醒]"/>
	</bean>

 

	private EmailSender emailSender;
	
	public void setEmailSender(EmailSender emailSender) {
		this.emailSender = emailSender;
	}

		String s = "[email protected]";
		emailSender.setTo(s);
		emailSender.setContent("ABCDEFGHI");
		emailSender.send();

 

<property name="emailSender" ref="emailSender">

 

发送多人:[email protected],[email protected],[email protected] 逗号分隔

public class EmailSender implements Runnable{
	
	private Log log = LogFactory.getLog(this.getClass());
	
	private String from;
	private String to;
	private String subject;
	private String content;
	
	private MailSender mailSender;
	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}
	
	public EmailSender(){
	}
	
	public EmailSender(String from, String to, String subject, String content){
		this.from = from;
		this.to = to;
		this.subject = subject;
		this.content = content;
	}

	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 String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Override
	public void run() {
		try {
			MimeMessage msg = ((JavaMailSenderImpl)mailSender).createMimeMessage();  
			MimeMessageHelper helper = new MimeMessageHelper(msg, true, "GB2312");
			//多个收件人
			if (to != null && to.trim().length() > 0) {
				String[] arr = to.split(",");
				int receiverCount = arr.length;
				
				if (receiverCount > 0) {
					InternetAddress[] address = new InternetAddress[receiverCount];
					for (int i = 0; i < receiverCount; i++) {
						address[i] = new InternetAddress(arr[i]);
					}
					helper.setTo(address);
				}
				helper.setFrom(from); 
				helper.setSubject(subject); 
				helper.setText(content, true); 
				((JavaMailSenderImpl)mailSender).send(msg);
			}else{
				log.error("发送邮件失败,没有获取到收件人");
			}
		} catch (Exception e) {
			log.error("邮件发送失败", e);
		}
	}
	
	public void send(){
		Thread mailThread = new Thread(this);
		mailThread.start();
	}
	
}

 

你可能感兴趣的:(java mail)