1

誰かがcPanelの電子メールa/csに接続して電子メールを送信するJava電子メール/smtp/imapクライアントプログラムを試した/見つけた場合は、それを共有してください. そのコードをオンラインで見つけようとするのは骨の折れる作業でしたが、どれもうまく動作しません。5 種類以上のコードを試しましたが、うまくいきませんでした。以下にいくつかのサンプルを示します。

サンプル#1

    String host = "mail.domain.net"; 
String user = "catch-all@domain.net";
String pass = "xxxx";
String to = "admin@domain.net";
String from = "catch-all@domain.net";
String subject = "Dummy subject";
String messageText = "Dummy body";
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", 2525); //25 - default

Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
try {
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] address = {new InternetAddress(to)};
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText(messageText);
    Transport transport = mailSession.getTransport("smtp");
    transport.connect(host, user, pass);
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
} catch (Exception e) {
    e.printStackTrace();
}

エラー

Sending mail..  Done!javax.mail.MessagingException: Could not connect to SMTP host: mail.dealstock.net, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:192)
    at com.mail.EmailsSender2.main(EmailsSender2.java:209)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
    at com.sun.mai

Cpanel アカウントから、指定されたポートは 2525 で、保護されている場合は 465 ですが、どのポートも機能しません。ポスト 2525 では、接続しますが、応答がなく、1 ~ 2 分待ってからタイムアウトします。ポート 25 に変更すると、単純に上記のエラーがスローされます。同じ Cpanel メール a/c を使用すると、別のプログラムで POP を介してメールに接続して読み取ることができますが、メールの送信に失敗します。

コメント/入力を共有していただければ幸いです。

4

2 に答える 2

2

私はホストゲーターを持っており、セッションを作成するときに、次のコードを使用して動作します(TLSではなく暗号化またはSSLなし)。

ところで、ポート 25 は私の isp (Verizon) によってブロックされており、hostgator は 2525 ではなくポート 26 にそれを持っています。ホスティング会社が 2525 で実行していることを確認してください。

    private Session getSession(final EmailConfig emailConfig) {
    String port = Integer.toString(emailConfig.getPort());
    Properties props = new Properties();
    props.put("mail.smtp.host", emailConfig.getHost());
    props.put("mail.smtp.port", port);

    if (Encryption.SSL == emailConfig.getEncryption()) {
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", port);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    }
    else if (Encryption.TLS == emailConfig.getEncryption()) {
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
    }

    return Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(emailConfig.getUsername(), emailConfig.getDecryptedPassword());
        }
    });
}


    private void sendMessage(Session session, InternetAddress from, InternetAddress[] to, InternetAddress[] cc, 
        InternetAddress[] bcc, String subject, String text) throws AuthenticationFailedException {
    Message message = new MimeMessage(session);
    try {
        message.setFrom(from);
        if (to != null) {
            message.setRecipients(Message.RecipientType.TO, to);
        }
        if (cc != null) {
            message.setRecipients(Message.RecipientType.CC, cc);
        }
        if (bcc != null) {
            message.setRecipients(Message.RecipientType.BCC, bcc);
        }
        message.setSubject(subject);
        message.setText(text);

        Transport.send(message);
    }
    catch (AuthenticationFailedException ex) {
        log.info("authentication failed", ex);
        throw ex;
    }
    catch (MessagingException e) {
        log.info("error sending message", e);
        e.printStackTrace();
    }
}
于 2013-06-13T14:35:29.043 に答える
0

これは間違いなくあなたのために働くでしょう.

この関数を呼び出すだけで、自動化された電子メールをクライアントに送信できます。パラメータの「to」は、電子メールの送信先の電子メール アドレスです。

私は通常Mavenプロジェクトでそれを行います。Maven プロジェクトを使用している場合は、次の依存関係をインポートします。 https://mvnrepository.com/artifact/javax.mail/mail/1.4

    final String username = "youremail@gmail.com";
    final String password = "yourpassword";
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    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("youremail@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
        message.setSubject(subject);
        message.setContent(emailBody, "text/html; charset=utf-8");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
于 2016-09-20T22:34:57.443 に答える