使用gmail SMTP发送AuthenticationFailedException发送电子邮件

时间:2014-07-08 07:43:11

标签: smtp

我使用smtp从我的网站发送邮件到gmail.its工作很好localy。但是在远程服务器上没有工作

我以前使用此代码:

  public boolean sendMail(String subject, String bodyContent,String emailAddress){
    boolean isMailsent=false;
    final String SMTP_HOST= getText("email.smtp.host");
    final String SOCKET_FACTORY_PORT= getText("email.socket.factory.port");
    final String SMTP=getText("email.smtp.port");   
    final String MAIL_USER_EMAIL_ADDRESS=getText("email.username");
    final String MAIL_USER_PASSWORD= getText("email.password");
    final String EMAIL_FROMNAME= getText("email.fromname");
    try{

        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST);
        props.put("mail.smtp.socketFactory.port",SOCKET_FACTORY_PORT);
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", SMTP);

        Authenticator auth = new SMTPAuthenticator(MAIL_USER_EMAIL_ADDRESS, MAIL_USER_PASSWORD);
        Session session = Session.getInstance(props, auth);

        Message message = new MimeMessage(session);
        InternetAddress from = new InternetAddress(MAIL_USER_EMAIL_ADDRESS,EMAIL_FROMNAME);  
        message.setFrom(from);
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAddress));
        message.setSubject(subject);
        message.setContent(bodyContent,"text/html" );
        Transport.send(message);
        isMailsent=true;
    }catch(Exception e){
        LOGGER.error(e);
    }
    return isMailsent;
} 

如何修复它请给我最好的例子

1 个答案:

答案 0 :(得分:0)

您需要提供正确的GMail凭据才能进行身份验证。