gpt4 book ai didi

powershell - 使用 Gmail API 在 Powershell 中使用 Invoke-WebRequest 发送电子邮件

转载 作者:行者123 更新时间:2023-12-04 08:21:53 32 4
gpt4 key购买 nike

$firstEmail = "joe@gmail.com";
$secondEmail = "notjoe@gmail.com";

Function Set-MIMEBase64Encoded
{
Param(
[string]$subject
)

#Creates a MIME formatted email.
$text = "From: $firstEmail\r\n" + "To: $secondEmail\r\n" + "Subject: $subject\r\n\r\n" + "$subject";
$bytes = [System.Text.Encoding]::Unicode.GetBytes($text);
#Converts to Base 64.
$encodedText =[Convert]::ToBase64String($bytes);

#Makes encoding URL safe.
$urlSafe1 = $encodedText.replace('+', '-');
$urlSafe2 = $urlSafe1.replace('/', '_');
$urlSafe3 = $urlSafe2.replace('=', '*');

return $urlSafe3;
}

Function Mail-Output
{
Param(
[String]$subject
)

#Acquires access token.
$accessToken = Refresh-AccessToken;
#Sends subject for MIMEB64 encoding
$text = Set-MIMEBase64Encoded -subject $subject;

#Requests sends email according to parameters.
$messages = Invoke-WebRequest -Uri ("https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token=$accessToken&raw=$text") -Method Post;
Write-Output $messages
}

Mail-Output -subject "Hope this works!"

因此,我在这里尝试做的是通过 powershell 中的 Invoke-WebRequest 发送以 URL 安全 base64 编码的格式正确的 MIME(符合 RFC 2822)电子邮件。此示例应该可以工作,但问题似乎是 Gmail 实际上并不接受以这种格式发送的电子邮件。

最佳答案

任何不使用的理由 Send-MailMessage ?.如果没有,你可以试试这个例子:

$From = "YourEmail@gmail.com"
$To = "AnotherEmail@YourDomain.com"
$Cc = "YourBoss@YourDomain.com"
$Attachment = "C:\temp\Some random file.txt"
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (Get-Credential) -Attachments $Attachment

关于powershell - 使用 Gmail API 在 Powershell 中使用 Invoke-WebRequest 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41917091/

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