Skip to content

Commit

Permalink
add-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmahoney committed May 6, 2024
1 parent 491331c commit f796dbf
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 104 deletions.
15 changes: 15 additions & 0 deletions powershell-imap-smtp/README.md
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
```
40 changes: 10 additions & 30 deletions powershell-imap-smtp/imap-mailkit.ps1
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env pwsh
# Now load MailKit
. .\_helpers.ps1
$scriptPath = Split-Path -Path $script:MyInvocation.MyCommand.Definition -Parent
Expand All @@ -6,7 +7,7 @@ LoadMailKit($scriptPath)

try
{
$imapClient = New-Object MailKit.Net.Imap.ImapClient
New-Object MailKit.Net.Imap.ImapClient
Write-Host "MailKit is loaded properly. ImapClient instance created successfully."
}
catch
Expand All @@ -32,28 +33,21 @@ Invoke-RestMethod -Method Get -Uri "https://api.mailslurp.com/inboxes/imap-smtp-

# Load environment variables from downloaded files
Load-EnvironmentVariables -filePath ".\.env.inbox"

Write-Host "Connect insecure"

#<gen>mailkit_connect_insecure
$client = New-Object MailKit.Net.Imap.ImapClient
$client.ServerCertificateValidationCallback = { $true }
$client.Connect($env:IMAP_SERVER_HOST, [int32]$env:IMAP_SERVER_PORT, [MailKit.Security.SecureSocketOptions]::None)
$client.Connect("mailslurp.click", 1143, [MailKit.Security.SecureSocketOptions]::None)
$client.Authenticate($env:IMAP_USERNAME, $env:IMAP_PASSWORD)
#</gen>
#Write-Host "Connect secure"
##<gen>mailkit_connect_secure
#$clientS = New-Object MailKit.Net.Imap.ImapClient
#$clientS.ServerCertificateValidationCallback = { $true }
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls11
#$clientS.Connect($env:SECURE_IMAP_SERVER_HOST, [int32]$env:SECURE_IMAP_SERVER_PORT, [MailKit.Security.SecureSocketOptions]::Auto)
#$clientS.Authenticate($env:SECURE_IMAP_USERNAME, $env:SECURE_IMAP_PASSWORD)
##</gen>

# Fetch and list all messages' headers
#<gen>mailkit_select
$inbox = $client.Inbox.Open([MailKit.FolderAccess]::ReadOnly)
$client.Inbox.Open([MailKit.FolderAccess]::ReadOnly)
Write-Host "Total messages: $( $client.Inbox.Count )"
#</gen>

#<gen>mailkit_fetch_messages
$fetchRequest = New-Object MailKit.FetchRequest
$messages = $client.Inbox.Fetch(0, -1, $fetchRequest)
Expand All @@ -66,24 +60,10 @@ foreach ($message in $messages)
#</gen>

# Search for unseen messages
function Search-Unseen($client)
{
$client.Inbox.Open([MailKit.FolderAccess]::ReadOnly)
$unseenIds = $client.Inbox.Search([MailKit.Search.SearchQuery]::NotSeen)
return $unseenIds
}

# Select INBOX and list unseen messages
#<gen>mailkit_select_and_list_unseen($client) {
#<gen>mailkit_search_search_unseen
$client.Inbox.Open([MailKit.FolderAccess]::ReadOnly)
$unseenMessages = Search-Unseen $client
Write-Host "Unseen Messages: $unseenMessages.Count"
$unseenIds = $client.Inbox.Search([MailKit.Search.SearchQuery]::NotSeen)
Write-Host "Unseen IDs: $unseenIds"
#</gen>

# Modify message flags
$client.Inbox.Open([MailKit.FolderAccess]::ReadWrite)
$client.Inbox.AddFlags(1, $client.Inbox.Count, $flag, "")


$client.Disconnect($true)
$clientS.Disconnect($true)
$client.Disconnect($true)
74 changes: 0 additions & 74 deletions powershell-imap-smtp/imap.ps1

This file was deleted.

53 changes: 53 additions & 0 deletions powershell-imap-smtp/smtp-mailkit.ps1
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>

0 comments on commit f796dbf

Please sign in to comment.