Link to home
Start Free TrialLog in
Avatar of Lotfi BOUCHERIT
Lotfi BOUCHERITFlag for Algeria

asked on

Problem sending bulk email using Powershell

Hello,
I have a requirement to automate an operation of sending about 11,000 emails that should contain an attached picture as body of the email.
Those emails, should be sent 100 email / hour.
So i thought of writing a powershell script, that uses COM object of OUTLOOK, like this:

$Outlook = New-Object -comObject  Outlook.Application

$html=@"
      <body>
            
            <img src='cid:body.png' />
            </body>
"@





$file = get-content .\recipients.csv
$csv = $file | ConvertFrom-Csv

$myArray = @()
$rcptList = @()
$increment = 0
foreach($rcpt in $csv){
    $Mail = $null
    $Mail = $Outlook.CreateItem(0)

    $Mail.Subject = "SUBJECT"
    $Mail.HTMLBody= $html
    $Mail.Attachments.Add('D:\body.png', 0, 0)

    $s = $rcpt.recipient
    write-progress -Status "Adresse mail: $s" -Activity ACTIVITY -PercentComplete ($increment*100/100)
    if($increment -eq 100){
        Start-Sleep 3600
        $myArray | Export-Csv "resultats-$(get-date -f yyyy-MM-dd).csv"
        $increment = 0
        $myArray = @()
    }

    $rcptList+=$rcpt.recipient
    $obj = New-Object System.Object

    $obj | Add-Member -MemberType NoteProperty -Name Destinataire -Value $rcpt.recipient
    $myArray+=$obj
    $increment++

    ########
    $Mail.BCC=$s
    $mail.Send()

    Start-Sleep -Seconds 10
    get-process | where { $_.Name -like "Outlook" }| kill
}

for now, that code, works for the first line, but fails for the rest of the email addresses. And i cannot figure what i am missing? And i have errors starting from the second iteration of loop.
I even tried, to build an array of the BCC addresses, to reduce the number of the COM Object OUTLOOK calls to only one, like the following code, but didn't help. It says that the BCC type does not support array type


    foreach($email in $rcptList){
        $Mail.BCC+=$email
    }
Any help would be grateful.

Thanks in advance,
Avatar of David Favor
David Favor
Flag of United States of America image

Define "fails"... Best to provide whatever error message you're receiving...
Be aware, for this to work, you must have already gone through a checklist like...

https://www.experts-exchange.com/questions/29165628/Sending-Email-by-getting-email-address-from-database.html

Or some similar checklist.

Note: There's a big difference between sending an email + having said email actually delivered... then inboxed...
Avatar of Lotfi BOUCHERIT

ASKER

thank you,
the recipients are not from our organization, these are clients.
i changed the script, and it does not show errors, but does not work as expected:

[QUOTE]
function sendmail($recipient){
    $Outlook = New-Object -comObject  Outlook.Application

    $html=@"
        <body>
       
            <img src='cid:1594.png' />
            </body>
"@

    $Mail = $Outlook.CreateItem(0)

    $Mail.Subject = "1594"
    $Mail.HTMLBody= $html
    $Mail.Attachments.Add('D:\1594.png', 0, 0)

    $Mail.BCC='lotfi.boucherit@gmail.com'
    $mail.Send()

    Start-Sleep -Seconds 10
    get-process | where { $_.Name -like "Outlook" }| kill
}

$file = get-content D:\Users\l.boucherit\Desktop\recipients.csv
$csv = $file | ConvertFrom-Csv

$counter = 0
foreach($rcpt in $csv){
    if($counter -eq 100){
        #sleep
        Start-Sleep -Seconds 3600
        $counter = 0
    }
    sendmail($rcpt.recipient)
    Start-Sleep -Seconds 5
}
[/QUOTE]
I did find another built in cmdlet in powershell, which is send-mailmessage but couldn't find a switch that suits my need. If you can give any help, i'd be grateful.
Thanks in advance

ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial