Skip to content

Commit 9e5ad54

Browse files
committed
dotnet function to send email
Signed-off-by: Deepak Devadathan <[email protected]>
1 parent 0f5a53a commit 9e5ad54

12 files changed

+263
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This repository provides examples demonstrating how to use Oracle Functions.
2929
| Run a SQL statement against Autonomous DB using ORDS | [sample](./samples/oci-adb-ords-runsql-python) ||[sample](./samples/oci-adb-ords-runsql-dotnet)|
3030
| Run a SQL statement against Autonomous DB using DB Client |[sample](./samples/oci-adb-client-runsql-python)|| [sample](./samples/oci-adb-client-runsql-dotnet)|
3131
| Publish a notification using ONS |[sample](./samples/oci-ons-publish-python)||[sample](./samples/oci-ons-publish-dotnet)|
32-
| Send an email using Email Delivery Service |[sample](./samples/oci-email-send-python)|
32+
| Send an email using Email Delivery Service |[sample](./samples/oci-email-send-python)||[sample](./samples/oci-email-send-python)|
3333
| Decrypt cipher using Vault keys |[sample](./samples/oci-vault-decrypt-python)||[sample](./samples/oci-vault-decrypt-dotnet)|
3434
| Get a secret from Vault |[sample](./samples/oci-vault-get-secret-python)||[sample](./samples/oci-vault-get-secret-dotnet)|
3535
| Write IAM policies that enables Functions in a tenancy to access resources in other tenancies ||[sample](./samples/oci-cross-tenancy-policies-java)
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM fnproject/dotnet:3.1-1.0.4-dev as build-stage
2+
WORKDIR /function
3+
COPY . .
4+
RUN dotnet sln add SendEmail.csproj
5+
RUN dotnet build SendEmail.csproj -c Release
6+
RUN dotnet publish SendEmail.csproj -c Release -o out
7+
FROM fnproject/dotnet:3.1-1.0.4
8+
WORKDIR /function
9+
COPY --from=build-stage /function/out/ /function/
10+
ENTRYPOINT ["dotnet", "SendEmail.dll"]
11+
CMD ["SendEmail:Function:function_handler"]
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio 15
3+
VisualStudioVersion = 15.0.26124.0
4+
MinimumVisualStudioVersion = 15.0.26124.0
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Debug|x64 = Debug|x64
9+
Debug|x86 = Debug|x86
10+
Release|Any CPU = Release|Any CPU
11+
Release|x64 = Release|x64
12+
Release|x86 = Release|x86
13+
EndGlobalSection
14+
GlobalSection(SolutionProperties) = preSolution
15+
HideSolutionNode = FALSE
16+
EndGlobalSection
17+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace SendEmail
4+
{
5+
6+
class InputMessage
7+
{
8+
public string sender_email { get; set; }
9+
public string sender_name { get; set; }
10+
public string recipient { get; set; }
11+
public string subject { get; set; }
12+
public string body { get; set; }
13+
14+
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace SendEmail
4+
{
5+
6+
class Output
7+
{
8+
public string message
9+
{
10+
get;
11+
set;
12+
}
13+
14+
public Output(string message) { this.message = message; }
15+
}
16+
17+
}
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Function that sends an email using OCI Email Delivery
2+
This function sends an email using the OCI Email Delivery Service.
3+
4+
As you make your way through this tutorial, look out for this icon ![user input icon](./images/userinput.png).
5+
Whenever you see it, it's time for you to perform an action.
6+
7+
8+
## Prerequisites
9+
Before you deploy this sample function, make sure you have run step A, B and C of the [Oracle Functions Quick Start Guide for Cloud Shell](https://www.oracle.com/webfolder/technetwork/tutorials/infographics/oci_functions_cloudshell_quickview/functions_quickview_top/functions_quickview/index.html)
10+
* A - Set up your tenancy
11+
* B - Create application
12+
* C - Set up your Cloud Shell dev environment
13+
14+
15+
## List Applications
16+
Assuming your have successfully completed the prerequisites, you should see your
17+
application in the list of applications.
18+
```
19+
fn ls apps
20+
```
21+
22+
23+
## Review and customize the function
24+
Review the following files in the current folder:
25+
* the code of the function, [SendEmail.cs](./SendEmail.cs)
26+
* its dependencies, [SendEmail.csproj](./SendEmail.csproj)
27+
* the function metadata, [func.yaml](./func.yaml)
28+
29+
30+
## Deploy the function
31+
In Cloud Shell, run the *fn deploy* command to build the function and its dependencies as a Docker image,
32+
push the image to OCIR, and deploy the function to Oracle Functions in your application.
33+
34+
![user input icon](./images/userinput.png)
35+
```
36+
fn -v deploy --app <app-name>
37+
```
38+
39+
40+
## Configure the Email Delivery Service
41+
![user input icon](./images/userinput.png)
42+
43+
On the OCI console, navigate to *Email Delivery*. Click `Create Approved Sender`.
44+
![create topic](./images/create_approved_sender-1.png)
45+
46+
Enter the email address of the approved sender and click `Create Approved Sender`.
47+
48+
![create subscription](./images/create_approved_sender-2.png)
49+
50+
The creation of the *Approved Sender* takes several minutes.
51+
52+
Navigate to *Email Delivery* > *Email Configuration* and write down the SMTP server name.
53+
Click `Manage SMTP Credentials` which takes you to your *User Details*, click on `Generate SMTP Credentials` and enter a description. Your SMTP Username and password will be displayed, write them down, you will need them to configure the function.
54+
55+
56+
## Set the function configuration values
57+
The function requires the following configuration values to be set:
58+
- smtp-username
59+
- smtp-password
60+
- smtp-host
61+
- smtp-port (25 or 587)
62+
63+
![user input icon](./images/userinput.png)
64+
65+
Use the *fn CLI* to set the config value:
66+
```
67+
fn config function <app-name> <function-name> smtp-username <SMTP-username>
68+
fn config function <app-name> <function-name> smtp-password <SMTP-password>
69+
fn config function <app-name> <function-name> smtp-host <SMTP-Server-name>
70+
fn config function <app-name> <function-name> smtp-port <SMTP-Port>
71+
```
72+
e.g.
73+
```
74+
fn config function myapp oci-email-send-dotnet smtp-username "ocid1.user.oc1..aaaaaaaause3s2zw3kn3qvxxc5c5ouc4pu6byfxiuplncjkzzxinijhmqj5q@ocid1.tenancy.oc1..aaaaaaaaydrjm77otncda2xn7qtv7l3hqnd3zxn2u6siwdhniibwfv4wwhta.7g.com"
75+
fn config function myapp oci-email-send-dotnet smtp-password '{$M$mWmvlN&P#o>!14F8'
76+
fn config function myapp oci-email-send-dotnet smtp-host "smtp.us-phoenix-1.oraclecloud.com"
77+
fn config function myapp oci-email-send-dotnet smtp-port 587
78+
```
79+
80+
81+
## Invoke the function
82+
The function requires the following keys in the payload to be invoked:
83+
- sender_email
84+
- sender_name
85+
- recipient
86+
- subject
87+
- body
88+
89+
![user input icon](./images/userinput.png)
90+
```
91+
echo '{ "sender_email":"<approved-sender-email>", "sender_name":"<sender-name>", "recipient":"<recipient-email>",
92+
"subject":"<email-subject>", "body":"<email-body>" }' | fn invoke <app-name> oci-email-send-dotnet
93+
```
94+
e.g.:
95+
```
96+
echo '{ "sender_email":"[email protected]", "sender-name":"Test", "recipient":"[email protected]",
97+
"subject":"Hello!", "body":"This is a test email" }' | fn invoke myapp oci-email-send-dotnet
98+
```
99+
100+
Upon success, the function will return "Email successfully sent!" and the recipient will receive an email.
101+
102+
103+
## Monitoring Functions
104+
105+
Learn how to configure basic observability for your function using metrics, alarms and email alerts:
106+
* [Basic Guidance for Monitoring your Functions](../basic-observability/functions.md)
107+
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Fnproject.Fn.Fdk;
2+
using System.Runtime.CompilerServices;
3+
using System.Collections.Generic;
4+
using System;
5+
using System.Text;
6+
using System.Text.Json;
7+
using System.Threading.Tasks;
8+
using MailKit.Net.Smtp;
9+
using MailKit.Security;
10+
using MailKit;
11+
using MimeKit;
12+
13+
namespace SendEmail
14+
{
15+
class Function
16+
{
17+
public Output function_handler(InputMessage input)
18+
{
19+
20+
string smtp_password = Environment.GetEnvironmentVariable("smtp-password");
21+
string smtp_user_name = Environment.GetEnvironmentVariable("smtp-username");
22+
string smtp_endpoint = Environment.GetEnvironmentVariable("smtp-host");
23+
string smtp_port_str = Environment.GetEnvironmentVariable("smtp-port");
24+
int smtp_port = Int32.Parse(smtp_port_str);
25+
string from_email = input.sender_email;
26+
string from_name = input.sender_name;
27+
28+
var message = new MimeMessage();
29+
message.From.Add(new MailboxAddress(from_name, from_email));
30+
message.To.Add(new MailboxAddress("Dear recipient", input.recipient));
31+
message.Subject = input.subject;
32+
33+
message.Body = new TextPart("plain")
34+
{
35+
Text = @input.body
36+
};
37+
38+
try
39+
{
40+
41+
42+
using (var client = new SmtpClient())
43+
{
44+
client.Connect(smtp_endpoint, smtp_port, SecureSocketOptions.StartTls);
45+
client.Authenticate(smtp_user_name, smtp_password);
46+
client.Send(message);
47+
client.Disconnect(true);
48+
}
49+
50+
return new Output(string.Format(
51+
"Mail Sent to {0}!",
52+
string.IsNullOrEmpty(input.recipient) ? "<invalid_email_id>" : input.recipient.Trim()));
53+
54+
}
55+
catch (Exception ex)
56+
{
57+
Console.WriteLine("Error: " + ex.Message);
58+
return new Output(string.Format(ex.Message));
59+
60+
}
61+
62+
}
63+
64+
static void Main(string[] args) { Fdk.Handle(args[0]); }
65+
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Fnproject.Fn.Fdk" Version="1.0.4" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="OCI.DotNetSDK.Common" Version="42.1.0" />
14+
<PackageReference Include="MailKit" Version="3.3.0" />
15+
<PackageReference Include="OCI.DotNetSDK.Core" Version="42.1.0" />
16+
</ItemGroup>
17+
18+
</Project>
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
schema_version: 20180708
2+
name: oci-email-send-dotnet
3+
version: 0.0.107
4+
runtime: dotnet3.1
5+
build_image: fnproject/dotnet:3.1-1.0.4-dev
6+
run_image: fnproject/dotnet:3.1-1.0.4
7+
cmd: SendEmail:Function:function_handler
8+
entrypoint: dotnet SendEmail.dll
Loading
Loading
Loading

0 commit comments

Comments
 (0)