我有一个由PowerShell生成的表,我以前能够将表作为html
发送到电子邮件正文中。但现在,我们已经迁移到gmail,gmail中的表格显示只是所有html
代码或csv代码。经过一番挖掘,看起来像gmail不会支持我想要做的事情。但是我尝试将表格直接复制到gmail中,并且因为没有格式更改而成功收到它。
作为替代方案,我可以将表附加为html或csv文件,但我仍然希望将其放在电子邮件正文中。
无论如何我仍然可以从powershell发送我的表到gmail吗?
$myTable = {PS scripts}
$EmailBody = $myTable | ConvertTo-Csv -NoTypeInformation
$EmailTo = @("me@gmail.com")
$EmailFrom = "notifications@gmail.com"
$Subject = "Notifications"
$Body = $EmailBody
$SMTPServer = "smtp.gmail.com"
# $filenameAndPath = "C:\temp\test.csv"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
# $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
# $SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("notification@gmail.com", "xxxxxx");
$SMTPClient.Send($SMTPMessage)
答案 0 :(得分:1)
MailMessage类正在为正文查找单个字符串。您可以在documentation here中了解该课程。您当前的$Body
变量是一个字符串数组。
public MailMessage(
string from,
string to,
string subject,
string body
)
将$Body
变量转换为单个字符串,方法是将其传递给Out-String
,例如评论中提到的@JosefZ:
$EmailBody = $myTable | ConvertTo-Csv -NoTypeInformation | Out-String
根据您的评论。它看起来像你想要一个HTML表格:
[String[]] $To = "me@gmail.com"
[String] $From = "notifications@gmail.com"
[String] $Subject = "Notifications"
[String] $SMTPServer = "smtp.gmail.com"
$Credential = Get-Credential -UserName "notification@gmail.com"
$myTable = {PS scripts}
$EmailBody = $myTable | ConvertTo-Html | Out-String
Send-MailMessage -Body $EmailBody -BodyAsHtml -To $To -From $From -Subject $Subject -SmtpServer $SMTPServer -Credential $Credential -UseSsl