基于JavaMail的邮件发送及报错javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, po解决方案

import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
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;

import com.ctrip.vo.Orderbylicailipractice;
import com.sun.mail.util.MailSSLSocketFactory;
/**
 * Created by liuqi on 2019/7/17.
 */


public class SendMail {

        public static void sendmail(List listOrder) throws GeneralSecurityException
        {
            // 收件人电子邮箱
            String to = "[email protected]";

            // 发件人电子邮箱
            String from = "[email protected]";

            // 指定发送邮件的主机为 smtp.qq.com
            String host = "smtp.qq.com";  //QQ 邮件服务器

            // 获取系统属性
            Properties properties = System.getProperties();
           
            // 设置邮件服务器
            properties.setProperty("mail.smtp.host", host);

            properties.put("mail.smtp.auth", "true");
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);
            // 获取默认session对象
            Session session = Session.getDefaultInstance(properties,new Authenticator(){
                public PasswordAuthentication getPasswordAuthentication()
                {
                    return new PasswordAuthentication("[email protected]", "邮件码"); //发件人邮件用户名、密码
                }
            });

            try{
                // 创建默认的 MimeMessage 对象
                MimeMessage message = new MimeMessage(session);

                // Set From: 头部头字段
                message.setFrom(new InternetAddress(from));

                // Set To: 头部头字段
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

                // Set Subject: 头部头字段
                message.setSubject("有订单信息改为取消");

                // 设置消息体
                message.setText("取消的订单ID为");

                // 发送消息
                Transport.send(message);
                System.out.println("Sent message successfully....from runoob.com");
            }catch (MessagingException mex) {
                mex.printStackTrace();
            }
        }


}

 

 报错情况:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 25;
  nested exception is:
    java.net.SocketException: Permission denied: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
    at javax.mail.Service.connect(Service.java:297)
    at javax.mail.Service.connect(Service.java:156)
    at javax.mail.Service.connect(Service.java:105)
    at javax.mail.Transport.send0(Transport.java:168)
    at javax.mail.Transport.send(Transport.java:98)
    at com.ctrip.common.SendMail.sendmail(SendMail.java:82)
    at com.ctrip.serviceImpl.OrderFindCancelseviceImpl.searchOrderStatus(OrderFindCancelseviceImpl.java:42)
    at com.ctrip.job.OrderFindCancelWorker.sendCancelEmail(OrderFindCancelWorker.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at qunar.tc.qschedule.config.spring.TaskBean$1.doWork(TaskBean.java:79)
    at qunar.tc.qschedule.executor.SchedulerProvider$2.handleScheduleMessage(SchedulerProvider.java:197)
    at qunar.tc.qschedule.executor.SchedulerProvider$2.onMessage(SchedulerProvider.java:181)
    at qunar.tc.qschedule.executor.ScheduleMessageHandlerImpl$1.run(ScheduleMessageHandlerImpl.java:87)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Permission denied: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
    ... 22 more
基于JavaMail的邮件发送及报错javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, po解决方案_第1张图片

 

解决办法:

基于JavaMail的邮件发送及报错javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, po解决方案_第2张图片

 properties.setProperty("mail.smtp.auth", "true");//开启认证
 properties.setProperty("mail.debug", "true");//启用调试
 properties.setProperty("mail.smtp.timeout", "1000");//设置链接超时
 properties.setProperty("mail.smtp.port", "465");//设置端口
 properties.setProperty("mail.smtp.socketFactory.port", "465");//设置ssl端口
 properties.setProperty("mail.smtp.socketFactory.fallback", "false");
 properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

 

参考文章:https://www.nonelonely.com/article/1553441211846 

感谢地址中博主的分享!

你可能感兴趣的:(错误解决)