old email sender

mail@pastecode.io avatar
unknown
csharp
2 years ago
2.1 kB
1
Indexable
Never
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using MimeKit.Text;
using SendGrid;
using SendGrid.Helpers.Mail;

namespace employmently_be.Data.Models
{
    public class EmailSender
    {
        private readonly IConfiguration _config;


        public EmailSender(IConfiguration config)
        {
            _config = config;
        }

        public async Task<bool> SendEmailsAsync(string userEmail,string confirmationLink)
        {


            var apiKey = Environment.GetEnvironmentVariable("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");
            var client = new SendGridClient(apiKey);
            var msg = new SendGridMessage()
            {
                From = new EmailAddress("[REPLACE WITH YOUR EMAIL]", "[REPLACE WITH YOUR NAME]"),
                Subject = "Sending with Twilio SendGrid is Fun",
                PlainTextContent = "and easy to do anywhere, especially with C#"
            };
            msg.AddTo(new EmailAddress("[REPLACE WITH DESIRED TO EMAIL]", "[REPLACE WITH DESIRED TO NAME]"));
            var response = await client.SendEmailAsync(msg);

            return response.IsSuccessStatusCode;
        }


        public bool SendEmail(string userEmail, string confirmationLink)
        {

            var emailPass = _config["emailPassword"];
            
            var email = new MimeMessage();
            email.From.Add(MailboxAddress.Parse("employmentlyy@gmail.com"));
            email.To.Add(MailboxAddress.Parse(userEmail));
            email.Subject = "Confirm your email adress - Employmently";
            email.Body = new TextPart(TextFormat.Html) { Text = confirmationLink};

            // send email
            using var smtp = new SmtpClient();
            smtp.Connect("smtp.gmail.com", 25, SecureSocketOptions.StartTls);
            smtp.Authenticate("employmentlyy@gmail.com", "wlqegxqlkepwrguh");
            smtp.Send(email);
            smtp.Disconnect(true);
            return true;
        }
    }
}