gpt4 book ai didi

powershell - 如何使用 PowerShell 发送电子邮件

转载 作者:行者123 更新时间:2023-12-03 21:11:08 25 4
gpt4 key购买 nike

我想从 PowerShell 发送电子邮件,
所以我使用这个命令:

$EmailFrom = "customer@yahoo.com"
$EmailTo = "receiver@ymail.com"
$Subject = "today date"
$Body = "TODAY SYSTEM DATE=01/04/2016 SYSTEM TIME=11:32:05.50"
$SMTPServer = "smtp.mail.yahoo.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object
System.Net.NetworkCredential("customer@yahoo.com", "password")
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

此命令不适用于 Yahoo 邮件或 Outlook 邮件,但适用于我的 Gmail。
我做错了什么吗?

最佳答案

以下代码片段对我来说确实有效:

$Username = "MyUserName";
$Password = "MyPassword";
$path = "C:\attachment.txt";

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

$message = new-object Net.Mail.MailMessage;
$message.From = "YourName@gmail.com";
$message.To.Add($email);
$message.Subject = "subject text here...";
$message.Body = "body text here...";
$attachment = New-Object Net.Mail.Attachment($attachmentpath);
$message.Attachments.Add($attachment);

$smtp = new-object Net.Mail.SmtpClient("smtp.gmail.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 "reciever@gmail.com" -attachmentpath $path;

关于powershell - 如何使用 PowerShell 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36355271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com