Link to home
Start Free TrialLog in
Avatar of cerhelp
cerhelp

asked on

powershell sending email alert automatically attaches winmail.dat

Greetings,

We are using powershell scripts to email out notifications for windows server 2012 backups. However when sending the message, it is attaching a file winmail.dat automatically, which is causing some email providers to reject the message thinking its spam / virus related. How can we stop this attachment from sending? Here is the script we are using:

# Set up variables
$SmtpServer = "SMTP SERVER ADDRESS"
$SmtpPort = 25
$FromEmail = "FROM EMAIL ADDRESS"
$ToEmail = "TO EMAIL ADDRESS"

# Construct and send email
$SmtpClient = new-object system.net.mail.smtpClient
$Msg = new-object Net.Mail.MailMessage
$SmtpClient.host = $SmtpServer
$SmtpClient.Port = $SmtpPort
$computer = gc env:computername
$Msg.From = $FromEmail
$Msg.To.Add($ToEmail)
$Msg.Subject = "Company Name Windows Server Backup Successful on " +$Computer + ""
$Msg.Body = "The backup for " +$Computer+" has completed successfully."
$SmtpClient.Send($Msg)
Avatar of Cliff Galiher
Cliff Galiher
Flag of United States of America image

Winmail.dat is how some multipart messages get processed by sending servers (most notably exchange) when the initial sender does not properly declare mime types or the formatting is invalid. My inclination  is that one of your variables is an object type that is causing problems,  but would need to analyze the working/running environment to say more. The Powershell ISE should make quick work of troubleshooting though.
Avatar of cerhelp
cerhelp

ASKER

I tagged the question incorrectly. This is server 2008. We added the feature to run ISE. However we have limited knowledge of powershell scripting. Is there something specific that we should be looking for? Or perhaps is there an alternate syntax / commands that could work better? Also is there a command line we can use to try to set a standard MIME type to test?
Try using Send-MailMessage and see if there any difference..
# Set up variables
$SmtpServer = "SMTP SERVER ADDRESS"
$FromEmail = "FROM EMAIL ADDRESS"
$ToEmail = "TO EMAIL ADDRESS"
$computer = gc env:computername
Send-MailMessage -From $FromEmail -To $ToEmail -Body "The backup for $Computer has completed successfully." -Subject "Company Name Windows Server Backup Successful on $Computer" -SmtpServer $SmtpServer

Open in new window

Avatar of cerhelp

ASKER

Same thing happens, still getting the winmail.dat attachment when coming into webmail providers.
What if you send the mails as HTML?
Send-MailMessage -From $FromEmail -To $ToEmail -BodyAsHtml "The backup for $Computer has completed successfully." -Subject "Company Name Windows Server Backup Successful on $Computer" -SmtpServer $SmtpServer

Open in new window

Avatar of cerhelp

ASKER

still exhibits the same behavior. thank you in advanced for your assistance
What if you try with -Encoding parameter? For example..
Send-MailMessage -From $FromEmail -To $ToEmail -Body "The backup for $Computer has completed successfully." -Subject "Company Name Windows Server Backup Successful on $Computer" -SmtpServer $SmtpServer -Encoding ASCII

Open in new window


For more details on -Encoding parameter, refer the following article..
http://ss64.com/ps/send-mailmessage.html
Avatar of cerhelp

ASKER

I have found a few articles that have mentioned something about TNEF encoding as a possible cause. is there anyway to disable TNEF encoding easily?
TNEF encoding should be disabled at the server side.. Which Exchange Server version are you using?
Avatar of cerhelp

ASKER

We are not running exchange. Just powershell commands for issuing email alerts for backup events. I had found some possible related articles I thought were relevant, but the TNEF I'm assuming is directly for Exchange?
I am taking about the SMTP server which you specify in the command, do you have information about it?

BTB, TNEF is a proprietary format used by Microsoft Outlook and Microsoft Exchange Server.
Avatar of cerhelp

ASKER

Oh my mistake. It is a standard ISP smtp server, through time warner. smtp-server.stny.rr.com. standard ports 25 ssl 587, no spa, no smtp authentication. Thats about all i have.
In that case you can to test by adding -Encoding parameter as i mentioned in ID: 40277164. If it doesn't work, then nothing much can be done from your end. You need to contact the service provider to fix the issue..
Avatar of cerhelp

ASKER

oh i somehow missed that. When trying that method it results in the following error:


Send-MailMessage : Cannot bind parameter 'Encoding'. Cannot convert the "ASCII" value of type "System.String" to type "System.Text.Enco
ding".
At D:\Tech\Scripts\successemail.ps1:6 char:212
+ Send-MailMessage -From $FromEmail -To $ToEmail -Body "The backup for $Computer has completed successfully." -Subject "Company Name Wi
ndows Server Backup Successful on $Computer" -SmtpServer $SmtpServer -Encoding <<<<  ASCII
    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.SendMailMessage
Try..
 -Encoding ([System.Text.Encoding]::ASCII)

Open in new window

Avatar of cerhelp

ASKER

I found an article which stated changing the encoding bit from

 "-Encoding ASCII"

 to

 "-Encoding ([System.Text.Encoding]::ASCII)"

would resolve the error. It is now sending successfully again, however the winmail.dat file remains. I guess it may actually just be an ISP issue, unless this response opens any other avenues to explore.
Avatar of cerhelp

ASKER

Just for informational purposes, also attempted UT8 encoding using

$enc  = New-Object System.Text.utf8encoding

and then swapping out adding $enc after -encoding. same results, sends but still has winmail.dat
You can also try other encoding to see if it eliminate your problem...

like..

[System.Text.Encoding]::Unicode

[System.Text.Encoding]::UTF8

Yes, it should be an ISP issue, and we tried all our options to find a workaround for the problem..
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cerhelp

ASKER

ok, thank you very much for your assistance, we will have to just set them up with a different email address. efforts with our isp will be fruitless.