|
| 1 | + |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using SimpleEmailApp.CorrelationService; |
| 4 | +using System.Text.Json; |
| 5 | + |
| 6 | +namespace SimpleEmailApp.Controllers |
| 7 | +{ |
| 8 | + [Route("api/[controller]")] |
| 9 | + [ApiController] |
| 10 | + public class MailController : ControllerBase |
| 11 | + { |
| 12 | + private readonly IMailService _mailService; |
| 13 | + private readonly ILogger<MailController> _logger; |
| 14 | + private readonly ICorrelationIdGenerator _correlationIdGenerator; |
| 15 | + public MailController(IMailService mailService, ILogger<MailController> logger, |
| 16 | + ICorrelationIdGenerator correlationIdGenerator) |
| 17 | + { |
| 18 | + _mailService = mailService; |
| 19 | + _logger = logger; |
| 20 | + _correlationIdGenerator = correlationIdGenerator; |
| 21 | + } |
| 22 | + |
| 23 | + [HttpPost] |
| 24 | + public async Task<IActionResult> SendMail([FromForm] EmailMessage req) |
| 25 | + { |
| 26 | + _logger.LogInformation("..................... Sending Email Function Start............."); |
| 27 | + try |
| 28 | + { |
| 29 | + await _mailService.SendEmailAsync(req); |
| 30 | + _logger.LogInformation("CorrelationId {correlationId}: ", |
| 31 | + _correlationIdGenerator.Get()); |
| 32 | + |
| 33 | + //_logger.LogInformation($" Email {},{req.Reciver},{req.Subject},{req.Body} is send."); |
| 34 | + return Ok($"Email sended to {req.Reciver} sucessfully."); |
| 35 | + |
| 36 | + } |
| 37 | + catch (Exception) |
| 38 | + { |
| 39 | + return NotFound(); |
| 40 | + // _logger.LogError("We have error in sending Email."); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + //public IActionResult SendEmail(string subject , string body , string sender) |
| 49 | + //{ |
| 50 | + // var email = new MimeMessage(); |
| 51 | + // email.From.Add(MailboxAddress.Parse("anivargudeov@gmail.com")); |
| 52 | + // email.To.Add(MailboxAddress.Parse("anivargudeov@gmail.com")); |
| 53 | + // email.Subject = subject; |
| 54 | + // email.Body =new TextPart(TextFormat.Plain) { Text= body }; |
| 55 | + |
| 56 | + // using var smtp = new SmtpClient(); |
| 57 | + // smtp.Connect("smtp.gmail.com", 587,SecureSocketOptions.StartTls); |
| 58 | + // smtp.Authenticate("anivargudeov@gmail.com", "fsobaslupqqlmhtv"); |
| 59 | + // smtp.Send(email); |
| 60 | + // smtp.Disconnect(true); |
| 61 | + // return Ok(); |
| 62 | + //} |
| 63 | + |
| 64 | + } |
| 65 | +} |
0 commit comments