Powershell's POP3 Mail management

Source: Internet
Author: User
Tags imap reflection email account mailmessage

################################################################################ #配置信息 $config =new-object  Psobject$config| add-member -membertype noteproperty -name server -value  "Mail.xxx.com"                                   #服务器地址 $config | add-member -membertype noteproperty -name pop3port -value 995                                            #POP3协议端口 $config | add-member -membertype noteproperty -name imapport -value 993                                          # IMAP protocol Port $config| add-member -membertype noteproperty -name account -value  "[email protected] "                                #账号 $config | add-member -membertype noteproperty -name passwd -value  "Aa123456"                                       #密码 $config | add-member -membertype noteproperty -name onlyreceiveheader -value  "false"                               #是否只下载邮件头信息 (true-enabled, false-disabled) $config | add-member -membertype noteproperty -name resultsize -value  "One"                                         #设置分页大小 $ Config| add-member -membertype noteproperty -name enablepage -value  "true"                                       #是否启用分页设置 (true-enabled, false-disabled) $config | add-member -membertype noteproperty -name downattachpath -value  "false"                                  #是否下Load attachments (true-enabled, false-disabled) $config | add-member -membertype noteproperty -name attachpath -value  "D:\PSTemplate\ Attachments "               #邮件附件存放地址 $ Config| add-member -membertype noteproperty -name classfilepath -value  "c \ LumiSoft.Net.dll "                   #代码库文件 #pop3 Agreement function login-pop3{    [void][reflection.assembly]:: LoadFile ($Config. Classfilepath)      $pop 3=new-object lumisoft.net.pop3.client.pop3_ Client    if ($pop 3. isconnected -ne  ' true ') {         $pop 3. Connect ($Config. Server, $Config. Pop3port, ' true ')         try{$pop 3. Login ($Config. Account, $Config. Passwd)}catch{write-warning  "Unable to log on to the server"; write-host  $Error [0];break}    }    return  $pop 3} #IMAP协议function  Login-IMAP{     [void][reflection.assembly]::loadfile ($Config. Classfilepath)      $iMAP = New-object lumisoft.net.imap.client.imap_client    if ($iMAP. IsConnected -ne   "true") {         $iMAP. Connect ($Config. Server, $Config. imapport,$ true)         try{$iMAP. Login ($Config. Account, $Config. Passwd)}catch{ write-warning  "Unable to log on to the server"; write-host  $Error [0];break}    }    return  $iMAP}# Download Accessories function download-attach{    param ($MailString)     $ attachments= $MailString. Getattachments ($true, $true)     foreach ($Entity  in $ Attachments) {        if ($Entity .contentdescription -ne  $null ) {             $FileName = $Entity .contentdescription             if ($FileName  -ne  $null) {                 [system.io.directoryinfo] $Dir =$ Config. attachpath+ "\" + $UID                  if (!  $Dir. Exists) {                      $Dir. Create ()                  }                 [string] $Path =[system.io.path]::combine ($Dir. FullName, $FileName)                  [lumisoft.net.mime.mime_b_ Singlepartbase] $ByteObj= $Entity .body                 [system.io.stream] $DecodedDataStream = $ByteObj. Getdatastream ()                  try{                      $FS =new-object  System.IO.FileStream ($Path, [system.io.filemode]::create)                  }finally{                     [void][lumisoft.net.net_utils]::streamcopy ($ Decodeddatastream, $FS, 4000)                  }                  $FS. Dispose ()                 Remove-Variable  ' FS '                  Remove-Variable  ' Decodeddatastream '                  Write-Host  "$Path   file has been successfully downloaded! "&NBSP;-FOREGROUNDCOLOR&NBSP;GREEN&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}         }    }} #格式化数据function  data-format{      $psobject =new-object psobject     $psobject | add-member  -MemberType NoteProperty -Name ID -Value  $i      $psobject |  Add-Member -MemberType NoteProperty -Name UID -Value  $UID      # $psobject | add-member -membertype noteproperty -name isdisposed -value  $IsDisposed      $psobject | add-member -membertype noteproperty  -Name Date -Value  $MailString .date     $psobject | add-member  -MemberType NoteProperty -Name From -Value  $MailString .from.address      $psobject | add-member -membertype noteproperty -name to -value  $MailString .to.address     $psobject | add-member -membertype noteproperty  -Name Cc -Value  $MailString .cc.address     $psobject | add-member  -MemberType NoteProperty -Name Bcc -Value  $MailString .bcc.address      $psobject | add-member -membertype noteproperty -name subject -value   $MailString .subject     $psobject | add-member -membertype noteproperty  -name body -value  $MailString .bodyhtmltext     $psobject | add-member -membertype  NoteProperty -Name Size -Value  $MailSize      $psobject |  add-member -membertype noteproperty -name messageid -value  $MailString. MessageID      $psobject | add-member -membertype noteproperty -name  ismarkedfordeletion -value  $IsMarkedForDeletion     return  $psobject}# function get-mailmessage{     $pop 3=login-pop3     $AllMails =$ pop3.messages | sort-object -property sequencenumber -descending     if (! $AllMails) {        write-warning  "Current email Account no mail"          exit 1    }     #总邮件数      $Counts = $AllMails. Count     #第一封邮件编号     #[int] $FirstNumber = $AllMails [0]. sequencenumber     #分页设置     if ($config. enablepage -eq  ' true ') {        [int] $ResultSize = $config. resultsize    }else{         $ResultSize = $Counts     }     #动态数组整合邮件信息      $Lists =new-object  system.collections.arraylist     #编号      $i =1     write-host  "Total Current Account"  -NoNewline    Write-Host  $Counts   - foregroundcolor green -nonewline    write-host  "e-mail,"  -NoNewline      #如果邮件总数小于分页数, remove all data     if ($Counts  -le  $ResultSize) {         Write-Host  "has got you the latest" &NBSP;-NONEWLINE&NBsp;       write-host  "$Counts"  -nonewline -foregroundcolor  Green        Write-Host  "e-mail. "        foreach ($Mails  in  $AllMails) {             [string] $IsDisposed = $Mails .isdisposed             [int] $Number = $Mails .sequencenumber             [int] $UID = $Mails .uid             [int] $MailSize = $Mails .size             [string] $IsMarkedForDeletion = $Mails .ismarkedfordeletion             if ($config. onlyreceiveheader -eq  ' True ') {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;     $MailString =[lumisoft.net.mail.mail_message]::P arsefrombyte ($Mails. Headertobyte ())             }else{                  $MailBytes = $Mails. Messagetobyte ()                   $MailString =[ LumiSoft.Net.Mail.Mail_Message]::P arsefrombyte ($MailBytes)                  ###### #处理附件                  if ($config. downattachpath -eq  ' true ') {                     Download-Attach  $MailString                  }                #######             }              $DataFormat =data-format            [ VOID] $Lists. ADD ($DataFormat)              $i ++         }        return  $Lists         Remove-Variable  ' Lists '     }else{         Write-Host  "Get the latest"  -NoNewline         Write-Host  "$ResultSize"  -nonewline -foregroundcolor green         Write-Host  "e-mail. "         $j =1&NBSP;&NBSp;      foreach ($Mails  in  $AllMails) {             if ($j  -gt  $ResultSize) {                  $Lists                  Remove-Variable  ' Lists '                   $Lists =new-object  system.collections.arraylist                 Write-Host  "Do you want to continue, extract each time"  -NoNewline                 Write-Host  $ResultSize  -ForegroundColor  green -nonewline                 Write-Host   "Records   (y/n): "                 $rh = Read-host                if ($ rh -eq  "n") {                     return  $Lists                  }else{                      $j =1                 }             }            [string] $IsDisposed = $Mails. Isdisposed            [int] $Number =$ Mails.sequencenumber  &nbSp;         [int] $UID = $Mails .uid             [int] $MailSize = $Mails .size             [string] $IsMarkedForDeletion = $Mails .ismarkedfordeletion             if ($config. onlyreceiveheader -eq  ' True ') {                  $MailString =[lumisoft.net.mail.mail_message]::P arsefrombyte ($Mails. Headertobyte ( ))             }else{                  $MailBytes = $Mails. Messagetobyte ()                   $MailString =[ LumiSoft.Net.Mail.Mail_Message]::P arsefrombyte ($MailBytes)                  ###### #处理附件                  if ($config. downattachpath -eq  ' true ') {                     Download-Attach  $MailString                  }                 #######             }             $DataFormat = Data-format            [void] $Lists. ADD ($ DataFormat)              $i ++              $j ++        }        return  $Lists         Remove-Variable  ' Lists '     }} <# $iMAP =login-imap$imap.selectfolder (' Inbox ') $seqSet =[lumisoft.net.imap.imap_t_seqset]::P arse ("1000:*") $a = New-object lumisoft.net.imap.imap_t_fetch_i_envelope$b=new-object lumisoft.net.imap.imap_t_fetch_i_ Uid$c=new-object lumisoft.net.imap.imap_t_fetch_i_flags$d=new-object lumisoft.net.imap.imap_t_fetch_ i_internaldate$e=new-object lumisoft.net.imap.imap_t_fetch_i_rfc822#new-object  Lumisoft.net.imap.imap_t_fetch_i#>get-mailmessage | out-gridview


This article is from the "bxing" blog, make sure to keep this source http://bxing.blog.51cto.com/1677994/1875910

Powershell's POP3 Mail management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.