java springboot+maven发送邮件

springboot+maven发送邮件

废话不多说直接上代码

1. pom 文件导入jar包

       <!--邮件发送-->
       <dependency>
           <groupId>javax.mail</groupId>
           <artifactId>mail</artifactId>
           <version>1.4.7</version>
       </dependency>

 

2. 邮件方法 我用的是163 邮箱发送

 

/**
 * 项目文件根路径
 * @return
 */
 public static String rootPath() {

    return System.getProperty("user.dir");
}}

 

 

 // 发送邮件
 public static Boolean sendEmail() {
        final Properties props = new Properties();
        //登入邮箱服务器是需要验证的
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.163.com");
        props.put("mail.smtp.port", 25);
        //设置协议
        props.put("mail.transport.protocol", "smtp");
        // 发件人的账号
        props.put("mail.user", "ddddddddd@163.com");
        // 访问SMTP服务时需要提供的密码 非常重要 不是你登
陆邮箱的密码 是需要到163 邮箱设置的SMTP的密码
        props.put("mail.password", "mima");
        // 构建授权信息,用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息,创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
//        mailSession.setDebug(true);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        try {
String nick = "";
 nick = javax.mail.internet.MimeUtility.encodeText("title");
// 设置发件人
  InternetAddress from = new InternetAddress(nick + " <"+
    props.getProperty("mail.user") + ">");
            message.setFrom(from);
            Address[] a = new Address[1];
            // 接收方回复的邮件地址
            a[0] = new InternetAddress("eeeeeee@qq.com");
            message.setReplyTo(a);
            // 设置收件人
            InternetAddress to = new InternetAddress("shoujianren@qq.com");
            message.setRecipient(MimeMessage.RecipientType.TO, to);
            // 设置邮件标题
            message.setSubject("mailtitle");

            //添加附件部分
            //邮件内容部分1---文本内容
            MimeBodyPart body0 = new MimeBodyPart(); //邮件中的文字部分
            body0.setContent("<p>啦啦啦啦</p>","text/html;charset=utf-8");

            //邮件内容部分2---附件1
            MimeBodyPart body1 = new MimeBodyPart(); //附件1
            body1.setDataHandler( new DataHandler( new FileDataSource
(UlegalZCUtil.rootPath() +
 File.separator + "pdf" + File.separator + "templateOL" + ".pdf")) )
;//./代表项目根目录下

            body1.setFileName( MimeUtility.encodeText("拉拉.pdf") 
);//中文附件名,解决乱码

            //把上面的3部分组装在一起,设置到msg中
            MimeMultipart mm = new MimeMultipart();
            mm.addBodyPart(body0);
            mm.addBodyPart(body1);
            message.setContent(mm);

// 设置邮件的内容体
// message.setContent("题在我使用postman来上传图片时候
,死活都没过。。显示这个,问题在哪呢?",
 "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
        }
        catch (Exception e) {
            String err = e.getMessage();
            // 在这里处理message内容, 格式是固定的
            System.out.println("====:"+err);
            return false;
        }
        return true;
    }

  3. 如果是qq邮箱的话需要在上面的配置添加ssl加密

//开启了 SSL 加密
        //开启安全协议
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

 4. 经过的我的实验如果将项目部署到阿里云服务器,以163邮箱 为基准发送邮件

的话是不能成功的,以为163邮箱是25端口与阿里冲突,

   后期我以qq邮箱为基准发送邮件,但是163邮箱接收不到邮件,目前还没有找到解决办法

   我的想法是采用阿里云邮箱,应该没有问题。。。

 

 

注意 密码不是邮箱的登陆密码

转载于:https://www.cnblogs.com/memoryXudy/p/7680610.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值