我正在创建一个应用程序,需要从自定义gmail域发送电子邮件。 这是我的相同代码。
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);
}
}
}
它给了我这样的错误:
线程“ main”中的异常java.lang.RuntimeException: javax.mail.AuthenticationFailedException:534-5.7.14 请 通过534-5.7.14登录您的网络浏览器,然后重试。 534-5.7.14 了解更多信息,在534 5.7.14 https://support.google.com/mail/answer/78754 80-v6sm5667641ywh.55-gsmtp
在OTPMail.main(OTPMail.java:81)的原因: javax.mail.AuthenticationFailedException:534-5.7.14 请 通过534-5.7.14登录您的网络浏览器,然后重试。 534-5.7.14 了解更多信息,在534 5.7.14 https://support.google.com/mail/answer/78754 80-v6sm5667641ywh.55-gsmtp
在com.sun.mail.smtp.SMTPTransport $ Authenticator.authenticate(SMTPTransport.java:826) 在 com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761) 在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685) 在javax.mail.Service.connect(Service.java:317)在 javax.mail.Service.connect(Service.java:176)在 javax.mail.Service.connect(Service.java:125)在 javax.mail.Transport.send0(Transport.java:194)在 javax.mail.Transport.send(Transport.java:124)在 OTPMail.main(OTPMail.java:75)
当我对“ xxx@gmail.com”邮件使用相同的代码时,它可以正常工作。 但是,当我将自定义域设置为“ xxx@ownDomain.com”时,它给了我提到的错误。关于如何解决该问题的任何想法吗?
答案 0 :(得分:0)
要使用Java Mail发送电子邮件,您需要让安全程度较低的应用访问您的帐户。 转到 Google帐户->登录和安全页面,然后选中允许安全程度较低的应用,然后重试。