From 7584706123aa1df875ed2c4499836119af4af1aa Mon Sep 17 00:00:00 2001 From: Santhosh Nair Date: Wed, 20 Mar 2024 10:11:51 -0400 Subject: [PATCH] added methode3 --- Controllers/EmailController.cs | 42 +++++++++++++++++++++++++++++++- Controllers/IHttpActionResult.cs | 6 +++++ EmailServiceApi.sln | 2 +- Models/EmailRequest.cs | 11 ++++++--- Services/EmailService.cs | 3 +++ docker-compose.yml | 11 +++++++++ 6 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 Controllers/IHttpActionResult.cs create mode 100644 docker-compose.yml diff --git a/Controllers/EmailController.cs b/Controllers/EmailController.cs index b5bade8..77a0184 100644 --- a/Controllers/EmailController.cs +++ b/Controllers/EmailController.cs @@ -2,6 +2,8 @@ using EmailServiceApi.Services; using EmailServiceApi.Models; +using System.Net.Mail; +using System.Net; namespace EmailServiceApi.Controllers { @@ -116,8 +118,46 @@ namespace EmailServiceApi.Controllers } + + [HttpPost("Methode3")] + + public ActionResult SendEmailAsync(EmailRequest3 emailRequest, string smtpServer = "smtp.gmail.com", int smtpPort = 587, string? smtpUsername = "smtp User", string? smtpPassword = "smtp password", string? fromEmail = "from-email@domain.com", string? ToAddress= "to-email@domain.com") + { + try + { + //string smtpServer = "smtp.gmail.com"; + //int smtpPort = 587; + //string smtpUsername = "vijay.net33@gmail.com"; + //string smtpPassword = "nmlzxgkhmafdxxul"; + //string fromEmail = "vijay.net33@gmail.com"; + + using (MailMessage mail = new MailMessage()) + { + mail.From = new MailAddress(fromEmail); + mail.To.Add(ToAddress); + mail.Subject = emailRequest.Subject; + mail.Body = emailRequest.Body; + + using (SmtpClient smtp = new SmtpClient(smtpServer, smtpPort)) + { + smtp.Credentials = new NetworkCredential(smtpUsername, smtpPassword); + smtp.EnableSsl = true; + smtp.SendMailAsync(mail); + } + } + + return Ok("Email sent successfully."); + } + catch (Exception ex) + { + return StatusCode(500, $"Failed to send email: {ex.Message}"); + } + } + } +} + //public class EmailRequest //{ // public string SmtpServer { get; set; } @@ -126,4 +166,4 @@ namespace EmailServiceApi.Controllers // public string Subject { get; set; } // public string Body { get; set; } //} -} + diff --git a/Controllers/IHttpActionResult.cs b/Controllers/IHttpActionResult.cs new file mode 100644 index 0000000..c162840 --- /dev/null +++ b/Controllers/IHttpActionResult.cs @@ -0,0 +1,6 @@ +namespace EmailServiceApi.Controllers +{ + public interface IHttpActionResult + { + } +} \ No newline at end of file diff --git a/EmailServiceApi.sln b/EmailServiceApi.sln index 626ff32..139c024 100644 --- a/EmailServiceApi.sln +++ b/EmailServiceApi.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.7.34031.279 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmailServiceApi", "EmailServiceApi.csproj", "{A4017F77-8FC9-4816-AF54-90B55F66BDE1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EmailServiceApi", "EmailServiceApi.csproj", "{A4017F77-8FC9-4816-AF54-90B55F66BDE1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Models/EmailRequest.cs b/Models/EmailRequest.cs index c917659..df7dece 100644 --- a/Models/EmailRequest.cs +++ b/Models/EmailRequest.cs @@ -16,9 +16,14 @@ public string ToAddress { get; set; } public string? Subject { get; set; } public string? Message { get; set; } - public string username { get; set; } + public string username { get; set; } public string password { get; set; } } - -} + public class EmailRequest3 + { + // public string ToAddress { get; set; } + public string Subject { get; set; } + public string Body { get; set; } + } +} \ No newline at end of file diff --git a/Services/EmailService.cs b/Services/EmailService.cs index 5b69729..2198a29 100644 --- a/Services/EmailService.cs +++ b/Services/EmailService.cs @@ -89,5 +89,8 @@ namespace EmailServiceApi.Services // return $"Failed to send email: {ex.Message}"; } } + + + } } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ed0b93a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3.8' + +services: + email-service: + build: + context: . + dockerfile: Dockerfile + environment: + ASPNETCORE_ENVIRONMENT: Development + ports: + - "9001:80"