簡體   English   中英

Java SMTP郵件-連接超時錯誤

[英]Java SMTP mail - Connection timed out Error

我一直在開發一個程序,用於從基於桌面的Java應用程序發送電子郵件。

當我使用Google SMTP服務器(smtp.gmail.com)測試該程序時,該程序運行良好,但是,當我針對其他smtp服務器對其進行測試時,它會產生錯誤-

javax.mail.MessagingException: Could not connect to SMTP host:
 smtp.collaborationhost.net, port: 465;
 nested exception is:
 java.net.ConnectException: Connection timed out: connect

我正在Eclipse中運行代碼。

以下是代碼段-

public class Emailer {

    public static void main(String[] args) {

        String name = "Testing";

        Properties props = new Properties();

        props.put("mail.smtp.host", "smtp.collaborationhost.net");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props, 
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("qwert@xyzcompany.com","*******");
                }
        });

        try
        {
            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress("qwert@xyzcompany.com"));
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("testgmail.com"));     
            message.setSubject("Generic Mail Test from Eclipse !!!");
            message.setText("This is a Test Mail, sent from Local Eclipse System via Google SMTP server. \n\n" + "Regards, \n" + name + "." );
            Transport.send(message);

            System.out.println("Message Sent !!!");
        }

        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

此JavaMail FAQ條目將幫助您調試連接問題 在這種情況下,我懷疑其他服務器沒有使用與Gmail相同的端口,因此您需要更改配置。

另外,您可以通過更正一些常見錯誤來簡化程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM