Spire.Email for .NET is a professional .NET Email library specially designed for developers to create, read and manipulate emails from any .NET (C#, VB.NET, ASP.NET) platform with fast and high quality performance.

Wed Aug 18, 2021 5:03 pm

Hi. I've got the GetEmailText 'Main(args) partially working, but I don't know what args to use, so for the moment I've dropped them, as code below:
Code: Select all
    Private Shared Sub GetEmailText()           '(args As String())
        'Create an ImapClient instance
        Dim imap As New ImapClient()

        'https://www.e-iceblue.com/Knowledgebase/Spire.Email.html

        'Imports Spire.Email
        'Imports Spire.Email.IMap

        Try

            'Set host, port, authentication and connection protocol
            imap.Host = "outlook.office365.com"
            imap.Port = 143
            imap.Username = "x@y"
            imap.Password = "12345"
            imap.ConnectionProtocols = ConnectionProtocols.Ssl

            'Connect the imap server
            imap.Connect()

            'Select Inbox folder
            imap.[Select]("Inbox")

            'Search email messages sent from "From"
            Dim messages As ImapMessageCollection = imap.Search("'From' Contains 'xxx'")
            MsgBox("Number of messages sent from xxx: " & messages.Count, vbInformation, mCompanyName)

            'Search email messages with “Year” string in subject
            messages = imap.Search("'Subject' Contains '2021'")
            MsgBox("Number of messages with '2021' in subject: " & messages.Count, vbInformation, mCompanyName)

            Dim msgs As ImapMessageCollection
            msgs = imap.Search("'From' Contains 'xxxx'")

            MsgBox(msgs.Count & " 'xxxx' found.", vbInformation, mCompanyName)

            msgs = imap.Search("'From' Contains 'yyyy")

            MsgBox(msgs.Count & " 'yyyy' found.", vbInformation, mCompanyName)

            msgs = imap.Search("'Subject' Contains '2021'")

            MsgBox(msgs.Count & " '2021' found.", vbInformation, mCompanyName)


        Catch ex As Exception
            MsgBox(ex.Message, vbExclamation, mCompanyName)
        End Try

    End Sub


The message boxes show what looks to be the right numbers, but I can't extract the email body text. What should I put to read the collection of body texts? I tried several examples from various Spire questions and responses but the line crashed with eg. '019 bad the specified message set is invalid'.
Perhaps when I've got the args correct? Any help gratefully received. Thank you.

Jonathan99
 
Posts: 6
Joined: Wed Aug 18, 2021 4:51 pm

Thu Aug 19, 2021 7:30 am

Hello,

Thank you for your inquiry.
Please use the code below to extract the body text of the email. If there is any question, please feel free to write back.
Code: Select all
Dim imap As ImapClient = New ImapClient
'Set host, port, authentication and connection protocol
imap.Host = "outlook.office365.com"
imap.Port = 143
imap.Username = "x@y"
imap.Password = "12345"
imap.ConnectionProtocols = ConnectionProtocols.Ssl
'Connect the imap server
imap.Connect
'Select Inbox folder
imap.Select("inbox")
Dim sb As StringBuilder = New StringBuilder
Dim msgs As ImapMessageCollection = imap.Search("'Subject' Contains '2021'")
Dim i As Integer = 0
Do While (i < msgs.Count)
    Dim originalMail As MailMessage = imap.GetMessageText(msgs(i).SequenceNumber)
    ' Extract the body text
    Dim bodyText As String = originalMail.BodyText
    sb.Append(bodyText)
    i = (i + 1)
Loop

File.WriteAllText("bodyText.txt", sb.ToString)
MsgBox((msgs.Count + " '2021' found."), System.Windows.Forms.MessageBoxIcon.Information, mCompanyName)

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Thu Aug 19, 2021 2:12 pm

Thanks, Annika.

Jonathan99
 
Posts: 6
Joined: Wed Aug 18, 2021 4:51 pm

Fri Aug 20, 2021 1:52 am

Hello,

You're welcome.
If you have other questions about using Spire.Email in the future, please feel free to contact us.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Email