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?