在 Liferay 7.3 中使用 Microsoft Graph 发送电子邮件无法解决依赖关系

问题描述 投票:0回答:1

我最初是通过 JavaMail (SMTP) 从 Liferay 7.3 应用程序发送电子邮件。 SMTP 用于通过我们的 Exchange 365 实例进行中继。我不得不改用不同的方法,所以我决定使用 Graph 来访问 Exchange 365,因为它看起来很简单......

问题是我得到“org.osgi.framework.BundleException:无法解析模块:org.bsfinternational.dashboard [1147]_未解决的要求:导入包:com.azure.core.credential_[已清理] ”

通常我可以通过仔细检查 build.gradle 条目来解决这些问题,以确保我已经考虑了依赖项。 这是我所拥有的:

// Azure 核心

实现“com.azure:azure-core:1.43.0”

实现“com.azure:azure-core-http-netty:1.15.0”

// 包含 sdk 作为依赖项

实现“com.microsoft.graph:microsoft-graph:5.57.0”

实现“com.microsoft.graph:microsoft-graph-auth:0.3.0”

// 包含 Azure 身份进行身份验证

实现“com.azure:azure-identity:1.9.0”

我可以发布代码,但这似乎不是代码问题,因为代码可以编译。 在gradle中构建成功。 当我部署到OSGI时,抛出依赖失败错误。 我多次仔细检查了依赖关系。 我尝试省略项目中 bnd.bnd 文件中的某些包。 似乎什么都不起作用。 我的假设是,我缺少一些传递依赖项,但我找不到任何文档表明需要另一个库。 我尝试过不同版本的库,包括和省略它们。 我可以编译代码,但无法部署它。 我是 Graph API 的新手,但该 API 看起来非常简单。

Liferay 中的 GoGo shell 没有帮助。它准确地告诉我部署告诉我的“未解决的需求”。我试图找到一种更好的方法来理解依赖关系树,但根据我所读到的内容,我没有遗漏任何必需的依赖关系。 我之前在其他 Liferay 项目中也遇到过这个问题,但通过反复试验,我总是能够找出缺少的内容。 这次,没有运气。

此时任何想法都会有所帮助。 谢谢。

代码示例在这里:

public static int sendExchangeEmail(String fromName, String fromAddress, String toAddress, String toName, String subject, String emailbody, String replyT, boolean htmlFormat)
 throws SystemException
{                            
int messageID = 0;
                    
String clientId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
String clientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
String tenantId ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

 // Create credentials
ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder().clientId(clientId).clientSecret(clientSecret).tenantId(tenantId).build();

 // Initialize authentication provider and Graph client
final TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(clientSecretCredential);
final GraphServiceClient<okhttp3.Request> graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();

// Construct the email message
com.microsoft.graph.models.Message message = new com.microsoft.graph.models.Message();

message.subject = subject;
ItemBody body = new ItemBody();
body.contentType = BodyType.TEXT;
body.content = emailbody;
message.body = body;
LinkedList<Recipient> toRecipients = new LinkedList<>();
 Recipient recipient = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = "[email protected]";
emailAddress.name = toName;
recipient.emailAddress = emailAddress;
toRecipients.add(recipient);
message.toRecipients = toRecipients;
Recipient from = new Recipient();
from.emailAddress = new EmailAddress();
from.emailAddress.address = fromAddress;
from.emailAddress.name = fromName;
message.from = from;
LinkedList<Recipient> replyToRecipients = new LinkedList<>();
Recipient replyTo = new Recipient();
replyTo.emailAddress = new EmailAddress();
replyTo.emailAddress.address = replyT;
replyToRecipients.add(replyTo);
message.replyTo = replyToRecipients;
// Create the parameter set
UserSendMailParameterSet parameterSet = new UserSendMailParameterSet().newBuilder().withMessage(message).withSaveToSentItems(false).build();
// Don't save to Sent Items folder
// Send the email
try {
    graphClient.me().sendMail(parameterSet).buildRequest().post();

    System.out.println("Email sent successfully!");

     } catch (Exception e) {

         e.printStackTrace();

         System.err.println("Error sending email: " + e.getMessage());

}
 return messageID; 

 }

这不是一个独立的程序。 这是该特定源文件中众多方法中的一种。添加的相关导入以便程序可以编译是:

import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;

import com.microsoft.graph.models.*;
import com.microsoft.graph.models.UserSendMailParameterSet;
import com.microsoft.graph.models.BodyType;
import com.microsoft.graph.models.ItemBody;
import com.microsoft.graph.requests.GraphServiceClient;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;

再次强调,这不是代码问题,而是部署问题。 由于部署在 OSGI 堆栈中缺少某些内容,导致程序无法部署。

java azure microsoft-graph-api liferay-7
1个回答
0
投票

我在代码中使用了

Mail.Send
范围,使用 Gradle 在 Java 中发送电子邮件。我在
Mail.Send
中添加了
API permissions
范围并授予了
admin consent
,如下所示。

enter image description here

我在

build.gradle.kts
中使用了以下依赖项。

implementation("com.azure:azure-identity:1.9.0")
implementation("com.microsoft.graph:microsoft-graph:5.13.0")
implementation("com.sun.mail:javax.mail:1.6.2")
implementation("org.slf4j:slf4j-api:2.0.0-alpha1")
implementation("org.slf4j:slf4j-simple:2.0.0-alpha1")

build.gradle.kts:

plugins {
    id("java")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.azure:azure-identity:1.9.0")
    implementation("com.microsoft.graph:microsoft-graph:5.13.0")
    implementation("com.sun.mail:javax.mail:1.6.2")
    implementation("org.slf4j:slf4j-api:2.0.0-alpha1")
    implementation("org.slf4j:slf4j-simple:2.0.0-alpha1")
    testImplementation(platform("org.junit:junit-bom:5.10.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
    useJUnitPlatform()
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21) 
    }
}

KamEmail.java:

import com.azure.identity.InteractiveBrowserCredential;
import com.azure.identity.InteractiveBrowserCredentialBuilder;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.models.*;
import com.microsoft.graph.requests.GraphServiceClient;

import java.util.Arrays;

public class KamEmail {
    private static final String App_ID = "<appID>";
    private static final String Tenant_ID = "common";
    private static final String[] Scope = {"Mail.Send"};

    public static void main(String[] args) {
        try {
            InteractiveBrowserCredential credential = new InteractiveBrowserCredentialBuilder()
                    .clientId(App_ID)
                    .tenantId(Tenant_ID)
                    .build();

            TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(Arrays.asList(Scope), credential);
            GraphServiceClient<?> graphClient = GraphServiceClient
                    .builder()
                    .authenticationProvider(authProvider)
                    .buildClient();

            Message sendmessage = new Message();
            sendmessage.subject = "Meeting";
            ItemBody body = new ItemBody();
            body.contentType = BodyType.TEXT;
            body.content = "We have a meeting at 2:00 PM. Make sure not to miss it.";
            sendmessage.body = body;

            Recipient FROM = new Recipient();
            FROM.emailAddress = new EmailAddress();
            FROM.emailAddress.address = "[email protected]";
            Recipient TO = new Recipient();
            TO.emailAddress = new EmailAddress();
            TO.emailAddress.address = "[email protected]";

            sendmessage.toRecipients = Arrays.asList(FROM);
            sendmessage.ccRecipients = Arrays.asList(TO);
            UserSendMailParameterSet mailParams = new UserSendMailParameterSet();
            mailParams.message = sendmessage;
            mailParams.saveToSentItems = Boolean.FALSE;
            graphClient.me().sendMail(mailParams).buildRequest().post();
            System.out.println("Meeting mail sent successfully!");
        } catch (Exception e) {
            System.err.println("Error sending email: " + e.getMessage());
        }
    }
}

我在

Mobile and desktop applications
下添加了以下 URL 并允许
public client flow
,如下所示。

http://localhost

enter image description here

enter image description here

当我运行代码时,我得到了以下 Outlook 重定向登录页面。

enter image description here

enter image description here

使用Gradle在Java中发送邮件成功,如下图。

enter image description here

展望:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.