JAVA MAIL 发送邮件(SSL加密方式,TSL加密方式)以及常见问题

类型服务器名称服务器地址(163为例)SSL协议端口非SSL协议端口号TSL协议端口
收件服务器POPpop.163.com995110 
收件服务器IMAPimap.163.com993143 
发件服务器SMTPsmtp.163.com465/99425587

 

腾讯企业邮箱服务器地址:smtp.exmail.qq.com

腾讯邮箱服务器地址:smtp.qq.com

常见问题:

1、如果出现454 Command not permitted when TLS active 错误,请检查你的邮件端口配置的是不是25端口,如果是,请改成465即可解决问题。

2、部分邮件提供商smtp登录密码不是账号密码,而是授权码,务必注意。使用SSL、TLS这个一定要注意。

3、550 用户被锁定:普通 163 邮箱是无法通过 smtp.163.com 发送邮件的,只有 163 VIP 邮箱才行,然后设置 mail.smtp.host=smtp.vip.163.com

4、454 Command not permitted when TLS active:需要设置 mail.smtp.starttls.enable=false

5、553 authentication is required:需要设置 mail.smtp.auth=true

6、550 Invalid User:from 必须写成带 @ 的邮件格式,且 username 要用 @ 前面的

SSL、TSL 发送邮件DEMO

package com.xx.util;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailTLS {
    public static void main(String[] args) {

        final String username = "xxx@qq.com";
        final String password = "xxxxxxxxxxxxxx";

        Properties props = new Properties();
        props.put("mail.smtp.auth", true);
        // TLS 需要这两行
        props.put("mail.smtp.starttls.enable", true);
        props.put("mail.smtp.port", "587");
        // SSL 需要这三行
//        props.put("mail.smtp.ssl.enable", true);
//        props.put("mail.smtp.port", "465");
//        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//        props.put("mail.smtp.host", "smtp.163.com");
        props.put("mail.smtp.host", "smtp.qq.com");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        try {
             Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("202888534@qq.com"));
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse("fenghongwang@goclouds.cn"));
                message.setSubject("Testing Subject1");
                message.setText("Dear Mail Crawler,"
                        + "\n\n No spam to my email, please!");

                Transport.send(message);
                System.out.println("Done");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

 

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值