-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
491331c
commit f796dbf
Showing
4 changed files
with
78 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Powershell IMAP and SMTP examples using MailKit/MimeKit | ||
Ensure you have the [`MailKit`](https://www.nuget.org/packages/MailKit/) and [`MimeKit`](https://www.nuget.org/packages/MimeKit/) libraries installed. You can install them using the `NuGet` package manager. | ||
|
||
Then ensure you have a MailSlurp API_KEY variable set. You can get one from the [MailSlurp dashboard](https://app.mailslurp.com). | ||
|
||
```ps1 | ||
$env:API_KEY = "your-api-key" | ||
``` | ||
|
||
Then run the examples: | ||
|
||
```ps1 | ||
./imap-mailkit.ps1 | ||
./smtp-mailkit.ps1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/env pwsh | ||
. .\_helpers.ps1 | ||
$scriptPath = Split-Path -Path $script:MyInvocation.MyCommand.Definition -Parent | ||
$apiKey = $env:API_KEY | ||
|
||
Load-EnvironmentVariables -filePath ".\.env.inbox" | ||
LoadMailKit($scriptPath) | ||
|
||
# assert $env:SMTP_USERNAME and $env:SMTP_PASSWORD are set | ||
if (-not $env:SMTP_USERNAME -or -not $env:SMTP_PASSWORD) { | ||
Write-Host "Please set SMTP_USERNAME and SMTP_PASSWORD environment variables." | ||
exit 1 | ||
} | ||
|
||
# Create a new SMTP inbox and parse JSON response | ||
$response = Invoke-RestMethod -Method Post -Uri "https://api.mailslurp.com/inboxes?inboxType=SMTP_INBOX" -Headers @{ "x-api-key" = $apiKey } | ||
$inboxId = $response.id | ||
$emailAddress = $response.emailAddress | ||
|
||
# Set up email parameters | ||
$emailAddress = $emailAddress | ||
$senderAddress = $emailAddress | ||
$recipientName = "Recipient" | ||
$senderName = "Sender name" | ||
$subject = "Test Email from PowerShell" | ||
$body = "This is a test email sent from PowerShell using MailKit/MimeKit." | ||
|
||
#<gen>pswh_mailkit_smtp_message | ||
# Create a new MimeMessage | ||
$message = New-Object MimeKit.MimeMessage | ||
|
||
# Add sender and recipient | ||
$message.From.Add([MimeKit.MailboxAddress]::new($senderName, $senderAddress)) | ||
$message.To.Add([MimeKit.MailboxAddress]::new($recipientName, $emailAddress)) | ||
|
||
# Set the subject and body | ||
$message.Subject = $subject | ||
$message.Body = [MimeKit.TextPart]::new("plain") | ||
$message.Body.Text = $body | ||
#</gen> | ||
|
||
#<gen>pswh_mailkit_smtp_send | ||
# Connect to the SMTP server | ||
$smtpClient = New-Object MailKit.Net.Smtp.SmtpClient | ||
$smtpClient.Connect("mxslurp.click", 2525) | ||
|
||
# If authentication is required, provide credentials | ||
$smtpClient.Authenticate([System.Text.Encoding]::UTF8, $env:SMTP_USERNAME, $env:SMTP_PASSWORD) | ||
|
||
# Send the message | ||
$smtpClient.Send($message) | ||
$smtpClient.Disconnect($true) | ||
#</gen> |