Topic:   SendEmailAsync with Attchment.
Feb 13, 2021 10:24 1 Replies 70 Views TEJA

I'd like some help applying IEmailSender Interface to a class of mine called EmailSender.

This interface apply one method called SendEmailAsync(String email, String subject, String message) which works perfectly!

In some cases i want to send emails with attachments of type base64 string ,using the same way or something similar is it possible or is there a way i didn't think of to manipulate the class?

EmailSender.cs

    public class EmailSender: IEmailSender
    {
        public EmailOptions Options { get; set; }

 
        public EmailSender(IOptions emailOptions)
        {
            Options = emailOptions.Value;
        }

 
        public Task SendEmailAsync(string email, string subject, string message)
        {
            return Execute(Options.SendGridKey, subject, message, email);
        }

 
        private Task Execute(string sendGridKey, string subject, string message, string email)
        {
            var client = new SendGridClient(sendGridKey);
            var msg = new SendGridMessage()
            {
                From = new EmailAddress("no-reply@---", "no-reply"),
                Subject = subject,
                PlainTextContent = message,
                HtmlContent = message
            };
            msg.AddTo(new EmailAddress(email));
            try
            {
                return client.SendEmailAsync(msg);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} First exception caught.", ex);
            }
            return null;
        }
    }

when called

 await _emailSender.SendEmailAsync(
                                   _db.Users.Where("aa@aa.com"
                                    "email subject",
                                    "blah" + blah ");
Prev Next
Topic Replies (1)
  1. 1
    idnkx user

    PARTH

    Please refer to the below example -
    You can modify the demo below according to your needs.
    UserMailViewModel:

    public class UserMailViewModel
    {
    public string Sender { get; set; }
    public string SenderPassword { get; set; }
    public string Recipients { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public IFormFile Attachment { get; set; }
    }
    View:

    @ViewBag.Message
    @model DailyCoreMVCDemo.Models.UserMailViewModel






























    EmailSender:

    public class EmailSender : IEmailSender
    {
    public IFormFile Attachment { get; set; }
    public EmailOptions Options { get; set; }
    public Task SendEmailAsync(string recipient, string subject, string body)
    {
    throw new NotImplementedException();
    }
    public async Task SendEmailAsync(EmailDto input)
    {
    var client = new SendGridClient("xxxxxxx");
    var from = new EmailAddress(Options.SenderAccount,"Example User");
    var subject =input.Subject;
    var to = new EmailAddress(input.Recipients,"Example User");
    var htmlContent =input.Body;
    var msg = MailHelper.CreateSingleEmail(from, to, subject, null, htmlContent);
    if (Attachment != null)
    {
    var ms = new MemoryStream();
    Attachment.CopyTo(ms);
    var fileBytes = ms.ToArray();
    string file = Convert.ToBase64String(fileBytes);
    msg.AddAttachment(Attachment.FileName, file);
    }
    var response = await client.SendEmailAsync(msg);
    return response.IsSuccessStatusCode;
    }
    }
    Controller:

    public class EmailSenderController : Controller
    {
    private EmailSender _emailSender;
    public EmailSenderController(EmailSender emailSender)
    {
    _emailSender = emailSender;
    }
    public IActionResult Index()
    {
    return View();
    }
    [HttpPost]
    public async Task Index(UserMailViewModel model)
    {
    _emailSender.Attachment = model.Attachment;
    _emailSender.Options = new AspNetCore.Email.EmailOptions {
    SenderAccount=model.Sender,
    SenderPassword=model.SenderPassword
    };
    EmailDto msg = new EmailDto {
    Recipients=model.Recipients,
    Body=model.Body,
    Subject=model.Body
    };
    var result=await _emailSender.SendEmailAsync(msg);
    if (result)
    {
    ViewBag.Message = "success";
    }
    else
    {
    ViewBag.Message = "fail";
    }
    return View();
    }
    }

Leave a Reply
Guest User

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect