Office365 JavaMail错误

时间:2015-05-31 15:24:27

标签: javamail office365

我有一个Java类,可以完美地发送带有Gmail帐户的电子邮件。但是,我使用了推荐的SMTP设置,当我尝试使用Office365帐户发送电子邮件时出错。返回的错误是:

例外

javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

以下是我的代码:

public class GmailBean {

  public static final String SERVIDOR_SMTP = "smtp.office365.com";
  public static final int PORTA_SERVIDOR_SMTP = 587;
  private static final String CONTA_PADRAO = "xxx@xxx.com";
  private static final String SENHA_CONTA_PADRAO = "xxx";


  private String de;
  private String para;

  private String assunto;
  private String mensagem;

  public void enviarEmail() throws MessagingException {

    FacesContext context = FacesContext.getCurrentInstance();
    AutenticaUsuario autenticaUsuario = new AutenticaUsuario(GmailBean.CONTA_PADRAO, GmailBean.SENHA_CONTA_PADRAO);

    Session session = Session.getInstance(this.configuracaoEmail(), autenticaUsuario);

    // try{
    Transport envio = null;
    MimeMessage email = new MimeMessage(session);
    email.setRecipient(Message.RecipientType.TO, new InternetAddress(this.para));
    email.setFrom(new InternetAddress(this.de));
    email.setSubject(this.assunto);
    email.setContent(this.mensagem, "text/plain");
    email.setSentDate(new Date());
    envio = session.getTransport("smtp");
    envio.connect(GmailBean.SERVIDOR_SMTP, GmailBean.CONTA_PADRAO, GmailBean.SENHA_CONTA_PADRAO);
    email.saveChanges();
    envio.sendMessage(email, email.getAllRecipients());
    envio.close();

    context.addMessage(null, new FacesMessage("Mensagem enviada com sucesso!"));

    /* }
     catch(AddressException ex)
     { Logger logger = Logger.getAnonymousLogger();

     FacesMessage msg = new FacesMessage("Erro ao enviar mensagem "+ ex.getMessage());
     logger.info("Erro ao enviar mensagem _____________"+ ex.getMessage());
     }
     catch(MessagingException ex)
     {
     Logger logger = Logger.getAnonymousLogger();
     FacesMessage msg = new FacesMessage("Erro ao enviar mensagem "+ ex.getMessage());
     logger.info("Erro ao enviar mensagem _____________"+ ex.getMessage());


     }*/
  }

  public Properties configuracaoEmail() {

    Properties config = new Properties();

    config.put("mail.smtp.auth", "true");
    config.put("mail.transport.protocol", "smtp");
    config.put("mail.smtp.starttls.enabled", "true");
    config.put("mail.smtp.host", SERVIDOR_SMTP);
    config.put("mail.user", GmailBean.CONTA_PADRAO);
    config.put("mail.smtp.port", PORTA_SERVIDOR_SMTP);
    config.put("mail.smtp.socketFactory.port", PORTA_SERVIDOR_SMTP);
    config.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    config.put("mail.smtp.socketFactory.fallback", "false");

    return config;

  }

  // (Getters and setters... )

  class AutenticaUsuario extends Authenticator {

    private String usuario;
    private String senha;

    public AutenticaUsuario(String usuario, String senha) {
     this.usuario = usuario;
      this.senha = senha;
    }

    public PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(this.usuario, this.senha);
    }

  }

1 个答案:

答案 0 :(得分:1)

修复所有这些common mistakes,它应该有效。

JavaMail FAQ entry for outlook.com也适用于Office365,也可以更改主机名。