Thursday, May 24, 2007

SMTP using .NET Classes (Powershell)

A great Powershell code that you can use as a template for SMTP mail with authentication.

Easily extendible for Web development.


Scriptlet:
###############################################
$mail = new-object System.Net.Mail.MailMessage

#set sender email address
$mail.From = new-object System.Net.Mail.MailAddress("mymail@mydomain.com");

#set the recepient email address
$mail.To.Add("touser@domain.com");

#set the email subject
$mail.Subject = "";

#set the content

$mail.Body = "";

#send the message
$smtp = new-object System.Net.Mail.SmtpClient("mydomain.com");

#set the username and password properites on the SmtpClient for authentication
$smtp.Credentials = new-object System.Net.NetworkCredential("username", "password");

#send it
$smtp.Send(mail);


#Voila!
###############################################


No comments: