using Microsoft.AspNetCore.Mvc;
using EmailServiceApi.Services;
using EmailServiceApi.Models;
using System.Net.Mail;
using System.Net;
namespace EmailServiceApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class EmailController : ControllerBase
{
private readonly EmailService _emailService;
///
/// Email Testing API
///
///
public EmailController(EmailService emailService)
{
_emailService = emailService;
}
///
/// Generate Sample Request for Method I (provide user name and password)
///
///
[HttpGet("methode1/request")]
public ActionResult 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.");
}
}
///
///Send email using methode I
///
///
///
[HttpPost("methode1/send")]
public ActionResult 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}");
}
}
///
/// Generate Sample Request for Method II
///
///
[HttpGet("methode2/request")]
public ActionResult 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.");
}
}
///
/// Send email using methode II
///
///
///
[HttpPost("methode2/send")]
public ActionResult 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}");
}
}
[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; }
// public string FromAddress { get; set; }
// public string ToAddress { get; set; }
// public string Subject { get; set; }
// public string Body { get; set; }
//}