added methode3

This commit is contained in:
Santhosh Nair 2024-03-20 10:11:51 -04:00
parent bb57113461
commit 7584706123
6 changed files with 70 additions and 5 deletions

View File

@ -2,6 +2,8 @@
using EmailServiceApi.Services;
using EmailServiceApi.Models;
using System.Net.Mail;
using System.Net;
namespace EmailServiceApi.Controllers
{
@ -116,6 +118,44 @@ namespace EmailServiceApi.Controllers
}
[HttpPost("Methode3")]
public ActionResult<string> 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
@ -126,4 +166,4 @@ namespace EmailServiceApi.Controllers
// public string Subject { get; set; }
// public string Body { get; set; }
//}
}

View File

@ -0,0 +1,6 @@
namespace EmailServiceApi.Controllers
{
public interface IHttpActionResult
{
}
}

View File

@ -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

View File

@ -20,5 +20,10 @@
public string password { get; set; }
}
public class EmailRequest3
{
// public string ToAddress { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
}

View File

@ -89,5 +89,8 @@ namespace EmailServiceApi.Services
// return $"Failed to send email: {ex.Message}";
}
}
}
}

11
docker-compose.yml Normal file
View File

@ -0,0 +1,11 @@
version: '3.8'
services:
email-service:
build:
context: .
dockerfile: Dockerfile
environment:
ASPNETCORE_ENVIRONMENT: Development
ports:
- "9001:80"