• java使用QQ邮件发送


    import org.apache.commons.collections4.CollectionUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.io.File;
    import java.io.UnsupportedEncodingException;
    import java.util.Date;
    import java.util.List;
    import java.util.Properties;
    
    /**
     * @description:
     * @date:2022/6/10 10:00
     */
    public class MailSenderUtils {
    
        private final static Logger log = LoggerFactory.getLogger(MailSenderUtils.class);
    
        public static void sendMail(MailServerConfiguration mailServerConfiguration, MailSendInfo mailSendInfo){
            Message message = null;
            Session session = null;
    
            try {
                //定义邮箱服务器配置
                Properties properties = new Properties();
                properties.put("mail.transport.protocol",mailServerConfiguration.getProtocol());
                properties.put("mail.smtp.ssl.enable",true);
                //邮件服务器地址
                properties.put("mail.smtp.host",mailServerConfiguration.getServerHost());
                //邮件服务器端口
                properties.put("mail.smtp.port",mailServerConfiguration.getServerPort());
                //邮件服务器认证属性
                properties.put("mail.smtp.auth",mailServerConfiguration.getFlag());
                //身份认证类
                UserPwdMailAuthenticator authenticator = new UserPwdMailAuthenticator(mailServerConfiguration.getUserName(),mailServerConfiguration.getUserPwd());
                //创建session
                session = Session.getDefaultInstance(properties,authenticator);
                //开启Session的debug模式
                session.setDebug(true);
                //创建message邮件对象
                message = new MimeMessage(session);
                Address from = new InternetAddress(mailServerConfiguration.getFromAddress());
                //设置发送方的邮箱地址
                message.setFrom(from);
                //设置发送时间
                message.setSentDate(new Date());
    
                List toAddresses = mailSendInfo.getToAddress();
                if(CollectionUtils.isNotEmpty(toAddresses)){
                    Address[] addresses = new Address[toAddresses.size()];
                    for(int i = 0 ; i < toAddresses.size(); i++){
                        Address address1 = new InternetAddress(toAddresses.get(i));
                        addresses[i] = address1;
                    }
                    //设置接收方的邮件地址
                    message.setRecipients(Message.RecipientType.TO,addresses);
                    //甚至邮件主题
                    message.setSubject(mailSendInfo.getSubject());
                    //创建多媒体对象容器
                    Multipart multipart = new MimeMultipart();
                    //创建正文内容
                    BodyPart bodyPart = new MimeBodyPart();
                    bodyPart.setContent(mailSendInfo.getContent(),"text/html;charset=utf-8");
                    multipart.addBodyPart(bodyPart);
    
                    //获取附件文件
                    List files = mailSendInfo.getFiles();
                    if(CollectionUtils.isNotEmpty(files)){
                        for(File file : files){
                            if(file.exists()){
                                // 如果附件存在,创建附件对象
                                BodyPart attachPart = new MimeBodyPart();
                                attachPart.setDataHandler(new DataHandler(new FileDataSource(file)));
                                // 设置文件名 (解决附件名乱码)
                                attachPart.setFileName(MimeUtility.encodeText(file.getName()));
                                // 添加附件 (将附件内容添加到多媒体对象容器中)
                                multipart.addBodyPart(attachPart);
                            }
                        }
                    }
                    message.setContent(multipart);
                    Transport.send(message);
                }
    
            } catch (AddressException e) {
                log.error("邮件发送失败,错误原因:" + e.getMessage());
            } catch (UnsupportedEncodingException e) {
                log.error("邮件发送失败,错误原因:" + e.getMessage());
            } catch (NoSuchProviderException e) {
                log.error("邮件发送失败,错误原因:" + e.getMessage());
            } catch (MessagingException e) {
                log.error("邮件发送失败,错误原因:" + e.getMessage());
            }
    
        }
    }
    

    #邮件服务器
    audaque.mail.host=smtp.qq.com
    #邮件服务器端口
    audaque.mail.port=465
    ###邮箱号
    audaque.mail.username=XX.qq.com
    ###邮箱密码或邮箱授权码,qq邮箱发送短信获取16位数的授权码
    audaque.mail.password=XX
    #传输协议
    audaque.mail.protocol=stmp

    QQ邮箱发送。也可以使用腾讯企业邮箱,因为是收费版,只需要密码不需要授权码,qq就需要授权码,其他类邮箱类似可以配置

  • 相关阅读:
    node.js安装及环境配置
    STM32单片机—定时器产生PWM波
    Docker容器无法启动
    C. Division(分解质因数)
    微信小程序wxs标签 在wxml文件中编写JavaScript逻辑
    【英语:语法基础】B3.核心语法-形副冠数词
    Mac环境下jupyter添加nbextension插件
    网络爬虫中selenium和requests这两个工具有什么区别呢?
    【OpenCV 例程 300篇】247. 特征检测之最大稳定极值区域(MSER)
    R语言线性回归模型拟合诊断异常值分析家庭燃气消耗量和卡路里实例带自测题
  • 原文地址:https://blog.csdn.net/u012269637/article/details/126382021