123 lines
3.9 KiB
C#
123 lines
3.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Mail;
|
|
using System.Web.Http;
|
|
|
|
namespace VECV_WebApi.Controllers.NotificationEnginEmailSMS
|
|
{
|
|
public class NotificationEnginEmailSMSController : ApiController
|
|
{
|
|
// GET api/<controller>
|
|
public IEnumerable<string> Get()
|
|
{
|
|
return new string[] { "value1", "value2" };
|
|
}
|
|
|
|
// GET api/<controller>/5
|
|
public string Get(int id)
|
|
{
|
|
return "value";
|
|
}
|
|
|
|
// POST api/<controller>
|
|
public void Post([FromBody]string value)
|
|
{
|
|
}
|
|
|
|
// PUT api/<controller>/5
|
|
public void Put(int id, [FromBody]string value)
|
|
{
|
|
}
|
|
|
|
// DELETE api/<controller>/5
|
|
public void Delete(int id)
|
|
{
|
|
}
|
|
|
|
public class MailModel
|
|
{
|
|
public string MailTo { get; set; }
|
|
public string MaillCC { get; set; }
|
|
public string Subject { get; set; }
|
|
public string Msg { get; set; }
|
|
public bool IsBodyHtml { get; set; }
|
|
public EncodedAttachment attach { get; set; }
|
|
public string SMTP_From { get; set; }
|
|
public string SMTP_User { get; set; }
|
|
public string SMTP_Host { get; set; }
|
|
public string SMTP_Pwd { get; set; }
|
|
public int SMTP_Port { get; set; }
|
|
public bool Enable_SSL { get; set; }
|
|
}
|
|
|
|
public class EncodedAttachment
|
|
{
|
|
public string Base64Attachment;
|
|
public string Name;
|
|
public string MediaType;
|
|
}
|
|
//
|
|
// GET: /NotificationEnginEmailSMS/
|
|
//[HttpPost]
|
|
public MailModel Post([FromUri] string message, [FromUri] string mailTo, [FromBody] string subject)
|
|
{
|
|
try
|
|
{
|
|
MailModel mail = new MailModel
|
|
{
|
|
MailTo = mailTo,
|
|
Msg = message,
|
|
Subject = subject,
|
|
IsBodyHtml = true,
|
|
SMTP_User = "eichertestemail@gmail.com",
|
|
SMTP_From = "noreplyfieldforcetracking@gmail.com",
|
|
SMTP_Host = "smtp.gmail.com",
|
|
SMTP_Port = 25,
|
|
SMTP_Pwd = "eicher@123",
|
|
Enable_SSL = false
|
|
};
|
|
|
|
MailMessage msg = new MailMessage(mail.SMTP_From, mail.MailTo, mail.Subject, mail.Msg);
|
|
msg.IsBodyHtml = mail.IsBodyHtml;
|
|
|
|
if (mail.attach != null)
|
|
{
|
|
Attachment attach = CreateAttachment(mail.attach);
|
|
msg.Attachments.Add(attach);
|
|
}
|
|
if (!string.IsNullOrEmpty(mail.MaillCC))
|
|
msg.CC.Add(mail.MaillCC);
|
|
|
|
NetworkCredential cred = new
|
|
NetworkCredential(mail.SMTP_User, mail.SMTP_Pwd);
|
|
SmtpClient mailClient = new SmtpClient(mail.SMTP_Host, mail.SMTP_Port);
|
|
mailClient.EnableSsl = false;
|
|
mailClient.UseDefaultCredentials = false;
|
|
mailClient.Credentials = cred;
|
|
mailClient.Send(msg);
|
|
return mail;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Function create attachement with the mail.
|
|
/// </summary>
|
|
/// <param name="encodedAtt"></param>
|
|
/// <returns></returns>
|
|
Attachment CreateAttachment(EncodedAttachment encodedAtt)
|
|
{
|
|
MemoryStream reader = new MemoryStream(Convert.FromBase64String(encodedAtt.Base64Attachment));
|
|
Attachment att = new Attachment(reader, encodedAtt.Name, encodedAtt.MediaType);
|
|
return att;
|
|
}
|
|
|
|
}
|
|
} |