JAVA企业邮箱发送

地址:https://www.jianshu.com/p/da4d0137893a

 

java发送腾讯企业邮件-可用于发送邮箱验证码

96 Sanmeng 关注

 0.1 2018.01.29 09:43* 字数 405 阅读 1507评论 0喜欢 2

第一步:

申请腾讯企业邮箱账号

 

1.png

第二步:

登录 设置成员账号 成员账号进行登录发邮件

 

2.png

3.png

 

回到原来的登录界面使用刚刚设置的成员账号进行登录
java中也是用这个账号进行发邮件的

第三步:

可以查看官方提供的开发文档

 

4.png

5.png

6.png

7.png

第四步(忽略):

以下参数不需要修改(直接运行代码就可以 默认已经开启 ):

POP3/SMTP协议

接收邮件服务器:pop.exmail.qq.com ,使用SSL,端口号995
发送邮件服务器:smtp.exmail.qq.com ,使用SSL,端口号465
海外用户可使用以下服务器
接收邮件服务器:hwpop.exmail.qq.com ,使用SSL,端口号995
发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465

IMAP协议

接收邮件服务器:imap.exmail.qq.com ,使用SSL,端口号993
发送邮件服务器:smtp.exmail.qq.com ,使用SSL,端口号465
海外用户可使用以下服务器
接收邮件服务器:hwimap.exmail.qq.com ,使用SSL,端口号993
发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465

第五步:

不需要邮箱授权码 腾讯企业邮箱只需要账号密码就可以发送邮件

编写java代码:这里只是作为测试使用下面直接贴出我写代码 直接把账号密码改为你的就可以直接运行 需要导入mail.jar


    
  public static void main(String[] args) throws Exception {
        Properties prop = new Properties();
        //协议
        prop.setProperty("mail.transport.protocol", "smtp");
        //服务器
        prop.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
        //端口
        prop.setProperty("mail.smtp.port", "465");
        //使用smtp身份验证
        prop.setProperty("mail.smtp.auth", "true");
        //使用SSL,企业邮箱必需!
        //开启安全协议
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        //
        //获取Session对象
        Session s = Session.getDefaultInstance(prop,new Authenticator() {
            //此访求返回用户和密码的对象
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication pa = new PasswordAuthentication("你的账号", "密码");
                return pa;
            }
        });
        //设置session的调试模式,发布时取消
        s.setDebug(true);
        MimeMessage mimeMessage = new MimeMessage(s);
        try {
            mimeMessage.setFrom(new InternetAddress("你的账号","你的账号"));
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("接收账号"));
            //设置主题
            mimeMessage.setSubject("主题");
            mimeMessage.setSentDate(new Date());
            //设置内容
            mimeMessage.setText("正文内容");
            mimeMessage.saveChanges();
            //发送
            Transport.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        
    }
}

小礼物走一走,来简书关注我

赞赏支持

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值