gpt4 book ai didi

java - 如何使用带有 OAuth2 身份验证的 Gmail SMTP 使用 java 邮件发送邮件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:31 27 4
gpt4 key购买 nike

我阅读了下面的链接,但无法使用 OAuth2 身份验证机制发送邮件。谁能给我举个例子吗?

如果没有此身份验证,用户必须从 gmail 的安全设置中启用安全性较低的应用程序选项。但我不想强制用户这样做。这就是我需要这种身份验证机制的原因。

链接:https://java.net/projects/javamail/pages/OAuth2

最佳答案

下面给出了如何在使用 Spring 的 Web 项目中使用 OAuth2 发送电子邮件的工作代码。

重要:

  • 首先在 Google console 创建项目,之后在 Google 控制台创建凭据时记得设置应用程序类型为其他。

  • 使用 oauth2.py获取访问 token 和其他所需的值代码。文件本身有关于如何使用它来获取所需值的说明。

spring-email.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- email configuration -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com"/>
<property name="port" value="587"/>
<property name="username" value="xxxxxxxxxxxxx@gmail.com"/>
<property name="password" value=""/>

<property name="javaMailProperties">
<props>
<!-- Use SMTP transport protocol -->
<prop key="mail.transport.protocol">smtp</prop>
<!-- Use SMTP-AUTH to authenticate to SMTP server -->
<prop key="mail.smtp.auth">true</prop>
<!-- GMail requires OAuth to not be considered "low-security" -->
<prop key="mail.smtp.auth.mechanisms">XOAUTH2</prop>
<!-- Use TLS to encrypt communication with SMTP server -->
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">false</prop>
</props>
</property>

</bean>
<bean id="message" class="com.upriverbank.model.Message">
<property name="mailSender" ref="mailSender" />
</bean>

</beans>

OAuthMail.java

package com.abc.security;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.mail.javamail.JavaMailSender;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

public class OAuthMail {


// For using Oauth2

private static String TOKEN_URL = "https://www.googleapis.com/oauth2/v4/token";
private JavaMailSender sender;

// Not a best practice to store client id, secret and token in source
// must be stored in a file.
private String oauthClientId = "xxxxxxxxxxxxxxxx.apps.googleusercontent.com";
private String oauthSecret = "xxxxxxxxxxxxxxxxxxxx";
private String refreshToken = "xxxxxxxxxxxxxxxxxxxxxxx";
private static String accessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxx";
private long tokenExpires = 1458168133864L;

// getters and setters

public static String getAccessToken() {
return accessToken;
}

public static void setAccessToken(String accessToken) {
accessToken = accessToken;
}


/*
Renew access token if expired
*/
public void renewToken(){

if(System.currentTimeMillis() > tokenExpires) {

try
{
String request = "client_id="+ URLEncoder.encode(oauthClientId, "UTF-8")
+"&client_secret="+URLEncoder.encode(oauthSecret, "UTF-8")
+"&refresh_token="+URLEncoder.encode(refreshToken, "UTF-8")
+"&grant_type=refresh_token";
HttpURLConnection conn = (HttpURLConnection) new URL(TOKEN_URL).openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(request);
out.flush();
out.close();
conn.connect();

try
{

HashMap<String,Object> result;
result = new ObjectMapper().readValue(conn.getInputStream(), new TypeReference<HashMap<String,Object>>() {});
accessToken = (String) result.get("access_token");
tokenExpires = System.currentTimeMillis()+(((Number)result.get("expires_in")).intValue()*1000);
}
catch (IOException e)
{
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
while((line = in.readLine()) != null) {
System.out.println(line);
}
System.out.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}

Message.java

package com.abc.model;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.upriverbank.security.OAuthMail;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Properties;


public class Message {


public static final String PORT = "465";
private static final String HOST = "smtp.gmail.com";
private final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private String name;
private String customerID;
private String email;
private int queryType;
private int id;
private String title;
private String rStatus;
private String query;
private Byte isRead;
private int customerId;
private String password;
private static final String BANK_SUPPORT_EMAIL = "xxxxxxx@gmail.com";


public static String getBankSupportEmail() {
return BANK_SUPPORT_EMAIL;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getrStatus() {
return rStatus;
}

public void setrStatus(String rStatus) {
this.rStatus = rStatus;
}

public String getQuery() {
return query;
}

public void setQuery(String query) {
this.query = query;
}

public Byte getIsRead() {
return isRead;
}

public void setIsRead(Byte isRead) {
this.isRead = isRead;
}

public int getCustomerId() {
return customerId;
}

public void setCustomerId(int customerId) {
this.customerId = customerId;
}


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCustomerID() {
return customerID;
}

public void setCustomerID(String customerID) {
this.customerID = customerID;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public int getQueryType() {
return queryType;
}

public void setQueryType(int queryType) {
this.queryType = queryType;
}




public void viewMessage() {

}

public void setPassword(String password) {
this.password = password;
}



private MailSender mailSender;

public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}



/*
Sending an email according to provided parameters
(sender, receiver, subject, content)
*/
public void sendMessage(String from, String to, String subject, String msg) {


((JavaMailSenderImpl)this.mailSender).setPassword(OAuthMail.getAccessToken());

SimpleMailMessage message = new SimpleMailMessage();

message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(msg);

// sending email

mailSender.send(message);
}

}

MessageTest.java

package com.abc.model;

import com.abc.support.spring.SpringUtil;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

class MessageTest {

@Test
void sendMessage() {

Message mm = (Message) SpringUtil.customBeanFactory("message");
mm.sendMessage("xxxxxxx@gmail.com",
"xxxxxxxxx@gmail.com",
"Testing email",
"This is a testing email");
}

}

关于java - 如何使用带有 OAuth2 身份验证的 Gmail SMTP 使用 java 邮件发送邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32831174/

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