javamail 发送邮件报错:Could not connect to SMTP host: smtp.163.com, port: 25 ,问题解决

java

package com.qcn.framework;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class EmailUtils {
    public static void main(String[] args) {
        //常用发送邮件demo1【部署到服务器会报错:Could not connect to SMTP host: smtp.163.com, port: 25】
//        Properties props = new Properties();
//        props.setProperty("mail.smtp.auth", "true");
//        props.setProperty("mail.transport.protocol", "smtp");
//        props.put("mail.smtp.host", "smtp.163.com");// smtp服务器地址
//        Session session = Session.getInstance(props);
//        session.setDebug(true);
//        Message msg = new MimeMessage(session);
//        try {
//            msg.setSubject("邮件标题");
//            msg.setText("邮件内容");
//            msg.setFrom(new InternetAddress("发件人"));
//            msg.setRecipient(Message.RecipientType.TO,
//                    new InternetAddress("收件人"));
//            msg.saveChanges();
//            Transport transport = session.getTransport();
//            transport.connect("发件人", "SDFA授权码FFHBF");
//            transport.sendMessage(msg, msg.getAllRecipients());
//            System.out.println("邮件发送成功!");
//            transport.close();
//        } catch (MessagingException e) {
//            System.out.println("邮件发送成功失败:"+e.getMessage());
//        }
        //正确的demo【解决部署到服务器后报错】
        try {
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            Properties props = new Properties();
            props.setProperty("mail.transport.protocol", "SMTP");
            props.setProperty("mail.host", "smtp.163.com");
            props.setProperty("mail.smtp.auth", "true");// 指定验证为true是否需要身份验证
            props.setProperty("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);
            Authenticator auth = new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("发件人邮箱名(不要@163.com)", "SDFA授权码FFHBF");
                }
            };
            Session session = Session.getInstance(props, auth);
            session.setDebug(true);
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("chihouzhineng@163.com"));
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("收件人"));
            message.setSubject("邮件标题");
            message.setContent("邮件内容", "text/html;charset=utf-8");
            Transport.send(message);
        } catch (GeneralSecurityException e) {
            System.out.println("异常1:" + e.getMessage());
        } catch (MessagingException e) {
            System.out.println("异常2:" + e.getMessage());
        }
    }
}

如果部署后还报错,就查看端口配置

登录阿里云开放163邮箱端口
163邮箱对应端口:官方
在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个异常`com.sun.mail.util.MailConnectException`表示无法连接到指定的主机和端口。根据你提供的错误信息,它显示你无法连接到主机`smtp.qq.com`的端口`456`。 可能的原因是: 1. 错误的主机名或端口号:请确保你提供的主机名和端口号是正确的。在使用QQ邮箱的SMTP服务器时,通常使用端口号`465`或`587`,而不是`456`。 2. 网络连接问题:检查你的网络连接是否正常,确保能够访问该主机和端口。有时候防火墙或网络配置会阻止连接。 3. 邮箱服务器设置问题:请确保你已正确设置了QQ邮箱的SMTP服务器地址、端口号以及用户名和密码。你可以参考QQ邮箱提供的设置指南。 以下是一个示例代码片段,用于使用JavaMail发送电子邮件到QQ邮箱: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { final String username = "your_email@qq.com"; final String password = "your_password"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.qq.com"); props.put("mail.smtp.port", "465"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient_email@example.com")); message.setSubject("Test Email"); message.setText("This is a test email."); Transport.send(message); System.out.println("Email sent successfully!"); } catch (MessagingException e) { e.printStackTrace(); } } } ``` 请将`your_email@qq.com`替换为你的QQ邮箱地址,`your_password`替换为你的QQ邮箱密码,以及`recipient_email@example.com`替换为收件人的邮箱地址。确保你的QQ邮箱已启用SMTP服务,并使用正确的SMTP服务器地址和端口号。 如果问题仍然存在,请检查你的网络连接和邮箱服务器设置,并确保网络能够正常连接到QQ邮箱的SMTP服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值