Skip to main content
deleted 15 characters in body
Source Link
   
  "SysAdminEmail": "[email protected] ",
  "HostPort": {
    "Port": 587,
    "Host": "smtp**strong text**"smtp.com"
  }
}
   
  "SysAdminEmail": "[email protected] ",
  "HostPort": {
    "Port": 587,
    "Host": "smtp**strong text**.com"
  }
}
   
  "SysAdminEmail": "[email protected] ",
  "HostPort": {
    "Port": 587,
    "Host": "smtp.com"
  }
}
added 900 characters in body
Source Link

using MailKit.Net.Smtp;
using Microsoft.EntityFrameworkCore;
using MimeKit;

namespace Admin.Services
{
    public class EmailService
    {
        private readonly string _from;
        private readonly string _host;
        private readonly int _port;
        private readonly string _smtpUser;
        private readonly string _smtpPassword;
        private readonly IHttpContextAccessor _contextAccessor;
        private readonly Context _Context;
        private readonly PassResetByEmailService _passResetByEmailService;
        public EmailService(
            Context Context,
            IConfiguration configuration,
            IHttpContextAccessor contextAccessor,
            PassResetByEmailService passResetByEmailService
            )
        {
            _from = configuration["EmailConfig:From"];
            _host = configuration["EmailConfig:Host"];
            _port = int.Parse(configuration["EmailConfig:Port"]);
            _smtpUser = configuration["EmailConfig:UserName"];
            _smtpPassword = configuration["EmailConfig:Password"];
            _passResetByEmailService = passResetByEmailService;
            _Context = Context;
            _contextAccessor = contextAccessor;
        }

  

using MailKit.Net.Smtp;
using Microsoft.EntityFrameworkCore;
using MimeKit;

namespace Admin.Services
{
    public class EmailService
    {
        private readonly string _from;
        private readonly string _host;
        private readonly int _port;
        private readonly string _smtpUser;
        private readonly string _smtpPassword;
        private readonly IHttpContextAccessor _contextAccessor;
        private readonly Context _Context;
        private readonly PassResetByEmailService _passResetByEmailService;
        public EmailService(
            Context Context,
            IConfiguration configuration,
            IHttpContextAccessor contextAccessor,
            PassResetByEmailService passResetByEmailService
            )
        {
            _from = configuration["EmailConfig:From"];
            _host = configuration["EmailConfig:Host"];
            _port = int.Parse(configuration["EmailConfig:Port"]);
            _smtpUser = configuration["EmailConfig:UserName"];
            _smtpPassword = configuration["EmailConfig:Password"];
            _passResetByEmailService = passResetByEmailService;
            _Context = Context;
            _contextAccessor = contextAccessor;
        }

  

added 900 characters in body
Source Link
        public async Task<string?> SendPassEmail(string email)
        {
        //Check email nel DB 
        var remail = await _grabcadContext.UTENTE.FirstOrDefaultAsync(c => c.EMAIL == email && !c.FLG_DISABILITATO && !c.FLG_ELIMINATO);
        if (remail == null)
            return "Email non trovata";

        var request = _contextAccessor.HttpContext.Request;
        var requestUrl = $"{request.Scheme}://{request.Host}{request.PathBase}";
        var resetPasswordUrl = $"{requestUrl}/Account/ResetPasswordToken";

        var token = await _passResetByEmailService.AddPassResetByEmailService(email);

        var subject = "Reset password";
        var content = $"link: {resetPasswordUrl}?email={email}&token={token}";

        using (var client = new SmtpClient())
        {
            try
            {
                var emailMessage = new MimeMessage();
                emailMessage.From.Add(new MailboxAddress(_from, _from));
                emailMessage.To.Add(new MailboxAddress(email, email));
                emailMessage.Subject = subject;
                emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Text) { Text = content }; 


                await client.ConnectAsync(_host, _port, true);
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                await client.AuthenticateAsync(_smtpUser, _smtpPassword);
                await client.SendAsync(emailMessage);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                await client.DisconnectAsync(true);
                client.Dispose();
            }
        }
        return null;
    }
  }
}
using (var client = new SmtpClient())
        {
            try
            {
                var emailMessage = new MimeMessage();
                emailMessage.From.Add(new MailboxAddress(_from, _from));
                emailMessage.To.Add(new MailboxAddress(email, email));
                emailMessage.Subject = subject;
                emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Text) { Text = content };

                await client.ConnectAsync(_host, _port, true);
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                await client.AuthenticateAsync(_smtpUser, _smtpPassword);
                await client.SendAsync(emailMessage);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                await client.DisconnectAsync(true);
                client.Dispose();
            }
        }
        return null;
    }
        public async Task<string?> SendPassEmail(string email)
        {
        //Check email nel DB 
        var remail = await _grabcadContext.UTENTE.FirstOrDefaultAsync(c => c.EMAIL == email && !c.FLG_DISABILITATO && !c.FLG_ELIMINATO);
        if (remail == null)
            return "Email non trovata";

        var request = _contextAccessor.HttpContext.Request;
        var requestUrl = $"{request.Scheme}://{request.Host}{request.PathBase}";
        var resetPasswordUrl = $"{requestUrl}/Account/ResetPasswordToken";

        var token = await _passResetByEmailService.AddPassResetByEmailService(email);

        var subject = "Reset password";
        var content = $"link: {resetPasswordUrl}?email={email}&token={token}";

        using (var client = new SmtpClient())
        {
            try
            {
                var emailMessage = new MimeMessage();
                emailMessage.From.Add(new MailboxAddress(_from, _from));
                emailMessage.To.Add(new MailboxAddress(email, email));
                emailMessage.Subject = subject;
                emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Text) { Text = content }; 


                await client.ConnectAsync(_host, _port, true);
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                await client.AuthenticateAsync(_smtpUser, _smtpPassword);
                await client.SendAsync(emailMessage);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                await client.DisconnectAsync(true);
                client.Dispose();
            }
        }
        return null;
    }
  }
}
Source Link
Loading