间歇性发送电子邮件java

时间:2016-01-07 21:37:31

标签: java email

Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.office365.com");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    //props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");//As discussed but does not work if the leave
    props.put("mail.smtp.port", "587");



    Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("mail@domi.cl","paswwd");
            }
        });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("mail@domi.cl"));

        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("mail@domi.cl"));
        message.setSubject("Resumen envio masivo");
        message.setText(mensajeMail);

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

下面是StackTrace:

Could not connect to SMTP host: smtp.office365.com, port: 587;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
javax.faces.el.EvaluationException: java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587;
  nested exception is:
    java.net.ConnectException: Connection refused: connect

2 个答案:

答案 0 :(得分:1)

该异常意味着您无法连接到服务器smtp.office365.com上的端口587。使用telnet验证:

telnet smtp.office365.com 587

这可能导致您无法在您所在地工作的原因是:

Java中配置错误的代理设置

如果它不起作用并且您在公司网络中,那么很可能是导致问题的防火墙。还要检查密码和电子邮件是否正确。

也许它适用于TLS。你应该试一试。

答案 1 :(得分:0)

请检查您域的SMTP端口号或尝试我的代码。

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
public class Mail {
    String senderID;
    String senderPassword;
    String hostName;
    int portNumber;
    String attachmentPath;
    String subject;
    String body;
    String cc;
    String bcc;

    // #=============================================================================================#
    public String getBcc() {
        return bcc;
    }

    // #=============================================================================================#
    public void setBcc(String bcc) {
        this.bcc = bcc;
    }

    // #=============================================================================================#
    public String getCc() {
        return cc;
    }

    // #=============================================================================================#
    public void setCc(String cc) {
        this.cc = cc;
    }

    // #=============================================================================================#
    public String getBody() {
        return body;
    }

    // #=============================================================================================#
    public void setBody(String body) {
        this.body = body;
    }

    // #=============================================================================================#
    public String getSubject() {
        return subject;
    }

    // #=============================================================================================#
    public void setSubject(String subject) {
        this.subject = subject;
    }

    // #=============================================================================================#

    public String getSenderID() {
        return senderID;
    }

    // #=============================================================================================#
    public void setSenderID(String senderID) {
        this.senderID = senderID;
    }

    public String getSenderPassword() {
        return senderPassword;
    }

    // #=============================================================================================#
    public void setSenderPassword(String senderPassword) {
        this.senderPassword = senderPassword;
    }

    // #=============================================================================================#
    public String getHostName() {
        return hostName;
    }

    // #=============================================================================================#
    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    // #=============================================================================================#
    public int getPortNumber() {
        return portNumber;
    }

    // #=============================================================================================#
    public void setPortNumber(int portNumber) {
        this.portNumber = portNumber;
    }

    // #=============================================================================================#
    public String getAttachmentPath() {
        return attachmentPath;
    }

    // #=============================================================================================#
    public void setAttachmentPath(String attachmentPath) {
        this.attachmentPath = attachmentPath;
    }

    // #=============================================================================================#


    // #=============================================================================================#
    public void sendMail(String receiverId) {

        try {
            // this below commented line for the HTML body text
            // MultiPartEmail htmlEmail = new HtmlEmail();
            // OR
            // HtmlEmail email = new HtmlEmail();

            MultiPartEmail email = new MultiPartEmail();
            // setting the port number
            email.setSmtpPort(getPortNumber());
            // authenticating the user
            email.setAuthenticator(new DefaultAuthenticator(getSenderID(),
                    getSenderPassword()));
            // email.setDebug(true);
            email.setSSL(true);
            // setting the host name
            email.setHostName(getHostName());
            // setting the rciever id

            email.addTo(receiverId);

            // check for user enterd cc or not
            if (getCc() != null) {
                // add the cc
                email.addCc(getCc());
            }
            // check for user enterd bcc or not
            if (getBcc() != null) {
                // add the bcc
                email.addBcc(getBcc());
            }
            // setting the sender id
            email.setFrom(getSenderID());
            // setting the subject of mail
            email.setSubject(getSubject());
            // setting message body
            email.setMsg(getBody());
            // email.setHtmlMsg("<h1>"+getBody()+"</h1>");

            // checking for attachment attachment
            if (getAttachmentPath() != null) {
                // add the attachment
                EmailAttachment attachment = new EmailAttachment();
                attachment.setPath(getAttachmentPath());
                attachment.setDisposition(EmailAttachment.ATTACHMENT);
                email.attach(attachment);
            }

            // send the email
            email.send();
            // System.out.println("Mail sent!");
        } catch (Exception e) {
            // System.out.println("Exception :: " + e);
            // e.printStackTrace();
            // gl.writeWarning("Error occured in SendMail.java of sendMailWithAttachment() ");
            // gl.writeError(e);
        }
    }// sendmail()