通过谷歌登录Javax.mail失败

时间:2014-01-17 08:17:35

标签: java javax.mail

我已经从我的笔记本电脑成功使用以下代码,收件人收到了预期的邮件。但是,将代码放在jar中并从托管服务器运行时。我得到以下错误。

代码

    Properties props = new Properties();  
    props.put("mail.smtp.host", "smtp.gmail.com");  
    props.put("mail.smtp.auth", "true");  
    props.put("mail.debug", "true");  
    props.put("mail.smtp.port", 465);  
    props.put("mail.smtp.socketFactory.port", 465);  
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.transport.protocol", "smtp");
    Session mailSession = null;

    mailSession = Session.getInstance(props,  
            new javax.mail.Authenticator() {  
        protected PasswordAuthentication getPasswordAuthentication() {  
            return new PasswordAuthentication("emailadress", "password");  
        }  
    }); 

    try {

        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);

      message.setSubject("Todays Email!:");
      message.setFrom(new InternetAddress("theemailsender@gmail.com"));

      message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient1@gmail.com"));

      String body = theBody;
      message.setContent(body,"text/html");
      transport.connect();

        transport.sendMessage(message,message.getAllRecipients());
        transport.close();
    } catch (Exception exception) {

    }

错误

DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: STARTTLS requested but already using SSL
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed

我在这一点上假设我要么将startle.enable或ssl.enable设置为true,而不是两者都设置为true。

2 个答案:

答案 0 :(得分:1)

这段代码对我有用,如果有帮助的话。您可以删除与附件相关的部分。

public static void sendEmailWithAttachments(String host, String port, final String userName,
    final String password, String toAddress, String subject, String message,
    String[] attachFiles)
throws AddressException, MessagingException {
    // sets SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.port", port);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.user", userName);
    properties.put("mail.password", password);

    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    };
    Session session = Session.getInstance(properties, auth);

    // creates a new e-mail message
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());

    // creates message part
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(message, "text/html");

    // creates multi-part
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    // adds attachments
    if (attachFiles != null && attachFiles.length > 0) {
        for (String filePath : attachFiles) {
            MimeBodyPart attachPart = new MimeBodyPart();

            try {
                attachPart.attachFile(filePath);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            multipart.addBodyPart(attachPart);
        }
    }

    // sets the multi-part as e-mail's content
    msg.setContent(multipart);

    // sends the e-mail
    Transport.send(msg);
}

在安全模式下,您可能需要将端口465替换为587。

答案 1 :(得分:0)

按照以下说明解决您的问题

  • 首先登录您的Gmail帐户,然后单击以下路径: google account

    在安全块中,刚刚打开

    允许不太安全的应用:打开

    希望这会节省您的时间.....祝您好运