Cpanel Java电子邮件客户端程序

时间:2013-05-23 20:22:14

标签: java email smtp cpanel

如果有人尝试/找到工作的java电子邮件/ smtp / imap客户端程序连接到cPanel的电子邮件a / cs并发送电子邮件,请分享。尝试在线查找代码是一项令人厌倦的努力,但它们都没有正常工作。我确实尝试过五种以上的代码,但没有任何效果。以下是几个样本:

样本#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连接和阅读电子邮件,但无法发送电子邮件。

感谢您是否可以分享您的意见/输入。

2 个答案:

答案 0 :(得分:1)

我有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();
    }
}

答案 1 :(得分:0)

这绝对适合你。

只需调用此函数即可向客户端发送自动电子邮件。 在参数"到"是您要向其发送电子邮件的电子邮件地址。

我通常在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);
    }
相关问题