Send-MailMessage를 사용하여 자신의 상용 메일을 이용하여 상대방에게 이메일을 보낼 수 있다. 특히 아래와 같이 작업하여 mymessage.ps1을 만들어 보내면 쉽게 보낼 수 있다.
또한 Windows Server를 사용하여 smtp Server를 자신이 만들어서 사용한다면 불편하게 암호를 입력하지 않고도 쉽게 보낼 수 있다. 이렇게 하는 장점은 사용자가 로그온/로그오프 할 때 자동으로 이메일을 보낼 수 있다는 것이다.
1) Hotmail을 이용하여 SSL로 메시지 보내기
Send-MailMessage -From jesuswithme@hotmail.com -SmtpServer smtp.live.com -UseSsl $true -Port 587 -Credential (Get-Credential jesuswithme@hotmail.com) -To jesuswithme@gmail.com -Subject “This is my message from Powershell” -Body “How is it going?” -Attachments C:\temp\project.txt
** 이렇게 하면 Popup 창이 나타나는데 jesuswithme@hotmail.com의 자신의 password를 입력해야 한다
** .ps1 스크립틀 만들 때 Param()을 이용하면 To, Subject, Attachment등을 다양하게 입력하면서 이메일을 보낼 수 있다.
2) 로컬 SMTP Server를 이용하여 메일 보내기
Send-MailMessage -From jesuswithme@hotmail.com -SmtpServer localhost -To jesuswithme@gmail.com -Subject “This is my message from Powershell” -Attachments C:\temp\project.txt
3) Param()을 이용하여 스크립트를 생성하여 메일 보내기
Param (
[Parameter(Mandatory=$True)]
$To,
[Parameter(Mandatory=$True)]
$Subject,
[Parameter(Mandatory=$True)]
$Body
)
Send-MailMessage -From jesuswithme@hotmail.com -SmtpServer localhost -To $To -Subject $Subject -Body $Body
** 이것을 SendMail.ps1 파일로 저장하여 실행하면 To:, Subject:, $Body: 가 Popup이 되면 적절하게 입력하여 메일을 보낼 수 있다.