Reputation: 3162
I have a table generated by powershell, I used to be able to send the table to exchange as a html
in email body. but now, we have migrated to gmail and the table shows in gmail is just all html
code or csv code. After some digging, looks like gmail wont support what I am trying to do. But I tried to copy the table into gmail directly and i received it successfully as no format changes.
As an alternative, I can have the table attached as html or csv files but i still want to have it in email body.
Is there anyway that I can still send my table to gmail from powershell?
$myTable = {PS scripts}
$EmailBody = $myTable | ConvertTo-Csv -NoTypeInformation
$EmailTo = @("[email protected]")
$EmailFrom = "[email protected]"
$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("[email protected]", "xxxxxx");
$SMTPClient.Send($SMTPMessage)
Upvotes: 0
Views: 1059
Reputation: 2342
The class MailMessage is looking for a single string for the body. You can read about the class in the documentation here. Your current $Body
variable is an array of strings.
public MailMessage(
string from,
string to,
string subject,
string body
)
Convert your $Body
variable to a single string by passing it to Out-String
like @JosefZ mentioned in your comment:
$EmailBody = $myTable | ConvertTo-Csv -NoTypeInformation | Out-String
Based on your comment. It's looking like you wanted an HTML table:
[String[]] $To = "[email protected]"
[String] $From = "[email protected]"
[String] $Subject = "Notifications"
[String] $SMTPServer = "smtp.gmail.com"
$Credential = Get-Credential -UserName "[email protected]"
$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
Upvotes: 1