java测试smtp连接失败,邮件发送失败,SMTP无法连接

I am trying to send a mail using the below program but I am getting the following error message.

public class SMTPTest {

//private Logger log = Logger.getLogger(this.getClass());

public boolean sendSimpleMail(String to, String subject, String body) {

Properties props = new Properties();

props.put("mail.smtp.user", "amrita_test");

props.put("mail.smtp.password", "aview");

props.put("mail.smtp.host", "192.168.0.25");

props.put("mail.smtp.port", "25");

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

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

// props.put("mail.smtp.debug", "true");

props.put("mail.smtp.debug", "false");

props.put("mail.smtp.socketFactory.port", "425");

props.put("mail.smtp.socketFactory.class",

"javax.net.ssl.SSLSocketFactory");

props.put("mail.smtp.socketFactory.fallback", "false");

try {

Authenticator auth = new SMTPAuthenticator();

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

// session.setDebug(true);

MimeMessage msg = new MimeMessage(session);

String content = body;

msg.setSubject(subject);

msg.setFrom(new InternetAddress("buddhiedge@gmail.com"));

Address[] addresses = new Address[1];

addresses[0] = new InternetAddress("buddhiedge@gmail.com");

msg.setReplyTo(addresses);

msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

msg.setContent(content, "text/html");

Transport.send(msg);

return true;

} catch (Exception mex) {

mex.printStackTrace();

System.out.println("Error in sending mail :: " + mex.getMessage());

return false;

}

}

private class SMTPAuthenticator extends Authenticator {

public PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication("amrita_test",

"aview");

}

}

public static void main(String args[]){

SMTPTest test=new SMTPTest();

test.sendSimpleMail("praveenpkd@gmail.com", "subject", "body");

}

}

Error is:

javax.mail.MessagingException: Could not connect to SMTP host: 192.168.0.25, port: 25;

nested exception is:

java.net.ConnectException: Connection timed out: 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 SMTPTest.sendSimpleMail(SMTPTest.java:45)

at SMTPTest.main(SMTPTest.java:62)

Caused by: java.net.ConnectException: Connection timed out: connect

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)

at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)

at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)

at java.net.Socket.connect(Socket.java:519)

at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)

at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)

at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)

at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)

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

... 8 more

But the There is no problem with SMTP Server or connection.

telnet 192.168.0.25 25

connects fine.

How to solve this.

解决方案

Please try to send mail again by removing only the following from your code.

props.put("mail.smtp.socketFactory.port", "425");

props.put("mail.smtp.socketFactory.class",

"javax.net.ssl.SSLSocketFactory");

props.put("mail.smtp.socketFactory.fallback", "false");

Normally, the exception

java.net.ConnectException: Connection timed out

is caused only if the java mail is not able to connect to the mentioned server in the specified port. The most probable cause would be that java mail tries to connect to the 425 port specified in your session properties.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值