0

这是我的代码:

#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”,所有回车都从文件中删除了

你能帮忙吗?谢谢,

4

1 回答 1

0

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

要查看的另一件事是您的 .download 调用使​​用的默认编码是什么,以及原始附件的编码是什么。

于 2014-04-04T21:09:05.567 回答