Solved

Email From Powershell

Posted on 2014-01-16
5
400 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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Find out what you should include to make the best professional email signature for your organization.
Read this checklist to learn more about the 15 things you should never include in an email signature.
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
how to add IIS SMTP to handle application/Scanner relays into office 365.

910 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

21 Experts available now in Live!

Get 1:1 Help Now