【问题标题】:When I send mail Then Found Error javax.mail.AuthenticationFailedException: 535 Incorrect authentication data当我发送邮件时发现错误 javax.mail.AuthenticationFailedException: 535 Incorrect authentication data
【发布时间】:2015-09-28 17:12:21
【问题描述】:

通过以下代码发送电子邮件时出现以下错误

javax.mail.AuthenticationFailedException: 535 Incorrect authentication data

我的代码可能有什么问题。

public class SendMail {

    public static boolean sendHTMLMail(final String from, final String password, String senderName, String sub, String msg, String[] to) {

        String host = "mail.xxxx.org";
        MimeMultipart multipart = new MimeMultipart();
        MimeBodyPart bodypart = new MimeBodyPart();
        Properties p = new Properties();
        p.setProperty("mail.smtp.host", host);
        p.put("mail.smtp.port", 587);
        p.put("mail.smtp.auth", "true");

        try {
            Session session = Session.getInstance(p, new javax.mail.Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from, password);
                }
            });
            Transport transport = session.getTransport("smtp");
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress("" + senderName + "<" + from + ">"));
            InternetAddress[] toAddress = new InternetAddress[to.length];
            for (int i = 0; i < to.length; i++) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for (InternetAddress toAddres : toAddress) {
                mimeMessage.addRecipient(Message.RecipientType.TO, toAddres);
            }
            bodypart.setContent(msg, "text/html; charset=\"utf-8\"");
            multipart.addBodyPart(bodypart);
            mimeMessage.setSubject(sub);
            mimeMessage.setContent(multipart);
            transport.connect(host, from, password);
            mimeMessage.saveChanges();
            Transport.send(mimeMessage);
            transport.close();
            return true;
        } catch (MessagingException me) {
            me.printStackTrace();
        }
        return false;
    }
}

【问题讨论】:

    标签: java email


    【解决方案1】:

    简单地说:

    p.setProperty("mail.smtps.host", host);
    p.put("mail.smtps.port", 587);
    p.put("mail.smtps.auth", "true");
    

    代替:

    p.setProperty("mail.smtp.host", host);
    p.put("mail.smtp.port", 587);
    p.put("mail.smtp.auth", "true");
    

    【讨论】:

      【解决方案2】:

      我在第一次尝试时遇到了同样的问题...并且此代码可以在我的本地网络和 Gmail 中发送...您试一试

      package SendMail;
      
      import java.util.Properties;
      import javax.mail.Message;
      import javax.mail.MessagingException;
      import javax.mail.Session;
      import javax.mail.Transport;
      import javax.mail.internet.AddressException;
      import javax.mail.internet.InternetAddress;
      import javax.mail.internet.MimeMessage;
      
      /**
       * @author akash073@gmail.com
       *
       */
      
      public class CrunchifyJavaMailExample {
      
          //static Properties mailServerProperties;
         // static Session getMailSession;
        //  static MimeMessage generateMailMessage;
      
          public static void main(String args[]) throws AddressException, MessagingException {
              generateAndSendEmail();
              System.out.println("\n\n ===> Your Java Program has just sent an Email successfully. Check your email..");
          }
      
          public static void generateAndSendEmail() throws AddressException, MessagingException {
      
              String smtpHost="put Your Host";
              String smtpUser="UserName in full @somthing.com";
              String smtpPassword="your password";
              int smtpPort=25;//Port may vary.Check yours smtp port
              // Step1
              System.out.println("\n 1st ===> setup Mail Server Properties..");
              Properties mailServerProperties = System.getProperties();
              //mailServerProperties.put("mail.smtp.ssl.trust", smtpHost);
      //        mailServerProperties.put("mail.smtp.starttls.enable", true); // added this line
              mailServerProperties.put("mail.smtp.host", smtpHost);
              mailServerProperties.put("mail.smtp.user", smtpUser);
              mailServerProperties.put("mail.smtp.password", smtpPassword);
              mailServerProperties.put("mail.smtp.port", smtpPort);
      
              mailServerProperties.put("mail.smtp.starttls.enable", "true");
              System.out.println("Mail Server Properties have been setup successfully..");
      
              // Step2
              System.out.println("\n\n 2nd ===> get Mail Session..");
              Session getMailSession = Session.getDefaultInstance(mailServerProperties, null);
              MimeMessage generateMailMessage = new MimeMessage(getMailSession);
              generateMailMessage.setFrom (new InternetAddress (smtpUser));
              generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("akash073@waltonbd.com"));
              generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("akash073@gmail.com"));
              generateMailMessage.setSubject("Greetings from Crunchify..");
              String emailBody = "2.Test email by Crunchify.com JavaMail API example. " + "<br><br> Regards, <br>Crunchify Admin";
              generateMailMessage.setContent(emailBody, "text/html");
              System.out.println("Mail Session has been created successfully..");
      
              // Step3
              System.out.println("\n\n 3rd ===> Get Session and Send mail");
              Transport transport = getMailSession.getTransport("smtp");
      
              // Enter your correct gmail UserID and Password
              // if you have 2FA enabled then provide App Specific Password
              transport.connect(smtpHost,smtpPort, smtpUser, smtpPassword);
              transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
              transport.close();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多