使用IMAPX VB.NET检索电子邮件 [英] Retrieve emails using IMAPX VB.NET

查看:109
本文介绍了使用IMAPX VB.NET检索电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照这篇文章



我想要做的是使用IMAPX检索电子邮件。问题是,当我创建一个类来处理使用此代码的所有函数时:



I tried to follow this article

What i am trying to do is retrieve emails using IMAPX.The problem is, when i create a class to handle all the function using this code :

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports ImapX
Imports ImapX.Collections
Imports System.Windows

Namespace ImapPackage

    Class ImapService

        Private Shared Property client As ImapClient

        Public Shared Sub Initialize()
            client = New ImapClient("imap.gmail.com", True)
            If Not client.Connect() Then
                Throw New Exception("Error connecting to the client.")
            End If
        End Sub

        Public Shared Function Login(ByVal u As String, ByVal p As String) As Boolean
            Return client.Login(u, p)
        End Function

        Public Shared Sub Logout()
            If client.IsAuthenticated Then
                client.Logout()
            End If

            MainWindow.LoggedIn = False
        End Sub

        Public Shared Function GetFolders() As List(Of EmailFolder)
            Dim folders = New List(Of EmailFolder)()
            For Each folder In client.Folders
                folders.Add(New EmailFolder With {.Title = folder.Name})
            Next

            client.Folders.Inbox.StartIdling()
            client.Folders.Inbox.OnNewMessagesArrived += AddressOf Inbox_OnNewMessagesArrived
            Return folders
        End Function

        Private Shared Sub Inbox_OnNewMessagesArrived(ByVal sender As Object, ByVal e As IdleEventArgs)
            MessageBox.Show($"A new message was downloaded in {e.Folder.Name} folder.")
        End Sub

        Public Shared Function GetMessagesForFolder(ByVal name As String) As MessageCollection
            client.Folders(name).Messages.Download()
            Return client.Folders(name).Messages
        End Function
    End Class
End Namespace





我得到的例外:



1•未定义EMAILFOLDER类型

2•公共事件OnNewMessageArrived因为EventHandler(Of IdleEVentArgs)是一个事件,不能直接调用。使用RaiseEvent语句



第二个问题不是那个严重,但我如何修复第一个?我缺少什么?



我的尝试:



随便我试过在上面的帖子中提到。



The exception i get :

1 • Type EMAILFOLDER is not defined
2 • Public event OnNewMessageArrived as EventHandler(Of IdleEVentArgs) is an event and cannot be called directly.Use a RaiseEvent statement

The 2nd problem is not that serious but how do i fix the 1st one ? What am i missing ?

What I have tried:

Whatever i tried is mentioned in the post above .

推荐答案

在{e.Folder.Name}文件夹中下载了一条新邮件。)
End Sub

公共共享函数GetMessagesForFolder(ByVal name As String)As MessageCollection
client.Folders(name).Messages.Download()
返回client.Folders(name).Messages
结束函数
结束类
结束命名空间
"A new message was downloaded in {e.Folder.Name} folder.") End Sub Public Shared Function GetMessagesForFolder(ByVal name As String) As MessageCollection client.Folders(name).Messages.Download() Return client.Folders(name).Messages End Function End Class End Namespace





我得到的例外:



1•未定义类型EMAILFOLDER

2•公共事件OnNewMessageArrived为EventHandler(Of IdleEVentArgs)是一个事件,无法直接调用。使用RaiseEvent语句



第二个问题并不严重,但如何解决第一个问题呢?我缺少什么?



我的尝试:



随便我试过在上面的帖子中提到。



The exception i get :

1 • Type EMAILFOLDER is not defined
2 • Public event OnNewMessageArrived as EventHandler(Of IdleEVentArgs) is an event and cannot be called directly.Use a RaiseEvent statement

The 2nd problem is not that serious but how do i fix the 1st one ? What am i missing ?

What I have tried:

Whatever i tried is mentioned in the post above .


你已经在文章论坛中提问过了(使用C#在WPF中构建自定义电子邮件客户端 [ ^ ])。



Please有点耐心。我认为作者会回答(他仍然活跃在CP,但可能不会每天登录)。他还发了一封关于你的论坛帖子的通知邮件,以便他知道。



然而, EmailFolder class在 HomePage.xaml.cs 中定义:

You have already asked in the article forum (Building custom email client in WPF using C#[^]).

Please be a little patient. I think that the author will answer (he is still active her at CP but might not login daily). He also gots a notification mail about your forum post so that he knows about it.

However, the EmailFolder class is defined in HomePage.xaml.cs:
class EmailFolder
{
    public string Title { get; set; }
}

所以你必须创建一个相应的VB.Net类,或者只使用 String

So you have to create a corresponding VB.Net class or just use a String instead.


对于第二个问题,您转换的代码稍微关闭。 VB.NET使用 AddHandler [ ^ ]至连接事件处理程序,而不是C#使用的 + = 语法。

For the second problem, your converted code is slightly off. VB.NET uses AddHandler[^] to wire up event handlers, not the += syntax that C# uses.
AddHandler client.Folders.Inbox.OnNewMessagesArrived, AddressOf Inbox_OnNewMessagesArrived


这篇关于使用IMAPX VB.NET检索电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆