How to add an X-Header to a message sent by the Send SMTP mail message activity?

HI
I am sending mail using the Send SMTP Mail Message activity. How can I add an additional X-header to it that will be seen by the mail server?

I don’t see any section in this activity where you can specify any custom X-header_name https://docs.uipath.com/activities/docs/send-mail

You may try in this case to achieve the needed results with a PowerShell/C#/Python script.

Example of PowerShell script:

$username="your_username@gmail.com" 
$password = "your_applciation_password" | ConvertTo-SecureString -AsPlainText -Force
$EmailTo = "your_username@uipath.com"
$EmailFrom = "your_username@gmail.com"
$Subject = "Email Subject" 
$Body = "Email body text" 
$SMTPServer = "smtp.gmail.com" 
$filenameAndPath = "C:\Marian\report.txt"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPMessage.Headers.Add("X-Company", "My Company")
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Management.Automation.PSCredential($username, $password)
$SMTPClient.Send($SMTPMessage)

Results (the custom header “X-Company”, “My Company” was added):

References:

https://shin.li/2021/04/28/send-mailmessage-powershell/
https://social.technet.microsoft.com/Forums/en-US/e94d7152-9cf6-480b-852f-95194e8f9e64/adding-custom-xheaders-to-emails-using-the-sendmailmessage-powershell?forum=winserverpowershell

Let us know if this worked for you.

hi
it works, but in its basic form. However, I would like to “hide” the password by providing it as a parameter previously taken from Orchestrator credentials, as secureString.
My script looks like this:
Param
(
[Parameter(Mandatory=$True)] [string]$AdresTo,
[Parameter(Mandatory=$True)] [string]$AdresFrom,
[Parameter(Mandatory=$True)] [string]$EmailPasswd
)

$username=$AdresFrom

$password = $EmailPasswd

$EmailTo = $AdresTo

$EmailFrom = $AdresFrom

$Subject = “<Email_Subject>”

$Body = “<Email_Body>”

$SMTPServer = “smtp1.xxxx.pl”

$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)

$SMTPMessage.Headers.Add(“x-greenmod-classification”, “PropertyRoot=MF;CATEGORY=InformacjePubliczneInformacjeSektoraPublicznego”)

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)

$SMTPClient.EnableSsl = $true

$SMTPClient.Credentials = New-Object System.Management.Automation.PSCredential($username, $password)

$SMTPClient.Send($SMTPMessage)

as parameters in invoke Power Shell I give e.g. EmailPAsswd variable as securestring.
but invoke generates an error with the message:

Invoke Power Shell: Cannot find an overload for “PSCredential” and the argument count: “2”.

In the meantime, I send the decoded string as the emailPasswd parameter, but I decode it in the parameters window.
I think that’s a safe enough way.
So @marian.platonov thank You very much for your help. :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.