0

I have this code to send email with attachment:-

$encpassword = convertto-securestring -String "*****" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "***@***", $encpassword
Connect-PnPOnline -Url $sourceWebURL -Credentials $cred
Send-MailMessage -to "" -from "" -Credentials $cred -bcc "" -Port "587" -UseSSL "true" -Subject "subject" -Body "<b>1</b><br><b>2</b>" -BodyAsHtml -SmtpServer "smtp.office365.com"  -Attachments "C:\s.csv"

but based on the documentation's warning that Send-MailMessage is obsolete @ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7.3

and at the same time using Send-PnPMail does not support sending attachments @ https://pnp.github.io/powershell/cmdlets/Send-PnPMail.html .

so my question is how we can send an email using power shell using a supported/recommended approach which allow us to attach a file.

Thanks

3
  • Have you considered sending a link to a file rather than an attachment? There are a number of benefits of using a link to a file stored in SharePoint, rather than attaching a file to each email. Commented Jan 31, 2023 at 14:42
  • @CallumCrowley the requirement is to keep the functionality as-is,, so the customer need to get an attachment rather than link to a document on sharepoint/one-drive
    – John John
    Commented Jan 31, 2023 at 16:13
  • is there a solution you have got to this problem because I am having a similar one? It would be of great help to me.
    – user108849
    Commented Jul 10, 2023 at 8:49

1 Answer 1

-1

We can send the files as attachment, while sending the mail from PowerShell. This is done by adding an extra parameter -Attachment’ to the Send-MailMessage command. In addition to the mail body, we can specify the file location of the attachment file, which has to be sent along with the mail. In the code block given below, we are using Gmail relay Server to relay the mail, which contains the file located at the local machine. This attachment location is specified directly in the ‘Send-MailMessage’ command, using the ‘Attachment’ parameter.

Attachment Sending code is given below.

$Body = "Sample Email Body"
$SmtpServer = 'smtp.gmail.com'
$SmtpUser = '[email protected]'
$smtpPassword = '<Input Gmail Account Password Here>'
$MailtTo = '[email protected]'
$MailFrom = '[email protected]'
$MailSubject = "Testing Mail Attachments using $SmtpServer"
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword |
ConvertTo-SecureString -AsPlainText -Force)
Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -Body "$Body" -Attachments "C:\PS\SampleAttachment.txt" -SmtpServer $SmtpServer -BodyAsHtml -UseSsl -Credential $Credentials
write-Output "Custom Message : Attachment Email Sent to Business Users"

For more information, please refer to: https://social.technet.microsoft.com/wiki/contents/articles/37782.powershell-send-mails-with-attachments-using-office-365-and-gmail-smtp-server.aspx

1
  • seems you did not read my question, i already know about Send-MailMessage but it is obsolete
    – John John
    Commented Feb 1, 2023 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.