gpt4 book ai didi

java - 如何从自定义 Gmail 域发送邮件?

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:06 25 4
gpt4 key购买 nike

我正在创建一个应用程序,我需要从自定义 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);
}

}

}

它给了我这样的错误:

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)

当我对“xxx@gmail.com”邮件使用相同的代码时,它工作正常。但是当我放置像“xxx@ownDomain.com”这样的自定义域时,它会给我提到的错误。知道如何解决同样的问题吗?

最佳答案

要使用 Java Mail 发送电子邮件,您需要让安全性较低的应用程序访问您的帐户。转至 Google 帐户 -> 登录和安全页面,然后选中允许安全性较低的应用,然后重试。

关于java - 如何从自定义 Gmail 域发送邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53043320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com