1

如果有人尝试/发现连接到 cPanel 的电子邮件 a/cs 并发送电子邮件的有效 java email/smtp/imap 客户端程序,请分享。试图在网上找到该代码是一项令人厌烦的工作,但它们都不能正常工作。我确实尝试了五种以上的代码,但没有任何效果。以下是几个示例:

样品#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

我有 hostgator,在创建会话时,使用以下代码它可以工作(没有加密或 SSL,而不是 TLS)。

顺便说一句,我的 isp (Verizon) 阻止了端口 25,并且 hostgator 在端口 26 上拥有它,而不是 2525。确保您的托管公司在 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 回答