不能对 null 值表达式调用方法!ImapX、Powershell

You cannot call a method on a null-valued expression! ImapX, Powershell

提问人:user22418165 提问时间:8/20/2023 最后编辑:jdwenguser22418165 更新时间:8/20/2023 访问量:34

问:

我正在尝试使用IMAP检查我的Gmail收件箱的最新电子邮件。但无论出于何种原因,我在这里都面临着图书馆问题。无论我使用什么库版本,都会出现同样的问题。我正在尝试此代码,无论出于何种原因,它都显示以下错误:

GAC 版本位置


假 v2.0.50727 C:\Users\bijoy.rc\Downloads\ImapX.2.0.0.18\lib\net35\ImapX.dll 假 v2.0.50727 C:\Users\bijoy.rc\Downloads\LinqBridge.1.3.0\lib\net20\LinqBridge.dll 真 已建立 IMAP 连接 客户端对象不为空 假 以用户身份登录 IMAP 服务器:[email protected] 登录时出错:无法对空值表达式调用方法。

法典:

try {
    ### Import the dll
    [Reflection.Assembly]::LoadFile("C:\Users\bijoy.rc\Downloads\ImapX.2.0.0.18\lib\net35\ImapX.dll")
    ### Import the LinqBridge.dll
    [Reflection.Assembly]::LoadFile("C:\Users\bijoy.rc\Downloads\LinqBridge.1.3.0\lib\net20\LinqBridge.dll")
    ### Create a client object
    $client = New-Object ImapX.ImapClient
    ### Set the fetching mode to retrieve the part of the message you want to retrieve,
    ### the less the better
    $client.Behavior.MessageFetchMode = "Full"
    $client.Host = "imap.gmail.com"
    $client.Port = 993
    $client.UseSsl = $true

    try {
        $client.Connect()
        Write-Host "IMAP connection established"

        # Debug output to check the state of the $client object
        if ($client -ne $null) {
            Write-Host "Client object is not null"
            $user = "[email protected]"
            $password = "password"
            
            try {
                $client.Login($user, $password)
                Write-Host "Logged in to IMAP server as user: $user"
                
                $inbox = $client.Folders.Inbox
                $messages = $inbox.Search("ALL", $client.Behavior.MessageFetchMode, 1)
                
                if ($messages.Count -gt 0) {
                    $latestMessage = $messages[0]
                    $subject = $latestMessage.Subject
                    Write-Host "Latest email subject: $subject"
                } else {
                    Write-Host "No emails found in inbox."
                }
            } catch {
                Write-Host "An error occurred during login: $_"
            }
        } else {
            Write-Host "Client object is null"
        }
    } catch {
        Write-Host "An error occurred during connection: $_"
    }
} catch {
    Write-Host "An error occurred: $_"
}

我尝试连接到我的邮件服务器并使用 powershell 检索我最新的电子邮件主题。

PowerShell 电子邮件 IMAP IMAPX

评论

0赞 jdweng 8/20/2023
IMAP 是否打开?support.google.com/mail/answer/7126229?hl=en#zippy=

答: 暂无答案