开发者

Email Subject showing Encoding

开发者 https://www.devze.com 2023-04-06 06:28 出处:网络
I am using send-mail message to send email to our Support System. But when it send email it shows the subject line like this screen!

I am using send-mail message to send email to our Support System. But when it send email it shows the subject line like this screen!

=?us-ascii?Q?R899076:Aman:System Summary ?=

In Subject I am using the variable:

$vUserName = (Get-Item env:\username).Value
$vComputerName = (Get-Item env:\Computername).Value
$subject = "$vComputerName : $vUserName : System Summary"

and then

send-MailMessage  -SmtpServer Smtp-local -To $to -From $from -Subject $subject -Body $body  -BodyAsHtml -Priority High 

But when I recieve t开发者_如何学Gohis email in Outlook it looks fine any Idea?


Actually this a approx 150 lines script and the body of email and smtp server are already specified in the server. yes I tried the $subject = "$env:ComputerName : $env:UserName : System Summary" variable and the result is same.

yes I have tried the - encoding option and it gives an error

Send-MailMessage : Cannot bind parameter 'Encoding'. Cannot convert the "utf8" value of type "Syste m.String" to type "System.Text.Encoding". At D:\PowerShell\MyScripts\SystemInfo\SysInfo-V6-test[notfinal].ps1:151 char:84 + send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Encoding <<<< utf8 -B ody $body -Attachments "$filepath\$name.html" -BodyAsHtml -Priority High + CategoryInfo : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.SendMai lMessage

Any clue?


You could write a custom function to send using .net instead of using the Send-MailMessage cmdlet. It's a lot clunkier, but gets round the problem.

Something like this:

Function SendEmail  ($emailFrom, $emailTo, $subject, $body, $attachment) {

# Create from/to addresses  
$from = New-Object System.Net.Mail.MailAddress $emailFrom
$to =   New-Object System.Net.Mail.MailAddress $emailTo

# Create Message  
$message = new-object System.Net.Mail.MailMessage $from, $to  
$message.Subject = $subject  
$message.Body = $body

$attachment = new-object System.Net.Mail.Attachment($attachment)
$message.Attachments.Add($attachment)


# Set SMTP Server and create SMTP Client  
$server = <your server> 
$client = new-object system.net.mail.smtpclient $server  

# Send the message  
"Sending an e-mail message to {0} by using SMTP host {1} port {2}." -f $to.ToString(), $client.Host, $client.Port  
try {  
   $client.Send($message)  
   "Message to: {1}, from: {0} has beens successfully sent" -f $from, $to  
}  
catch {  
  "Exception caught in CreateTestMessage: {0}" -f $Error.ToString()  
} 

}

(Thanks to Thomas Lee (tfl@psp.co.uk) - I tweaked this from his code at http://powershell.com/cs/media/p/357.aspx)


The Send-MailMessage cmdlet doesn't have any output after you send emails, I wonder where the output comes from? Can you include the command you use and the output?

As to your subject line, you can reduce it to one line only:

$subject = "$env:ComputerName : $env:UserName : System Summary"

The Send-MailMessage has an Encoding parameter, have you tried it?


This happened to me when I used Send-MailMessage with the default encoding (ASCII) when there were spaces in the subject.

First, to answer the question about the Encoding parameter: You are trying to pass it as a string (ie "-Encoding ASCII"), when you should be using this type of syntax instead: "-Encoding ([System.Text.Encoding]::ASCII)".

This "encoding in the subject" issue happened to me, and I narrowed it down to spaces in the subject. To demonstrate:

This will not contain encoding in the subject:

Send-MailMessage -To "alice@example.com" -From "bob@example.com" -Smtp "localhost" -Subject "one" -BodyAsHtml "body1" -Encoding ([System.Text.Encoding]::ASCII)

But this will:

Send-MailMessage -To "alice@example.com" -From "bob@example.com" -Smtp "localhost" -Subject "one two" -BodyAsHtml "body1" -Encoding ([System.Text.Encoding]::ASCII)

Note that the only material difference is the space in the subject.

If I specify UTF8 as the encoding, there is no encoding in the subject:

Send-MailMessage -To "alice@example.com" -From "bob@example.com" -Smtp "localhost" -Subject "one" -BodyAsHtml "body1" -Encoding ([System.Text.Encoding]::UTF8)

But as you say, it looks fine in Outlook, so I presume that the subject is correct. I'm not an expert on email formats or text-encoding, so I won't speculate as to why.

0

精彩评论

暂无评论...
验证码 换一张
取 消