java发送html邮件正文变附件,Spring 4发送HTML/纯文件邮件并且携带邮件附件

applicationContext文件配置内容如下:

true

剩下的,我就直接捞干的了。。。。。。。。。。。

import java.io.File;

/**

* 邮件对象

* @author liuhuan

* Created by Administrator on 2014/10/24.

*/

public class EmailVo

{

/**

* 邮件发送者

*/

private String sender;

/**

* 邮件接收者

*/

private String [] receivers;

/**

* 邮件抄送人

*/

private String [] cc;

/**

* 邮件抄送人

*/

private String [] bcc;

/**

* Email发送的内容

*/

private String emailContent;

/**

* 邮件主题

*/

private String subject;

/**

* 邮件附件

*/

private File [] attachFile;

public String getSender()

{

return sender;

}

public void setSender(String sender)

{

this.sender = sender;

}

public String getEmailContent()

{

return emailContent;

}

public void setEmailContent(String emailContent)

{

this.emailContent = emailContent;

}

public String getSubject()

{

return subject;

}

public void setSubject(String subject)

{

this.subject = subject;

}

public File[] getAttachFile()

{

return attachFile;

}

public void setAttachFile(File[] attachFile)

{

this.attachFile = attachFile;

}

public String[] getReceivers()

{

return receivers;

}

public void setReceivers(String[] receivers)

{

this.receivers = receivers;

}

public String[] getCc()

{

return cc;

}

public void setCc(String[] cc)

{

this.cc = cc;

}

public String[] getBcc()

{

return bcc;

}

public void setBcc(String[] bcc)

{

this.bcc = bcc;

}

}

邮件发送接口类:

import com.sdyc.app.newmedia.vo.EmailVo;

import javax.mail.MessagingException;

import java.util.Date;

/**

* Email通知消息发送

* @author liuhuan

* Created by Administrator on 2014/10/23.

*/

public interface EmailNotifyService

{

/**

* 发送简单文本Email消息

* @param emailVo

*/

public void sendEmailMessageOfSimpleText(EmailVo emailVo, Date date);

/**

* 发送HTML格式的消息

* @param emailVo

* @param date

*/

public void sendEmailMessageOfHtmlText(EmailVo emailVo , Date date) throws MessagingException;

/**

* 带附件并且html格式邮件发送,带附件并简单文本格式邮件发送

* @param emailVo

*/

public void sendEmailMessageOfAttachedFileAndSimpleText(EmailVo emailVo, Date date, boolean isHtmlText) throws MessagingException;

}

邮件发送实现:

import com.sdyc.app.newmedia.service.EmailNotifyService;

import com.sdyc.app.newmedia.vo.EmailVo;

import org.apache.commons.lang.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.core.io.FileSystemResource;

import org.springframework.mail.SimpleMailMessage;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.mail.javamail.MimeMessageHelper;

import org.springframework.stereotype.Service;

import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

import java.io.File;

import java.util.Date;

/**

* Email发送

* @author Rayn

* Created by Administrator on 2014/10/23.

*/

@Service

public class EmailNotifyServiceImpl implements EmailNotifyService

{

@Autowired

private JavaMailSender emailTemplate;

public void setEmailTemplate(JavaMailSender emailTemplate)

{

this.emailTemplate = emailTemplate;

}

/**

* 发送简单文本Email消息

* @param emailVo

*/

@Override

public void sendEmailMessageOfSimpleText(EmailVo emailVo, Date date)

{

SimpleMailMessage simpleTextMessage = new SimpleMailMessage();

simpleTextMessage.setFrom(emailVo.getSender());

simpleTextMessage.setTo(emailVo.getReceivers());

if(emailVo.getBcc().length > 0)

{

simpleTextMessage.setBcc(emailVo.getBcc());

}

if(emailVo.getCc().length > 0)

{

simpleTextMessage.setCc(emailVo.getCc());

}

simpleTextMessage.setText(emailVo.getEmailContent());

if(null == date)

{

date = new Date();

}

simpleTextMessage.setSentDate(date);

emailTemplate.send(simpleTextMessage);

}

/**

* 带附件并简单文本格式邮件发送

*

* @param emailVo

*/

@Override

public void sendEmailMessageOfHtmlText(EmailVo emailVo , Date date) throws MessagingException

{

MimeMessage message = emailTemplate.createMimeMessage();

MimeMessageHelper helper = new MimeMessageHelper(message);

helper.setFrom(emailVo.getSender());

helper.setValidateAddresses(true);

if(StringUtils.isNotBlank(emailVo.getEmailContent()))

{

helper.setText(emailVo.getEmailContent(), true);

}

helper.setSubject(emailVo.getSubject());

helper.setCc(emailVo.getCc());

helper.setTo(emailVo.getReceivers());

helper.setBcc(emailVo.getBcc());

if(null == date)

{

date = new Date();

}

helper.setSentDate(date);

emailTemplate.send(message);

}

/**

* 带附件并且html格式邮件发送,HTML格式的消息

* @param emailVo

* @param date

*/

@Override

public void sendEmailMessageOfAttachedFileAndSimpleText(EmailVo emailVo, Date date, boolean isHtmlText) throws MessagingException

{

MimeMessage message = emailTemplate.createMimeMessage();

MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setFrom(emailVo.getSender());

//helper.setValidateAddresses(true);

helper.setText(emailVo.getEmailContent(), isHtmlText);

helper.setSubject(emailVo.getSubject());

helper.setCc(emailVo.getCc());

helper.setTo(emailVo.getReceivers());

helper.setBcc(emailVo.getBcc());

if(null == date)

{

date = new Date();

}

helper.setSentDate(date);

for(File file : emailVo.getAttachFile())

{

FileSystemResource fileSystemResource = new FileSystemResource(file);

helper.addAttachment(file.getName(), fileSystemResource);

}

emailTemplate.send(message);

}

}

邮件发送测试:

@Test

public void ss( ) throws Exception

{

EmailVo emailVo = new EmailVo();

emailVo.setReceivers(new String[]{"liuwei412552703@163.com"});

emailVo.setCc(new String[]{});

emailVo.setBcc(new String[]{});

emailVo.setSubject("测试");

emailVo.setSender("raynTest@163.com");

emailVo.setEmailContent("

中国人民

测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试
");

File [] f = new File[]{new File("E:/test11111.txt"), new File("E:/test2.txt")};

emailVo.setAttachFile(f);

emailNotifyService.sendEmailMessageOfHtmlText(emailVo, new Date());

emailNotifyService.sendEmailMessageOfAttachedFileAndSimpleText(emailVo, null, true);

}

最后上结果截图:

2450d4750a9ecebbbcd1b7d130a9c231.png

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页