如何使用Powershell中的Get-IMAP NetCmdlets保存IMAP电子邮件正文文本?

时间:2017-12-12 16:29:08

标签: powershell imap

我正在尝试使用netcmdts捕获多部分和纯文本电子邮件的邮件正文。连接成功,电子邮件被提取,我可以保存附件没有问题,但我不知道如何保存正文。

import-module "C:\Program Files\nsoftware\NetCmdlets V4\netcmdlets"

$emails = get-imap -Connect $imapconnection -Folder $folder
foreach($msg in $emails)
{
    if($msg.ContentType.StartsWith("multipart"))
    {
        # get message body text for multipart?
    }
    else
    {
        # get message body text for non multipart??
    }
}

修改

get-imap -Connect $imapconnection -Folder $folder | Get-Member

给出

TypeName: nsoftware.NetCmdlets.Commands.MessageInfoObject

Name                MemberType Definition
----                ---------- ----------
Equals              Method     bool Equals(System.Object obj)
GetHashCode         Method     int GetHashCode()
GetType             Method     type GetType()
ToString            Method     string ToString()
BCC                 Property   string[] BCC {get;set;}
CC                  Property   string[] CC {get;set;}
ContentEncoding     Property   string ContentEncoding {get;set;}
ContentType         Property   string ContentType {get;set;}
Date                Property   string Date {get;set;}
Flags               Property   string Flags {get;set;}
From                Property   string From {get;set;}
FromEmail           Property   string FromEmail {get;set;}
FromFriendlyName    Property   string FromFriendlyName {get;set;}
Id                  Property   string Id {get;set;}
PartContentEncoding Property   string[] PartContentEncoding {get;set;}
PartContentType     Property   string[] PartContentType {get;set;}
PartCount           Property   int PartCount {get;set;}
PartFileName        Property   string[] PartFileName {get;set;}
PartId              Property   string[] PartId {get;set;}
PartMultiPartMode   Property   string[] PartMultiPartMode {get;set;}
PartParameters      Property   string[] PartParameters {get;set;}
PartSize            Property   string[] PartSize {get;set;}
ReplyTo             Property   string ReplyTo {get;set;}
ReplyToEmail        Property   string ReplyToEmail {get;set;}
ReplyToFriendlyName Property   string ReplyToFriendlyName {get;set;}
Size                Property   long Size {get;set;}
Subject             Property   string Subject {get;set;}

1 个答案:

答案 0 :(得分:0)

从文件夹中获取所有消息将返回MessageInfoObjects的集合,但是为了获取电子邮件正文本身,您需要通过将消息Id传递给-View参数来获取特定消息。您可以从MessageInfoObject中获取Id,因此您可能有这样的逻辑:

$emailList = Get-IMAP -Connection $conn -Folder INBOX
$currentEmail = Get-IMAP -Connection $conn -Folder INBOX -View $emailList[0].Id

此时$ currentEmail.Text将返回该消息的正文(收件箱中的第一条消息)。