script to send email to user with their multiple email addresses RRS feed

  • Question

  • Hi 

    i got the script below which mainly email users about their current SMTP addresses (multiple ones) and ask them which one they dont need

    $Msg.Subject = "Email Addresses Clean up"
    $Msg.Body = $EmailBody
    $Msg.IsBodyHTML = $true
    # Define local Exchange server info for message relay. Ensure that any servers running this script have permission to relay.
    $ExchangeServer = ""
    $FromAddress = ""
    # Import user list and information from .CSV file
    $Users = Import-Excel testfinalsmtpaddress.xlsx
    # Send notification to each user in the list
    Foreach ($User in $Users) {
    $ToAddress = $User.PrimarySmtpAddress
    $Name = $User.DisplayName
    $EmailAddresses = $User.EmailAddresses
    $EmailBody = @"
    <p style='font-size:10.0pt;font-family:"Arial",sans-serif'>Dear $Name.<o:p></o:p></span></p>
    <p style='margin-left:18.0pt;text-indent:-18.0pt;
    <![if !supportLists]><span style='font-size:10.0pt;
    style='·<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </span></span></span><![endif]><span style='font-size:10.0pt;font-family:"Arial",sans-serif'>The
    following is a list of email addresses which are currently attached to your


    Email Addresses: $EmailAddresses

    The list of users with multiple smtp addresses have been generated and it is in excel format with each email have been displayed follow line break like this

    Problem is when I send an email to them using this function to display their email addresses Email Addresses: $EmailAddresses, all their smtp address does not display one by one with line break, it seem to combine them into one long line which make difficult for user to read. The excel spreadsheet which contains smtp addresses already got correct format 

    I just wonder if anyone know how can I overcome that?

    Name                                                                                                   Primary Email                                                EmailAddresses                                                                                                  

    Hung Nguyen

    Tuesday, July 16, 2019 10:53 PM

All replies

  • Please fix you post. Code must be posted using the code posting tool provided. The example posted is not helpful. What you are asking is not understandable.

    We do not fix or customize code that you have found on the Internet.

    How to ask questions in a technical forum


    Tuesday, July 16, 2019 11:00 PM
  • Your code is broken and incomplete making it even harder to understand what you are trying to do. You reference a function but there is no function.

    The email addresses are an array.

    The HTML will  not work correctly in an email body.


    Tuesday, July 16, 2019 11:07 PM
  • # Import user list and information from .CSV file
    $Users = Import-Excel testfinalsmtpaddress.xlsx

    Please fix the code formatting  in order to help you. Looking at the code.. are you trying to import a csv ?

    Wednesday, July 17, 2019 7:34 PM
  • Try using the HTML style </br> between addresses. 
    • Proposed as answer by Gavin..S Thursday, July 18, 2019 6:02 PM
    Thursday, July 18, 2019 6:01 PM
  • $mailProps = @{
        From        = ''
        Subject     = 'Email Addresses Clean up'
        SmtpServer  = ''
        Port        = 25
        BodyAsHtml = $true
    $tmplt = @'
        <p style='font-size:10.0pt;font-family:Arial'>
            Dear {0}.
        <p style='margin-left:18.0pt;text-indent:-18.0pt;'
            The following is a list of email addresses which are currently attached to your mailbox.
    $users = Import-Excel testfinalsmtpaddress.xlsx
    Foreach ($user in $users) {
        $body  = $tmplt -f $user.DisplayName, ($user.EmailAddresses -join '<br/>')
        Send-MailMessage $mailProps -To $user.PrimarySmtpAddress -Body $body


    Friday, July 19, 2019 9:09 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Just do it.

    Wednesday, July 31, 2019 7:44 AM