首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >java email发: Couldn't connect to host?

java email发: Couldn't connect to host?

提问于 2019-07-03 15:43:51
回答 0关注 0查看 1.6K

代码:

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;

/**

* 使用SMTP协议发送电子邮件

*/

public class SendMailTest {

public static void main(String[] args) throws MessagingException {

// 1.创建一个程序与邮件服务器会话对象 Session

Properties props = new Properties();

props.setProperty("mail.transport.protocol", "SMTP");

props.setProperty("mail.smtp.host", "smtp.163.com");

props.setProperty("mail.smtp.port", "465");

// 指定验证为true

props.setProperty("mail.smtp.auth", "true");

props.setProperty("mail.smtp.timeout", "1000");

props.put("mail.smtp.ssl.enable", "true");

// 验证账号及密码,密码对应邮箱授权码(163密码可行,qq必须授权码)

Authenticator auth = new Authenticator() {

public PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication("xxxxx@163.com", "xxxx");

}

};

Session session = Session.getInstance(props, auth);

// 2.创建一个Message,它相当于是邮件内容

Message message = new MimeMessage(session);

// 设置发送者

message.setFrom(new InternetAddress("xxxxx@163.com"));

// 设置发送方式与接收者

message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("xxxxx@qq.com"));

// 设置主题

message.setSubject("邮件发送测试");

// 设置内容

message.setContent("Hello!", "text/html;charset=utf-8");

// 3.创建 Transport用于将邮件发送

Transport.send(message);

}

}

报错:‘’

Exception in thread "main" com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.163.com, 465; timeout -1;

nested exception is:

java.net.ConnectException: Connection timed out: connect

at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)

at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)

at javax.mail.Service.connect(Service.java:388)

at javax.mail.Service.connect(Service.java:246)

at javax.mail.Service.connect(Service.java:195)

at javax.mail.Transport.send0(Transport.java:254)

at javax.mail.Transport.send(Transport.java:124)

at org.mark.demo.service.impl.SendMailTest.main(SendMailTest.java:48)

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档