Getting started with PowerShellLoopsOperatorsUsing ShouldProcessPowerShell ClassesSwitch statementWorking with ObjectsUsing existing static classesBasic Set OperationsPowerShell FunctionsSending EmailHandling Secrets and CredentialsPowershell RemotingPowerShell "Streams"; Debug, Verbose, Warning, Error, Output and InformationVariables in PowerShellCommunicating with RESTful APIsWorking with the PowerShell pipelinePowerShell Background JobsReturn behavior in PowerShellWorking with XML FilesIntroduction to PsakeUsing the progress barStringsTCP Communication with PowerShellSharePoint ModuleAliasesAutomatic VariablesEnvironment VariablesPowershell profilesEnforcing script prerequisitesUsing the Help SystemSplattingDesired State ConfigurationSigning ScriptsSecurity and CryptographyCSV parsingIntroduction to PesterModules, Scripts and FunctionsPowerShell.exe Command-LineCommon parametersParameter setsRegular ExpressionsPowerShell Dynamic ParametersWMI and CIMGUI in PowershellConditional logicURL Encode/DecodeMongoDBRunning ExecutablesError handlingHashTablesActiveDirectory modulepowershell sql queriesAutomatic Variables - part 2Package managementCmdlet NamingBuilt-in variablesCreating DSC Class-Based ResourcesPowershell ModulesPowerShell WorkflowsHow to download latest artifact from Artifactory using Powershell script (v2.0 or below)?Calculated PropertiesSpecial OperatorsAnonymize IP (v4 and v6) in text file with PowershellComment-based helpAmazon Web Services (AWS) Simple Storage Service (S3)Amazon Web Services (AWS) RekognitionPSScriptAnalyzer - PowerShell Script AnalyzerNaming ConventionsEmbedding Managed Code (C# | VB)Archive ModuleInfrastructure AutomationScheduled tasks moduleISE module

Sending Email

Other topics

Simple Send-MailMessage

Send-MailMessage -From [email protected] -Subject "Email Subject" -To [email protected] -SmtpServer smtp.com

Send-MailMessage with predefined parameters

$parameters = @{
    From = '[email protected]'
    To = '[email protected]'
    Subject = 'Email Subject'
    Attachments =  @('C:\files\samplefile1.txt','C:\files\samplefile2.txt')
    BCC = '[email protected]'
    Body = 'Email body'
    BodyAsHTML = $False
    CC = '[email protected]'
    Credential = Get-Credential
    DeliveryNotificationOption = 'onSuccess'
    Encoding = 'UTF8'
    Port = '25'
    Priority = 'High'
    SmtpServer = 'smtp.com'
    UseSSL = $True
}

# Notice: Splatting requires @ instead of $ in front of variable name
Send-MailMessage @parameters

SMTPClient - Mail with .txt file in body message

# Define the txt which will be in the email body
$Txt_File = "c:\file.txt"

function Send_mail {
    #Define Email settings
    $EmailFrom = "[email protected]"
    $EmailTo = "[email protected]"
    $Txt_Body = Get-Content $Txt_File -RAW
    $Body = $Body_Custom + $Txt_Body
    $Subject = "Email Subject"
    $SMTPServer = "smtpserver.domain.com"
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 
    $SMTPClient.EnableSsl = $false
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

}

$Body_Custom = "This is what contain file.txt : "

Send_mail

Parameters:

ParameterDetails
Attachments<String[]>Path and file names of files to be attached to the message. Paths and filenames can be piped to Send-MailMessage.
Bcc<String[]>Email addresses that receive a copy of an email message but does not appear as a recipient in the message. Enter names (optional) and the email address (required), such as Name [email protected] or [email protected].
Body <String_>Content of the email message.
BodyAsHtmlIt indicates that the content is in HTML format.
Cc<String[]>Email addresses that receive a copy of an email message. Enter names (optional) and the email address (required), such as Name [email protected] or [email protected].
CredentialSpecifies a user account that has permission to send message from specified email address. The default is the current user. Enter name such as User or Domain\User, or enter a PSCredential object.
DeliveryNotificationOptionSpecifies the delivery notification options for the email message. Multiple values can be specified. Delivery notifications are sent in message to address specified in To parameter. Acceptable values: None, OnSuccess, OnFailure, Delay, Never.
EncodingEncoding for the body and subject. Acceptable values: ASCII, UTF8, UTF7, UTF32, Unicode, BigEndianUnicode, Default, OEM.
FromEmail addresses from which the mail is sent. Enter names (optional) and the email address (require), such as Name [email protected] or [email protected].
PortAlternate port on the SMTP server. The default value is 25. Available from Windows PowerShell 3.0.
PriorityPriority of the email message. Acceptable values: Normal, High, Low.
SmtpServerName of the SMTP server that sends the email message. Default value is the value of the $PSEmailServer variable.
SubjectSubject of the email message.
ToEmail addresses to which the mail is sent. Enter names (optional) and the email address (required), such as Name [email protected] or [email protected]
UseSslUses the Secure Sockets Layer (SSL) protocol to establish a connection to the remote computer to send mail

Contributors

Topic Id: 2040

Example Ids: 6666,6667,28449

This site is not affiliated with any of the contributors.