• javax.mail发送邮件功能


    java发送邮件

    package com.cinc.messageservice.message.mail.util;
    
    import com.cinc.messageservice.message.mail.dto.MailDto;
    import com.cinc.messageservice.utils.EmptyUtils;
    import lombok.extern.slf4j.Slf4j;
    
    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.Date;
    import java.util.Properties;
    
    /**
     * @Author: hhr
     * @Despriction: 邮件发送
     * @CreatedTime: 2020/3/12 10:07
     * @ModifyBy:
     * @ModifyTime:
     * @ModifyDespriction:
     * @Version: V1.0.0
     */
    @Slf4j
    public class MailUtil {
    
        public boolean sendMail(Boolean isSingle, MailDto mailDto) throws MessagingException {
    
            //初始化
            Session session = this.authenticationMail();
    
            try {
                //设置消息
                MimeMessage message = getMimeMessage(isSingle, session, mailDto);
                //发送
                Transport transport = session.getTransport();
                transport.connect(mailDto.getMailAccount(), mailDto.getMailLicense());
                transport.sendMessage(message, message.getAllRecipients());
    
                transport.close();
    
            }catch (MessagingException e){
                e.printStackTrace();
                return false;
            }
    
            return true;
        }
    
        /**
         *  验证信息认证
         * @return
         * @throws MessagingException
         */
        private Session authenticationMail() throws MessagingException {
            Session session;
            try {
                Properties props = new Properties();
                //设置用户的认证方式
                props.setProperty("mail.smtp.auth", "true");
                //设置传输协议
                props.setProperty("mail.transport.protocol", "smtp");
                //设置发件人的SMTP服务器地址
                props.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
                props.put("mail.smtp.ssl.enable", "true"); //qq的ssl加密
                session = Session.getInstance(props);
                session.setDebug(true);
            } catch (Exception e) {
                e.printStackTrace();
                throw new MessagingException("认证失败");
            }
            return session;
        }
    
        /**
         * @param isSingle 是否单发
         *                 <P>true-向指定的一个收件人发送邮件
         *                 <P>false-向多个收件人群发邮件
         *                 <P>群发时多个收件人之间用英文逗号','分割
         * @param mailDto 邮件实体
         */
        private MimeMessage getMimeMessage(Boolean isSingle, Session session, MailDto mailDto)
                throws MessagingException {
            if (EmptyUtils.isStringEmpty(mailDto.getMailTiTle())) {
                throw new MessagingException("邮件标题不能为空");
            }
            if (EmptyUtils.isStringEmpty(mailDto.getMailContent())) {
                throw new MessagingException("邮件内容不能为空");
            }
    
            MimeMessage message = new MimeMessage(session);
            try {
                //设置发件人地址
                message.setFrom(new InternetAddress(mailDto.getMailAccount()));
            } catch (MessagingException e) {
                throw new MessagingException("发件人地址错误");
            }
            /*
              设置收件人地址
              MimeMessage.RecipientType.TO:发送
              MimeMessage.RecipientType.CC:抄送
              MimeMessage.RecipientType.BCC:密送
             */
            if (isSingle) {
                message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(mailDto.getRecipients()));
            } else {
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailDto.getRecipients()));
            }
            //设置抄送人
            if (!EmptyUtils.isStringEmpty(mailDto.getCopyRecipients())){
                message.setRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(mailDto.getCopyRecipients()));
            }
    
            //设置邮件标题
            message.setSubject(mailDto.getMailTiTle(), "UTF-8");
            //设置邮件内容
            message.setContent(mailDto.getMailContent(), "text/html;charset=UTF-8");
            //设置附件
            if (!EmptyUtils.isStringEmpty(mailDto.getMailFilePath())){
                try {
                    Multipart multipart = new MimeMultipart();
                    BodyPart part = new MimeBodyPart();
                    // 加入附件
                    FileDataSource ds = new FileDataSource(mailDto.getMailFilePath());
                    part.setDataHandler(new DataHandler(ds));
                    part.setFileName(MimeUtility.encodeText(ds.getName()));
                    multipart.addBodyPart(part);
                    // 设置邮件内容
                    message.setContent(multipart);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            message.setSentDate(new Date());
            return message;
        }
    
    
    }
    

      

    package com.cinc.messageservice.message.mail.dto;
    
    import lombok.Data;
    
    /**
     * @Author: hhr
     * @Despriction: 邮件实体
     * @CreatedTime: 2020/3/12 9:59
     * @ModifyBy:
     * @ModifyTime:
     * @ModifyDespriction:
     * @Version: V1.0.0
     */
    @Data
    public class MailDto {
    
        /**
         * 邮件标题
         */
        private String mailTiTle;
        /**
         * 邮件内容
         */
        private String mailContent;
        /**
         * 附件路径
         */
        private String mailFilePath;
        /**
         * 收件人(们)-(多个人邮箱号逗号分隔)
         */
        private String recipients;
        /**
         * 抄送人(们)-(多个人邮箱号逗号分隔)
         */
        private String copyRecipients;
        /**
         * 发件人账号
         */
        private String mailAccount;
        /**
         * 发件人授权码
         */
        private String mailLicense;
        /**
         * 发件人是否启用
         */
        private String isUse;
    }
    

      

  • 相关阅读:
    svn上传文件钩子
    linux服务器版svn安装
    csp-s模拟55
    csp-s模拟54
    csp-s模拟53
    csp-s模拟52
    csp-s模拟51
    csp-s模拟50
    csp-s模拟49
    csp-s模拟48
  • 原文地址:https://www.cnblogs.com/HHR-SUN/p/12469092.html
Copyright © 2020-2023  润新知