header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Sending email using New-Object -comObject Outlook.Application?
Last Post 24 Dec 2010 04:51 AM by jbruns2010. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
get-jamesUser is Offline
New Member
New Member
Posts:59
Avatar

--
22 Jan 2009 01:09 PM
    Hi Guys,

    I am trying to send an plain text email using the com object Outlook.Application using Outlook 2003, which works, but I have having trouble trying to get the body of the message to format correctly?

    I have a plain text file with the following lines in it:

    PS C:\> Get-content "C:\Users\James\Desktop\body.txt"
    Line1

    Line2

    Line 3
    PS C:\>


    When I use the following code to display the email in outlook, the body of the message is all on one line like:


    <!--[if gte mso 9]> Normal 0 false false false EN-GB X-NONE X-NONE <!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:Consolas; panose-1:2 11 6 9 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:-1610611985 1073750091 0 0 159 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US;} p.MsoPlainText, li.MsoPlainText, div.MsoPlainText {mso-style-noshow:yes; mso-style-priority:99; mso-style-link:"Plain Text Char"; margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; font-family:Consolas; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US;} span.PlainTextChar {mso-style-name:"Plain Text Char"; mso-style-noshow:yes; mso-style-priority:99; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"Plain Text"; mso-ansi-font-size:10.5pt; mso-bidi-font-size:10.5pt; font-family:Consolas; mso-ascii-font-family:Consolas; mso-hansi-font-family:Consolas;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 72.0pt 72.0pt 72.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> <!--[if gte mso 10]> Line1  Line2  Line 3



    #############################################
    $ibody = Get-content "C:\Users\James\Desktop\body.txt"
    $ol = New-Object -comObject Outlook.Application
    $mail = $ol.CreateItem(0)
    $Mail.BodyFormat = 1
    [void]$Mail.Recipients.Add("Test@Internal.com")
    $Mail.Subject = "Test Message"
    $Mail.Body = $ibody
    $Mail.Display()
    #############################################

    Does anyone know how to do this, so I get the formatting right as it needs to be in plain text.

    Cheers
    James


    glnsizeUser is Offline
    Basic Member
    Basic Member
    Posts:193

    --
    22 Jan 2009 02:42 PM
    James,

    Your problem is with get-content.  When Get-Content ingest a file like that, it strips off all the line breaks.  Normally, the line breaks are added back in when any out-* or format-* cmdlet is invoked.  Anyways, two fixes that I know of are either a here-string, or add the breaks back in manualy.

    $ibody= @"
    Line1

    Line2

    Line 3
    "@

    or

    $ibody = Get-content "C:\Users\James\Desktop\body.txt"
    $body = [string]::join([environment]::NewLine,$ibody)

    Hope that helps,
    ~Glenn
    get-jamesUser is Offline
    New Member
    New Member
    Posts:59
    Avatar

    --
    22 Jan 2009 10:15 PM
    Thanks Glen,

    That works great, i will remember that for next time.

    Cheers
    James
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    22 Jan 2009 10:46 PM
    You can also try:

    $Mail.Body = [io.file]::readAllText("C:\Users\James\Desktop\body.txt")

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    jbruns2010User is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    24 Dec 2010 04:51 AM
    Anyone tried to add formatting to email text before it is sent?  I need to bold or perhaps change the font when email is created.  Are there special back tick characters for this or do I have to create an HTML stream and use that?
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer