EmailServiceApi/Controllers/EmailController.cs
2024-03-19 18:48:19 -04:00

130 lines
4.0 KiB
C#

using Microsoft.AspNetCore.Mvc;
using EmailServiceApi.Services;
using EmailServiceApi.Models;
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}");
}
}
}
//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; }
//}
}