关闭

Powershell 通过POP3接收邮件

540人阅读 评论(0) 收藏 举报
分类:


################################################################################
#POP3接收邮件代码
function Get-Pop3Config(){
    $config=New-Object psobject
    $config|Add-Member -MemberType NoteProperty -Name POP3Server -Value "partner.outlook.cn"
    $config|Add-Member -MemberType NoteProperty -Name POP3Port -Value 995
    $config|Add-Member -MemberType NoteProperty -Name EmailAddress -Value "xxx@xxx.com"
    $config|Add-Member -MemberType NoteProperty -Name Passwd -Value "123456"
    $config|Add-Member -MemberType NoteProperty -Name OnlyReceiveHeader -Value "true"
    $config|Add-Member -MemberType NoteProperty -Name ClassFilePath -Value "c:\LumiSoft.Net.dll"
    return $config
}
function Connect-POP3Server(){
    $script:Pop3Config=Get-Pop3Config
    [void][reflection.assembly]::LoadFile($Pop3Config.ClassFilePath)
    $pop3=New-Object LumiSoft.Net.POP3.Client.POP3_Client
    if($pop3.IsConnected -ne 'true'){
        $pop3.Connect($Pop3Config.POP3Server,$Pop3Config.POP3Port,$true)
        $pop3.Login($Pop3Config.EmailAddress,$Pop3Config.Passwd)
    }
    $messages=$pop3.Messages
    $counts=$messages.Count
    #Write-Host "邮箱中总共有 $counts 封邮件" -ForegroundColor Green
    $rh=Read-Host "邮箱中总共有 $counts 封邮件,是否继续(Y/N)"
    if($rh -eq "y"){
        return $messages
    }else{
        break
    }
}

function Convert-ToByte(){
    $messages=Connect-POP3Server
    $allMessage=New-Object System.Collections.ArrayList
    foreach($message in $messages){
        if($message -ne $null){
            $psobject=New-Object psobject
            #$messageBytes=$message.MessageToByte()
            if($Pop3Config.OnlyReceiveHeader -eq 'true'){
                $messageString=[LumiSoft.Net.Mail.Mail_Message]::ParseFromByte($message.HeaderToByte())
            }else{
                $messageBytes=$message.MessageToByte()
                $messageString=[LumiSoft.Net.Mail.Mail_Message]::ParseFromByte($messageBytes)
            }
            $psobject| Add-Member -MemberType NoteProperty -Name From -Value $messageString.From
            $psobject| Add-Member -MemberType NoteProperty -Name To -Value $messageString.To
            $psobject| Add-Member -MemberType NoteProperty -Name Cc -Value $messageString.cc
            $psobject| Add-Member -MemberType NoteProperty -Name Bcc -Value $messageString.bcc
            $psobject| Add-Member -MemberType NoteProperty -Name Subject -Value $messageString.subject
            $psobject| Add-Member -MemberType NoteProperty -Name Body -Value $messageString.BodyHtmlText
           
            #$message.MarkForDeletion()
            [void]$allmessage.Add($psobject)
        }
    }
    return $allMessage
}

Convert-ToByte|ft

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15308次
    • 积分:332
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论