added methode3

This commit is contained in:
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,8 +118,46 @@ 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
//{
// public string SmtpServer { get; set; }
@ -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
{
}
}