$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发送一个格式正确的MIME(符合RFC 2822标准)电子邮件,并对其进行URL安全的base64编码。这个示例应该可以工作,但问题似乎是Gmail实际上不接受这种格式的电子邮件发送。