0

Here is my code:

#Region "Imports"
Imports System.Text.RegularExpressions
Imports System.Text
Imports Microsoft.VisualBasic.CallType
Imports ImapX
Imports System.Runtime.CompilerServices
Imports System.Security.Authentication
Imports System.IO
Imports ml = System.Net.Mail
Imports System.Net
Imports ImapX.Enums
Imports ImapX.Constants
Imports System.Security.Authentication.SslProtocols
#End Region

Module Module1
Sub Main()
    Dim _messages As List(Of ImapX.Message)

    Using MyImapClient = New ImapX.ImapClient
        With MyImapClient
            .Host = ImapServer  
            .Port = Port    
            .SslProtocol = Ssl3 Or Tls
            .ValidateServerCertificate = True

            .Credentials = New ImapX.Authentication.PlainCredentials(UserName, Password)
            Dim IsConnected As Boolean = .Connect
            .Login()
            .Behavior.AutoDownloadBodyOnAccess = False
            .Behavior.AutoPopulateFolderMessages = False
            .Behavior.MessageFetchMode = MessageFetchMode.Full
            .Behavior.ExamineFolders = False
            .Behavior.RequestedHeaders = {MessageHeader.From, MessageHeader.[Date], MessageHeader.Subject, MessageHeader.ContentType, MessageHeader.Importance}
            'Dim IsInboxSelected As Boolean = .SelectFolder(.Folders.Inbox.Name)
            'Dim IsInboxSelected As Boolean = .Folders(.Folders.Inbox.Name).[Select]()
        End With

        Dim MyFolder As Folder = MyImapClient.Folders.Inbox            
        _messages = MyFolder.Search().OrderBy(Function(n) n.[Date]).ToList()
        _messages.ForEach(Sub(n) n.Download(MessageFetchMode.Full))         
        _messages.ForEach(Sub(n) n.Download(MessageFetchMode.Full))         
     End Using

    Dim MyAttachment As ImapX.Attachment = _messages.First.Attachments.First
    MyAttachment.Download()

    Dim FolderPath As String = "C:\Users\AAA\Downloads\"
    Dim LocalFileName As String = "1212.txt"
    MyAttachment.Save(FolderPath, LocalFileName)

End Sub

End Module

The code works without issue--it connects to the imap server, downloads the first attachment of the first email, which happens to be a .txt file, so I'm saving it as such.

The problem is that the contents of the file is prepended with " * 2 FETCH (" and is followed by " UID 45", and all carriage returns are removed from the file.

Can you please assist? Thanks,

as9876
  • 934
  • 1
  • 13
  • 38
  • The issue with missing CRLFs has already been fixed, you can download the latest code from the [repository](https://imapx.codeplex.com/SourceControl/latest). The issue about junk data, such as `* 2 FETCH (` in the body is known, an update is coming soon. – Pavel Azanov Apr 05 '14 at 21:35
  • @PavelAzanov, I'm using the library which was uploaded on Dec 16, 2013 and it's still not working (I tried adding source, dll, and NuGet). Is there something wrong with my code? TY. – as9876 Apr 07 '14 at 20:58
  • The binaries in download section, as well as the Nuget package has the old version. To get the latest code, please use this link: https://imapx.codeplex.com/SourceControl/latest. I made more updates on your issue, not it should work fine. However, the Internet Message Format ha some limitation which makes it nearly impossible to restore the right line breaks for 7bit/8bit plain text message parts. – Pavel Azanov Apr 13 '14 at 11:45
  • Is that just for body text? I used the latest link you sent and it's still not working for attachments. BTW, I'm not sure if this would help: http://www.codeproject.com/Articles/17201/Detect-Encoding-for-In-and-Outgoing-Text – as9876 Apr 18 '14 at 18:50
  • It's not only for body text, but also for 7/8bit plain text attacments. Can you show me original file you're trying to download? Just send it to p13a92@googlemail.com – Pavel Azanov Apr 21 '14 at 14:03
  • Thank you for the message! I have tested it and replied to your mail. Using the latest code from the repository (https://imapx.codeplex.com/SourceControl/latest), I see no CRLFs missing in the downloaded file, everything is working fine. – Pavel Azanov Apr 25 '14 at 22:10

1 Answers1

0

My guess is the CRLF's ( Carriage Return Line Feeds ) are not the format whatever you're viewing the download in is looking for. I would look at your attachment using something like NotePad++ and make sure you're showing Carriage Returns. If all you see are cr's and you're viewing the file in something looking for crlf's then they will get ignored.

Another thing to look at is what default encoding is your .download call using and what encoding is the original attachment in.

thetimmer
  • 186
  • 1
  • 8
  • TY. In regular IMAP client, like Outlook, Notepad++ (w/ all symbols on) shows the carriage returns, but when using IMAPX library it doesn't. Is there a way I can change the encoding? – as9876 Apr 04 '14 at 21:34