C#C
C#3y ago
2 replies
Sandesh Dhakal

❔ File Attachment In mail. Using a Model object.

Here is the code for the mail configuration I am using. Also the Service action that I am using to send the mail. I need help with sending atttachment and saving the attachment file to the local directory. I am using .Net 6 Web Api.

Service=>
This is the action I am using to send the email , I need to send attachments as well.
public bool Send <T>(PSendEmail model)
{

Message message = new Message(new string[] { model.send_email_to }, model.issue_name, model.description, null, null);
_emailService.SendEmailAsync(message);
return true;
}

Here is the configuration for sending the email.
public class Message
{
public List<MailboxAddress> To { get; set; }
public List<MailboxAddress> Cc { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public IFormFileCollection Attachments { get; set; }
public Message(IEnumerable<string> to, string subject, string content, IFormFileCollection attachments, IEnumerable<string> cc)
{
To = new List<MailboxAddress>();
To.AddRange(to.Select(x => new MailboxAddress(x, x)));
Cc = new List<MailboxAddress>();
Cc.AddRange(cc != null ? cc.Select(x => new MailboxAddress(x, x)) : new List<MailboxAddress>());
Subject = subject;
Content = content;
Attachments = attachments;
}
}

Help please!!
image.png
Was this page helpful?