Sending mail with PowerShell – Exchange Online

$Username = “[email protected]”;
$Password = “password”;
$path = “C:\test\attachment.txt”;

function Send-ToEmail([string]$email, [string]$attachmentpath){

$message = new-object Net.Mail.MailMessage;
$message.From = "[email protected]";
$message.To.Add($email);
$message.Subject = "Powershell test.";
$message.Body = "Powershell test.";
$attachment = New-Object Net.Mail.Attachment($attachmentpath);
$message.Attachments.Add($attachment);

$smtp = new-object Net.Mail.SmtpClient("smtp.office365.com", "587");
$smtp.EnableSSL = $true;
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message);
write-host "Mail Sent" ; 
$attachment.Dispose();

}
Send-ToEmail -email “[email protected]” -attachmentpath $path;

Yazar hakkında

serdanaydin