下载的电子邮件附件ImapX中缺少回车符

时间:2014-04-04 20:14:53

标签: vb.net email email-attachments imapx

这是我的代码:

#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

代码没有问题 - 它连接到imap服务器,下载第一封电子邮件的第一个附件,恰好是.txt文件,因此我保存它。

问题是该文件的内容前缀为" * 2 FETCH("后跟" UID 45",,所有回车都从文件中删除

你能帮忙吗?谢谢,

1 个答案:

答案 0 :(得分:0)

我的猜测是CRLF(回车符)不是您正在查找正在查找的下载内容的格式。我会使用像NotePad ++这样的东西看你的附件,并确保你显示回车。如果您看到的只是cr并且您正在查找文件中寻找crlf,那么它们将被忽略。

要看的另一件事是默认编码是你的.download调用使用以及原始附件的编码。