EmailServiceApi/Controllers/EmailController.cs

170 lines
5.6 KiB
C#
Raw Permalink Normal View History

2024-03-19 17:48:19 -05:00
using Microsoft.AspNetCore.Mvc;
using EmailServiceApi.Services;
using EmailServiceApi.Models;
2024-03-20 09:11:51 -05:00
using System.Net.Mail;
using System.Net;
2024-03-19 17:48:19 -05:00
namespace EmailServiceApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class EmailController : ControllerBase
{
private readonly EmailService _emailService;
/// <summary>
/// Email Testing API
/// </summary>
/// <param name="emailService"></param>
public EmailController(EmailService emailService)
{
_emailService = emailService;
}
/// <summary>
/// Generate Sample Request for Method I (provide user name and password)
/// </summary>
/// <returns></returns>
[HttpGet("methode1/request")]
public ActionResult<EmailRequest1> GetMailParameters4Method1()
{
try
{
var emailRequest = new EmailRequest1
{
smptserver = "mail-00.dadeschools.net",
Subject = "Test WB",
FromAddress = "no-reply@dadeschools.net",
ToAddress = "",
Message = "Testemail",
username = "username",
password = "password",
// Times = 1
};
return Ok(emailRequest);
}
catch (Exception ex)
{
return StatusCode(500, "An error occurred while processing the request.");
}
}
/// <summary>
///Send email using methode I
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpPost("methode1/send")]
public ActionResult<string> SendMethode1([FromBody] EmailRequest1 request)
{
try
{
_emailService.SendEmailmethode1(request.smptserver = "mail-00.dadeschools.net", request.FromAddress, request.ToAddress, request.Subject, request.Message, request.username, request.password);
return Ok("Email sent successfully.");
}
catch (Exception ex)
{
return StatusCode(500, $"Failed to send email: {ex.Message}");
}
}
/// <summary>
/// Generate Sample Request for Method II
/// </summary>
/// <returns></returns>
[HttpGet("methode2/request")]
public ActionResult<EmailRequest2> GetMailParameters4Method2()
{
try
{
var emailRequest = new EmailRequest2
{
smptserver= "mail-00.dadeschools.net",
Subject = "Test WB",
FromAddress = "no-reply@dadeschools.net",
ToAddress = "",
Message="Testemail",
Times=1
};
return Ok(emailRequest);
}
catch (Exception ex)
{
return StatusCode(500, "An error occurred while processing the request.");
}
}
/// <summary>
/// Send email using methode II
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpPost("methode2/send")]
public ActionResult<string> SendMthode2([FromBody] EmailRequest2 request)
{
try
{
_emailService.SendEmailMethode2(request.smptserver, request.FromAddress, request.ToAddress, request.Subject, request.Message, request.Times);
return Ok("Email sent successfully.");
}
catch (Exception ex)
{
return StatusCode(500, $"Failed to send email: {ex.Message}");
}
}
2024-03-20 09:11:51 -05:00
[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}");
}
}
2024-03-19 17:48:19 -05:00
}
2024-03-20 09:11:51 -05:00
}
2024-03-19 17:48:19 -05:00
//public class EmailRequest
//{
// public string SmtpServer { get; set; }
// public string FromAddress { get; set; }
// public string ToAddress { get; set; }
// public string Subject { get; set; }
// public string Body { get; set; }
//}
2024-03-20 09:11:51 -05:00