SMTP Multiple emails with attachements Powershell

Hello guys, my first post here and I was hoping someone could help me a little bit. I was working on a powershell project that sends an email with multiple attachements from a directory. Filenames i this directory will change all the time. My code so far is:

#Connection Details
$username=jfgs@gmail.com
$password=xxxxxxxx
$smtpServer = smtp.gmail.com
$msg = new-object Net.Mail.MailMessage

#Change port number for SSL to 587
$smtp = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 

#Uncomment Next line for SSL  
$smtp.EnableSsl = $true

$smtp.Credentials = New-Object System.Net.NetworkCredential( $username, $password )

#From Address
$msg.From = "CMSUPPORT@NFO.no"
#To Address, Copy the below line for multiple recipients
$msg.To.Add(Chris@NFO.no)

#Message Body
$msg.Body=Please See Attached Database Files

#Message Subject
$msg.Subject = DATABASE
write-host "SENDING FILES"

#your file location
$files=Get-ChildItem C:\CM\Send\
Foreach($file in $files)
{
Write-Host Attaching File :- $file
$attachment = New-Object System.Net.Mail.Attachment ArgumentList C:\CM\Send\$file
$msg.Attachments.Add($attachment)

}
$smtp.Send($msg)
$attachment.Dispose();
$msg.Dispose();
write-host "Mail Sent Successfully"

And this code works just fine for this, then I realize that gmail only has 25 MB max attachement size and the files I want to send is around 18 mb. 

Anyone have any good ideas to split all files in the directory into seperate emails. i.e one email per file? Having trouble doing so because the filenames keep changing daily.

I am a beginner in this kind of language, but anything will help to get me on the right track :-)

Thank you.

January 10th, 2014 10:32am

Hi Christopher,

welcome to Technet!

One piece of advice right away: The Technet forums have a great functionality to post code ("Insert Code Block"). Using it will get you something like what I've posted below.

If you know that each of your files will be smaller than your limit, you may be able to get away with this:

#your file location
$files = Get-ChildItem "C:\CM\Send"
Foreach($file in $files)
{
	Write-Host "Attaching File :- " $file
	$attachment = New-Object System.Net.Mail.Attachment ArgumentList $file.FullName
	$msg.Attachments.Add($attachment)
	$smtp.Send($msg)
	$msg.Attachments.Clear()
}

What this does is get all files in the folder, and for each file, it attaches it to the mail, sends the mail and then clears the attachments.

Cheers,

Free Windows Admin Tool Kit Click here and download it now
January 10th, 2014 12:28pm

Hello FWN and thank you for your reply. I will use that functionality next time for sure :-)

The code worked perfectly, I know my files will always be under 25 MB, so that is not an issue. Thank you very much for your help! :-)


January 10th, 2014 12:42pm

You should be using Send-MailMessage as it makes the scripting cleaner and also cleans up the objects correctly.  You are never disposing of the attachments so the files will remain locked. This could have negative impact on external systems.  The mail CmdLet does not have this problem and does its own attachment management,
Free Windows Admin Tool Kit Click here and download it now
January 10th, 2014 2:28pm

For some reason, Send-MailMessage didn't have a -Port parameter in PowerShell 2.0, so make sure you've upgraded to at least PowerShell 3.0 if you need to use it with gmail (or any other SMTP server running on a port other than 25.)

I have a PowerShell 2.0-compatible version of Send-MailMessage (including a Port parameter, and support for inline attachments) up on the TechNet gallery, if that is helpful:  http://gallery.technet.microsoft.com/Send-MailMessage-3a920a6d

January 10th, 2014 4:54pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics