Skip to content

Instantly share code, notes, and snippets.

@syron
Created April 19, 2016 18:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syron/a24530a486b460c9ccc6f2033f2c0efc to your computer and use it in GitHub Desktop.
Save syron/a24530a486b460c9ccc6f2033f2c0efc to your computer and use it in GitHub Desktop.
#r "Newtonsoft.Json"
using System;
using System.Net;
using Newtonsoft.Json;
using System.Net.Mail;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
log.Verbose($"Webhook was triggered!");
string jsonContent = await req.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(jsonContent);
// do some data validation... skipped this for demo purpose only.
// if validation failes -> HttpStatusCode.BadRequest should be returned as HTTP Status
bool isImportantEmail = bool.Parse(data.isImportant.ToString());
string fromEmail = data.fromEmail;
string toEmail = data.toEmail;
int smtpPort = 587;
bool smtpEnableSsl = true;
string smtpHost = ""; // your smtp host
string smtpUser = ""; // your smtp user
string smtpPass = ""; // your smtp password
string subject = data.subject;
string message = data.message;
MailMessage mail = new MailMessage(fromEmail, toEmail);
SmtpClient client = new SmtpClient();
client.Port = smtpPort;
client.EnableSsl = smtpEnableSsl;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = smtpHost;
client.Credentials = new System.Net.NetworkCredential(smtpUser, smtpPass);
mail.Subject = message;
if (isImportantEmail) {
mail.Priority = MailPriority.High;
}
mail.Body = message;
try {
client.Send(mail);
log.Verbose("Email sent.");
return req.CreateResponse(HttpStatusCode.OK, new {
status = true,
message = string.Empty
});
}
catch (Exception ex) {
log.Verbose(ex.ToString());
return req.CreateResponse(HttpStatusCode.InternalServerError, new {
status = false,
message = "Message has not been sent. Check Azure Function Logs for more information."
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment