gpt4 book ai didi

c# - 如何在c#中使用mailkit在草稿中添加byte[]附件

转载 作者:太空宇宙 更新时间:2023-11-03 20:55:12 32 4
gpt4 key购买 nike

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这是使用下面的类:

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:

Content = new MimeContent (new MemoryStream(byteArray), ContentEncoding.Default),

关于c# - 如何在c#中使用mailkit在草稿中添加byte[]附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51260805/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com