When I send mail Then Found Error javax.mail.AuthenticationFailedException: 535 Invalid authentication data

I got the following error when sending email with the code below

javax.mail.AuthenticationFailedException: 535 Incorrect authentication data

What could be the problem in my code.

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;
    }
}

      

+3


source to share


2 answers


just type:

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

      



instead:

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

      

0


source


I got the same problem on my first try ... and this code is fine to send both on LAN and Gmail ... Try



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();
    }
}

      

0


source







All Articles