Solved

Email From Powershell

Posted on 2014-01-16
5
399 Views
Last Modified: 2014-01-21
I am trying to find a good code where I can send email through a Powershell Script.  

Here is the situation:  
We have 6,550 contacts we need to verify to make sure they are still valid or not.  We want to shot them an email but not kill our reputation online against the Spam filters.

What we need:
Is a powerhsell scrip that can read from a text document that has email addresses and grab 30 at a time every 10-15 seconds and send an email to them.  We need to be able to specify who its from (will always be the same account) and it will always have the same message in the body.

Any help would be greatly appreciated!
0
Comment
Question by:Sedgwick_County
  • 2
  • 2
5 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39787039
Roughly that should do that:
$Body = @"
The email body text,
in several lines.
Could also be a text or html file
"@

$cnt = 0
foreach ($to in get-content C:\EMailAddress.txt)
{
  Send-MailMessage -SmtpServer mail.here.com -From me@here.com -To $to -Subject 'TestMail' -Body $body
  if ((++$cnt % 30) -eq 0)
  {
    Write-Host 'Waiting for 15 seconds ...'
    Start-Sleep 15 
  }
}

Open in new window

This can be expanded by adding the -BodyAsHtml to Send-MailMessage if your mail body consists of HTML code.
0
 
LVL 18

Expert Comment

by:Raheman M. Abdul
ID: 39787068
Function Mailer ($emailTo)
{
   $message = @"
                                 
Some stuff that is meaningful  
 
Thank you,
IT Department
Cotendo Corporation
it@cotendo.com
"@        
 
$emailFrom = "noreply@<yourdomain>.com"
$subject="<Your Text Here>"
$smtpserver="<your mailhost>.<yourdomain>.com"
$smtp=new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $message)
}
$i=0
$Addresses=get-content c:\contacts.txt
Foreach($address in $Addresses)
{

Mailer($address)
if($i%15 -eq 0)
{
  Start-sleep -m 10
}
  $i=$i+1
}
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39787080
$i%15 will build blocks of 15 mails (instead of 30), and Start-sleep -m 10 only waits 10 millisec ...
0
 
LVL 18

Expert Comment

by:Raheman M. Abdul
ID: 39787087
Oops, thought 15 in bunch
Thanks.
0
 

Author Closing Comment

by:Sedgwick_County
ID: 39797528
That worked perfectly!  Thank you VERY much on your fast response.
0

Featured Post

Do email signature updates give you a headache?

Constantly trying to correctly format email signatures? Spending all of your time at every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this video we show how to create a Distribution Group in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >>…
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now