javaMailSend邮件

package utils.mails;


import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;


import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;


import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;


/**
 * 类MailsServiceImpl.java的实现描述:邮件操作工具实现类
 * 
 *
 */
public class MailsServiceImpl implements MailsService {


    private final Log      log = LogFactory.getLog(this.getClass());


    @Resource
    private JavaMailSender mailSender;


    /*
     * (non-Javadoc)
     * @see utils.mails.MailsService#sendMimeMessage(java.util.List, java.lang.String, java.lang.String,
     * java.lang.String, java.util.List)
     */
    @Override
    public void sendMimeMessage(final List<String> toAddressList, final String fromAddress, final String subject,
                                final String content, final List<String> fileList, final String contentType)
                                                                                                            throws Exception {
        try {
            // 扩展信息介质接口,实现接口中的方法
            MimeMessagePreparator mimeMail = new MimeMessagePreparator() {


                public void prepare(MimeMessage mimeMessage) throws MessagingException, UnsupportedEncodingException {
                    // 设置收信人地址
                    if (CollectionUtils.isNotEmpty(toAddressList)) {
                        for (String toAddress : toAddressList) {
                            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
                        }
                    } else {
                        return;
                    }
                    // 设置发件人地址
                    if (StringUtils.isNotBlank(fromAddress)) {
                        mimeMessage.setFrom(new InternetAddress(fromAddress));
                    } else {
                        return;
                    }
                    // 设置邮件主题
                    if (StringUtils.isNotBlank(subject)) {
                        mimeMessage.setSubject(subject);
                    }
                    // 多部件的,可以看做一个邮件容器,包含正文、附件等
                    Multipart multipart = new MimeMultipart();
                    // 添加正文
                    MimeBodyPart contentMimeBodyPart = new MimeBodyPart();
                    if (StringUtils.isNotBlank(content)) {
                        if (StringUtils.isNotBlank(contentType)) {
                            // contentMimeBodyPart.setContent(content, "text/html;charset=utf-8");
                            contentMimeBodyPart.setContent(content, contentType);
                        } else {
                            contentMimeBodyPart.setText(content);
                        }
                    }
                    multipart.addBodyPart(contentMimeBodyPart);
                    // 添加附件,可以添加多个附件
                    if (CollectionUtils.isNotEmpty(fileList)) {
                        for (String file : fileList) {
                            MimeBodyPart fileMimeBodyPart = new MimeBodyPart();
                            // 文件数据源
                            FileDataSource fds = new FileDataSource(file);
                            // 数据处理器
                            fileMimeBodyPart.setDataHandler(new DataHandler(fds));
                            // 设置文件名
                            fileMimeBodyPart.setFileName(MimeUtility.encodeText(fds.getName()));
                            multipart.addBodyPart(fileMimeBodyPart);
                        }
                    }
                    // 将Multipart添加MimeMessage
                    mimeMessage.setContent(multipart);
                    mimeMessage.setSentDate(new Date());
                }
            };
            // 发送邮件
            mailSender.send(mimeMail);
        } catch (Exception e) {
            log.error("toAddress:" + toAddressList.toString() + ",fromAddress:" + fromAddress + ",send mail fail:", e);
            throw new Exception("send mail fail");
        }
    }
}

转载于:https://my.oschina.net/u/586094/blog/125527

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值