Skip to content

HTTPS clone URL

Subversion checkout URL

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