-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsend-mail.ps1
71 lines (57 loc) · 2.11 KB
/
send-mail.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
###
#
# Author
# Navid Azimi (nazimi)
#
# Send Mail
# This script sends an arbitrary email from the current logged on user
# provided their account supports sending email. For more information,
# please see: http://infoplus30/getdocument.aspx?documentid=d07-ze
#
# Params
# $to list of email recipients
# $subject the subject of the email message
# $body the content of the email
# $text if present, indicates this is a plain-text message (not HTML)
# $whatif if present, does not send the email but outputs
#
###
param([string] $to = $(throw "You must specify at least one recipient."), [string] $subject, [string] $body, [switch] $text, [switch] $whatIf);
$private:from = "$env:[email protected]";
$private:smtp = "smtphost.redmond.corp.microsoft.com";
## create an empty mail message
$private:message = new-object Net.Mail.MailMessage;
## if we fail to send this message, notify the sender
$message.DeliveryNotificationOptions = [Net.Mail.DeliveryNotificationOptions]::OnFailure;
## split the to line based on spaces, commas or semi-colons
$addresses = $to.Split(' ,;');
## iterate through all the recipients and add them to the TO line
foreach($address in $addresses)
{
$message.To.Add($address);
}
## set the message from the current logged on user
$message.From = new-object Net.Mail.MailAddress($from);
## set the subject of the email
$message.Subject = $subject;
## set the body of the email
$message.Body = $body;
## set the body type
$message.IsBodyHtml = (-not $text);
## prepare the SMTP client information
$private:smtpClient = new-object Net.Mail.SmtpClient($smtp);
## ensure we use our AD credentials for authentication
$smtpClient.UseDefaultCredentials = $true;
## set the delivery method to network send
$smtpClient.DeliveryMethod = [Net.Mail.SmtpDeliveryMethod]::Network;
if($whatIf)
{
## just show us the settings
$message
$smtpClient
}
else
{
## send the message!
$smtpClient.Send($message);
}