如何在c#中使用mailkit在草稿中添加byte[]附件
How to add byte[] attachment in draft using mailkit in c#
public byte[] Attachment { get; set; }
是我要添加的附件
草稿保存。有人知道如何将 byte[]
保存在草稿附件中吗?
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)
{
}
}
上面的代码我正在传递参数 List<UserAttachment> listAttachments
,它在下面使用 class:
public class UserAttachment
{
public byte[] Attachment { get; set; }
public string strFileName { get; set; }
}
FAQ 解释了如何创建附件:
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)
};
MimeContent
类型接受 Stream
,您可以通过创建 MemoryStream
:
将 byte[]
转换为 Stream
Content = new MimeContent (new MemoryStream(byteArray), ContentEncoding.Default),
public byte[] Attachment { get; set; }
是我要添加的附件
草稿保存。有人知道如何将 byte[]
保存在草稿附件中吗?
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)
{
}
}
上面的代码我正在传递参数 List<UserAttachment> listAttachments
,它在下面使用 class:
public class UserAttachment
{
public byte[] Attachment { get; set; }
public string strFileName { get; set; }
}
FAQ 解释了如何创建附件:
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)
};
MimeContent
类型接受 Stream
,您可以通过创建 MemoryStream
:
byte[]
转换为 Stream
Content = new MimeContent (new MemoryStream(byteArray), ContentEncoding.Default),