1
votes

I am creating an application where I need to send an email from custom gmail domain. Here is my code for the same.

import java.time.LocalDateTime;
import java.util.Properties;
import java.util.Random;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class OTPMail {

     static char[] OTP(int len) 
        { 
            System.out.println("Generating OTP using random() : "); 
            // Using numeric values 
            String numbers = "0123456789"; 
            // Using random method 
            Random rndm_method = new Random(); 
            char[] otp = new char[len]; 
            for (int i = 0; i < len; i++) 
            { 
                // Use of charAt() method : to get character value 
                // Use of nextInt() as it is scanning the value as int 
                otp[i] = 
                 numbers.charAt(rndm_method.nextInt(numbers.length())); 
            } 
            return otp; 
        } 
        public static void main(String[] args) 
        { 
            int length = 4; 
            char[] OTP = OTP(length);
            System.out.print("Generated OTP is: ");
            System.out.println(OTP);
            String OTPString = String.valueOf(OTP);
            //send an email
            String messageForMail = "Your OTP for <company name>is: " + OTPString;

            //update admin mail and password here 


            final String username = "shop@<ownDomain>.com";
            final String password = "<passowrd>";

            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587");

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

            try {

                Message message = new MimeMessage(session);
                //message.setFrom(new InternetAddress("Kisna"));

                //update recipient mail id here.
                message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("<mailId>@gmail.com"));
                message.setSubject("OTP");
                message.setText(messageForMail);

                Transport.send(message);

                System.out.println("OTP sent to mail");
                //check the time when mail is sent

            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }

        } 

}

It gives me error like:

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via 534-5.7.14 your web browser and then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 80-v6sm5667641ywh.55 - gsmtp

at OTPMail.main(OTPMail.java:81) Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via 534-5.7.14 your web browser and then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 80-v6sm5667641ywh.55 - gsmtp

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826) at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685) at javax.mail.Service.connect(Service.java:317) at javax.mail.Service.connect(Service.java:176) at javax.mail.Service.connect(Service.java:125) at javax.mail.Transport.send0(Transport.java:194) at javax.mail.Transport.send(Transport.java:124) at OTPMail.main(OTPMail.java:75)

When I use same code for '[email protected]' mails, it works fine. But when I put custom domain like '[email protected]' it gives me mentioned error. Any idea on how I can solve the same?

1
'[email protected]' -> is the missing domain name a mistake or do you want to also test on an empty domain name?Absent
Did you try with a valid custom domain?user10527814
Thanks for informing, edited the question. Please check.Deva
@aka-one Yes, it gave me mentioned error.Deva
Can you log in using your browser with that custom-domain email address?user10527814

1 Answers

0
votes

To send emails with Java Mail you need to let less secure apps access your account. Go to Google Account -> Sign-in & security page and check Allow less secure apps then try again.