ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Mail Allignment Issue

    IT Discussion
    4
    13
    1.7k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • RoopanKumarR
      RoopanKumar
      last edited by

      ena boss automation panuringala work panam ???

      1 Reply Last reply Reply Quote 0
      • LakshmanaL
        Lakshmana
        last edited by scottalanmiller

        Help Needed to paste the excel sheet inside email body

        $filename = 'file.xlsx'
        $From ="[email protected]"
        $to = "[email protected]"
        $SMTP= "[email protected]"
        $filepath = "C:\Location"
        
        
        $excel = New-Object -comobject Excel.Application
        $ReportWorkBook = $excel.Workbooks.Open($filePath)
        $excel.Visible = $false
        $ws = $ReportWorkBook.Worksheets.Item(1)
        $SelectedRange = $ws.UsedRange
        $SelectedRange.Copy() 
        $newmail.GetInspector.WordEditor.Range().Paste()
        
        Function sendEmail([string]$emailFrom, [string]$emailTo, [string]$subject,[string]$body,[string]$smtpServer,[string]$filepath)
        {
        #initate message
        $email = New-Object System.Net.Mail.MailMessage 
        $email.From = $emailFrom
        $email.To.Add($emailTo)
        $email.Subject = $subject
        $email.Body = $body
        #initiate email attachment
        
        
        $emailAttach = New-Object System.Net.Mail.Attachment $newmail.GetInspector.WordEditor.Range().Paste()
        $email.Attachments.Add($emailAttach)
        
        
        #initiate sending email 
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        $smtp.Send($email)
        }
        Send-MailMessage -From $From -To $to -Subject "Test Report -$(Get-Date)" -BodyAsHtml "Report" -SmtpServer $SMTP
        
        1 Reply Last reply Reply Quote 0
        • dafyreD
          dafyre
          last edited by

          $SMTP= "[email protected]"

          First off, $smtp should be the address of your company's SMTP server... It would be like "mail.domain.com" or something like that -- not an email address.

          $filepath ="C:\Location"

          needs to be the FULL file path, not just a folder name.

          The way this script is laid out, you really don't need the sendEmail function at all. See how it works like this:

          (i've only checked this for basic syntax)

          $filename = 'file.xlsx'
          $From ="[email protected]"
          $to = "[email protected]"
          $SMTP= "mail.domain.com"
          $filepath = "C:\Location\$filename"
          
          
          $excel = New-Object -comobject Excel.Application
          $ReportWorkBook = $excel.Workbooks.Open($filePath)
          $excel.Visible = $false
          $ws = $ReportWorkBook.Worksheets.Item(1)
          $SelectedRange = $ws.UsedRange
          $SelectedRange.Copy() 
          #$newmail.GetInspector.WordEditor.Range().Paste()
          
          $body = @"
          
          My email content goes here
          
          "@
          
          
          #initate message
          $email = New-Object System.Net.Mail.MailMessage 
          $email.From = $emailFrom
          $email.To.Add($emailTo)
          $email.Subject = $subject
          $email.Body = $body
          
          #initiate email attachment
          $emailAttach = $SelectedRange.Paste()
          $email.Attachments.Add($emailAttach)
          
          
          #initiate sending email 
          $smtp = new-object Net.Mail.SmtpClient($smtp)
          $smtp.Send($email)
          
          LakshmanaL 1 Reply Last reply Reply Quote 0
          • LakshmanaL
            Lakshmana @dafyre
            last edited by scottalanmiller

            @dafyre said in Mail Allignment Issue:

            $filename = 'file.xlsx'
            $From ="[email protected]"
            $to = "[email protected]"
            $SMTP= "mail.domain.com"
            $filepath = "C:\Location$filename"

            $excel = New-Object -comobject Excel.Application
            $ReportWorkBook = $excel.Workbooks.Open($filePath)
            $excel.Visible = $false
            $ws = $ReportWorkBook.Worksheets.Item(1)
            $SelectedRange = $ws.UsedRange
            $SelectedRange.Copy()
            #$newmail.GetInspector.WordEditor.Range().Paste()

            $body = @"

            My email content goes here

            "@

            #initate message
            $email = New-Object System.Net.Mail.MailMessage
            $email.From = $emailFrom
            $email.To.Add($emailTo)
            $email.Subject = $subject
            $email.Body = $body

            #initiate email attachment
            $emailAttach = $SelectedRange.Paste()
            $email.Attachments.Add($emailAttach)

            #initiate sending email
            $smtp = new-object Net.Mail.SmtpClient($smtp)
            $smtp.Send($email)

            Exception calling "Add" with "1" argument(s): "Value cannot be null.
            Parameter name: item"
            At C:\Reports\excel.ps1:26 char:1
            + $email.To.Add($emailTo)
            + ~~~~~~~~~~~~~~~~~~~~~~~
                + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
                + FullyQualifiedErrorId : ArgumentNullException
             
            Method invocation failed because [System.__ComObject] does not contain a method named 'Paste'.
            At C:\Reports\excel.ps1:31 char:1
            + $emailAttach = $SelectedRange.Paste()
            + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                + CategoryInfo          : InvalidOperation: (Paste:String) [], RuntimeException
                + FullyQualifiedErrorId : MethodNotFound
             
            Exception calling "Add" with "1" argument(s): "Value cannot be null.
            Parameter name: item"
            At C:\Reports\excel.ps1:32 char:1
            + $email.Attachments.Add($emailAttach)
            + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
                + FullyQualifiedErrorId : ArgumentNullException
             
            Exception calling "Send" with "1" argument(s): "A recipient must be specified."
            At C:\Reports\excel.ps1:37 char:1
            + $smtp.Send($email)
            + ~~~~~~~~~~~~~~~~~~
                + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
                + FullyQualifiedErrorId : InvalidOperationException
            
            1 Reply Last reply Reply Quote 1
            • dafyreD
              dafyre
              last edited by

              Check through the code, I see a couple of issues I didn't catch the first time.

              At the top, change $to="[email protected]" to be $emailTo="[email protected]

              I'm not sure about the $SelectedRange.paste() error. You'll have to dig into that one. You had that bit working in one of your earlier scripts.

              Fixing the $emailTo at the top should also fix the "Send" last error as well.

              1 Reply Last reply Reply Quote 0
              • LakshmanaL
                Lakshmana
                last edited by

                @stess can you help me on this tooo??

                S 1 Reply Last reply Reply Quote 0
                • S
                  stess @Lakshmana
                  last edited by

                  @lakshmana
                  Is the file XLSX or CSV? If it is CSV then the example I provided in an other thread works just fine. XLSX is a lot more trickier as you have to target the cells and what not.

                  1 Reply Last reply Reply Quote 0
                  • LakshmanaL
                    Lakshmana
                    last edited by

                    here the file is xlsx where i need to copy sheet1 only from excel to body of the mail!!

                    S 1 Reply Last reply Reply Quote 0
                    • S
                      stess @Lakshmana
                      last edited by

                      @lakshmana
                      I'll say I'm not good with excel in powershell. I always avoid dealing with excel just because there are too many area that could goes wrong in the long term.

                      1 Reply Last reply Reply Quote 0
                      • LakshmanaL
                        Lakshmana
                        last edited by

                        whether can i convert. xlsx to. csv andcopy data to body of mail??? ![alt text](image url)

                        1 Reply Last reply Reply Quote 0
                        • LakshmanaL
                          Lakshmana
                          last edited by scottalanmiller

                          How to add Hyperlink to the Introduction where <a href="">is not working here

                          Help needed !!!

                          $xl=New-Object -ComObject Excel.Application
                          $wb=$xl.Workbooks.Open('c:\scripts\process.xlsx')
                          $ws=$wb.Sheets.Item(1)
                          $ws.MailEnvelope.Introduction = "This is a sample worksheet."
                          $ws.MailEnvelope.Item.To = "[email protected]"
                          $ws.MailEnvelope.Item.Subject = "My subject"
                          $ws.MailEnvelope.Item.Send()
                          
                          1 Reply Last reply Reply Quote 0
                          • LakshmanaL
                            Lakshmana
                            last edited by Lakshmana

                            @stess
                            @scottalanmiller
                            I got one script for copying the excel sheet content to the mail body which have issue in it.The issue where the table is not copying to body only contents are copied to the mail body without table.Help needed

                            $email = "[email protected]"
                            $smtpServer = "mail.somecompany.com"
                            $ForEmail = @()
                            ######################################
                            #Create and get my Excel Obj
                            $x1 = New-Object -comobject Excel.Application
                            $UserWorkBook = $x1.Workbooks.Open("C:\temp\bluenose.xlsx")
                             
                            #Select first Sheet
                            $UserWorksheet = $UserWorkBook.Worksheets.Item(1)
                            $UserWorksheet.activate()
                             
                            #Copy the part of the sheet I want in the Email
                            $rgeSource=$UserWorksheet.range("A1","E20")
                            $rgeSource.Copy() | out-null
                             
                            $Results = Get-Clipboard
                            foreach ($Result in $Results)
                            {
                                $ForEmail += "$Result<br>"
                            }
                             
                            ######################################
                             
                            $msg = new-object Net.Mail.MailMessage
                            $smtp = new-object Net.Mail.SmtpClient($smtpServer)
                             
                            $msg.From = "[email protected]"
                            $msg.To.Add($email)
                            $msg.Subject = "Excel Pasted"
                            $msg.IsBodyHtml = $True
                             
                            $msg.Body = "Here is the contents of the excel file<br>
                            <br>
                            $ForEmail
                            <br>
                            "
                            $smtp.Send($msg)](![image url](image url))
                            
                            1 Reply Last reply Reply Quote 0
                            • 1 / 1
                            • First post
                              Last post