Solved

Email From Powershell

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

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 19

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 69

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 19

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

In-place Upgrading Dirsync to Azure AD Connect
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this video we show how to create a Shared Mailbox 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 >> Sha…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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