微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

电子邮件附件和正文命名

如何解决电子邮件附件和正文命名

我正在下载电子邮件正文及其附件。我想确保每个发件人在文件名上都附有数字,以区别于其他文档。计数器应按每个发送方启动。例如 发件人1 Sender1-Emailbody-0 Sender1-pdf附件1 Sender1-jpg附件-2

发件人2 Sender2-Emailbody-0 Sender2-pdf附件-1 Sender2-jpg附件-2

我不确定该如何处理。请帮忙。谢谢

$emailQuery1 = $url + "?`$select=Id,Sender,DateTimeReceived&`$filter=HasAttachments eq true and DateTimeReceived ge " + $datetime
$emails = Invoke-RestMethod -Uri $emailQuery1 -Headers $head
## Loop through each results
Foreach ($email in $emails.value)

{
  #get attachments and save to file system
   $query = $url + "/" + $email.Id + "/attachments"
   $body = $url + "/" + $email.Id + "/body"
   $receivedtime = "/" + $email.id + "/DateTimeReceived"
   $attachments = Invoke-RestMethod -Uri $query -Headers $head
   $emailbody = Invoke-RestMethod -Uri $body -Headers $head

  Foreach($em in $emailbody)
  {
     $bodyitems = $em.content
  Foreach ($attachment in $attachments.value)
  {
     $attachment.Name
     $path = $DFSPath + $recvtime +"_" + $email.Sender.EmailAddress.Address + "_" + $attachment.Name 
     $Content = [System.Convert]::FromBase64String($attachment.ContentBytes)
     $recvtime = $email.DateTimeReceived | get-date -Format yyyyMMddHHmmss 
     $bodyitems | out-file -FilePath ($DFSPath +$recvtime +"_" + $email.Sender.EmailAddress.Address` 
     +"_emailbody-" +$Counter+".html")
     Set-Content -Path $path -Value $Content -Encoding Byte
    }
  }
}

解决方法

我建议使用一个计数变量,该变量会在保存文件之前立即增加。在foreach电子邮件循环$x=0的开始处将其设置为0,然后将$x++放在Out-File命令之前。保存文件后,可以将其附加到文件名。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。