Reputation: 75
public byte[] Attachment { get; set; }
is attachment which I want to add in
draft save. Anyone has the idea how to save byte[]
in a draft attachment?
public void DraftMessage(string strto, string strcc, string strBcc,
string strSubject, string strBody, List<UserAttachment> listAttachments)
{
try
{
MimeMessage email = new MimeMessage();
email.MessageId = MimeUtils.GenerateMessageId();
var list = new InternetAddressList();
if (strto != "")
{
string[] strArrayto = strto.Split(';');
if (strArrayto != null)
{
list = new InternetAddressList();
foreach (string _strTo in strArrayto)
list.Add(new MailboxAddress(_strTo));
email.To.AddRange(list);
}
}
if (strcc != "")
{
string[] strArraycc = strcc.Split(';');
if (strArraycc != null)
{
list = new InternetAddressList();
foreach (string _strcc in strArraycc)
list.Add(new MailboxAddress(_strcc));
email.Cc.AddRange(list);
}
}
if (strBcc != "")
{
string[] strArrayBcc = strBcc.Split(';');
if (strArrayBcc != null)
{
list = new InternetAddressList();
foreach (string _strBcc in strArrayBcc)
list.Add(new MailboxAddress(_strBcc));
email.Bcc.AddRange(list);
}
}
email.Subject = strSubject;
email.Body = new TextPart(TextFormat.Html)
{
Text = strBody
};
SaveMessgeSummary(email, strDraftfolder, listAttachments);
var draftFolder = MailManager.Instance.ImapClient.GetFolder(strDraftfolder);
if (draftFolder != null)
{
draftFolder.Open(FolderAccess.ReadWrite);
draftFolder.Append(email, MessageFlags.Draft);
draftFolder.Expunge();
}
DAL.MessageSummary.UpdateExecutStatus(email.MessageId);
}
catch (Exception ex)
{
}
}
Above code I am passing parameter List<UserAttachment> listAttachments
which is using below class:
public class UserAttachment
{
public byte[] Attachment { get; set; }
public string strFileName { get; set; }
}
Upvotes: 1
Views: 4694
Reputation: 141
If you want to send an attachment with your e-mail, Visit https://learn.microsoft.com/en-us/answers/questions/590638/net-core-5-web-api-email-attachment-using-mimekit I have implemented it easly.
Upvotes: 0
Reputation: 169280
The FAQ explains how to create an attachment:
var attachment = new MimePart ("image", "gif") {
Content = new MimeContent (File.OpenRead (path), ContentEncoding.Default),
ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = Path.GetFileName (path)
};
The MimeContent
type accepts a Stream
and you can convert a byte[]
to a Stream
by creating a MemoryStream
:
Content = new MimeContent (new MemoryStream(byteArray), ContentEncoding.Default),
Upvotes: 5