forked from oracle-samples/oracle-functions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSendEmail.cs
67 lines (54 loc) · 2.05 KB
/
SendEmail.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Fnproject.Fn.Fdk;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using MailKit.Net.Smtp;
using MailKit.Security;
using MailKit;
using MimeKit;
namespace SendEmail
{
class Function
{
public Output function_handler(InputMessage input)
{
string smtp_password = Environment.GetEnvironmentVariable("smtp-password");
string smtp_user_name = Environment.GetEnvironmentVariable("smtp-username");
string smtp_endpoint = Environment.GetEnvironmentVariable("smtp-host");
string smtp_port_str = Environment.GetEnvironmentVariable("smtp-port");
int smtp_port = Int32.Parse(smtp_port_str);
string from_email = input.sender_email;
string from_name = input.sender_name;
var message = new MimeMessage();
message.From.Add(new MailboxAddress(from_name, from_email));
message.To.Add(new MailboxAddress("Dear recipient", input.recipient));
message.Subject = input.subject;
message.Body = new TextPart("plain")
{
Text = @input.body
};
try
{
using (var client = new SmtpClient())
{
client.Connect(smtp_endpoint, smtp_port, SecureSocketOptions.StartTls);
client.Authenticate(smtp_user_name, smtp_password);
client.Send(message);
client.Disconnect(true);
}
return new Output(string.Format(
"Mail Sent to {0}!",
string.IsNullOrEmpty(input.recipient) ? "<invalid_email_id>" : input.recipient.Trim()));
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
return new Output(string.Format(ex.Message));
}
}
static void Main(string[] args) { Fdk.Handle(args[0]); }
}
}