考虑这段代码
private MailMessage GetMailMessageFromMailItem(Data.SystemX.MailItem mailItem)
{
var msg = new MailMessage();
foreach (var recipient in mailItem.MailRecipients)
{
var recipientX = Membership.GetUser(recipient.UserKey);
if (recipientX == null)
{
continue;
}
msg.To.Add(new MailAddress(recipientX.Email, recipientX.UserName));
}
msg.From = new MailAddress(ConfigurationManager.AppSettings["EmailSender"],
ConfigurationManager.AppSettings["EmailSenderName"]);
msg.Subject = sender.UserName;
if (!string.IsNullOrEmpty(alias)) msg.Subject += "(" + alias + ")";
msg.Subject += " " + mailItem.Subject;
msg.Body = mailItem.Body;
msg.Body += Environment.NewLine + Environment.NewLine + "To reply via Web click link below:" + Environment.NewLine;
msg.Body += ConfigurationManager.AppSettings["MailPagePath"] + "?AID=" + ContextManager.AccountId + "&RUN=" + sender.UserName;
if (mailItem.MailAttachments != null)
{
foreach (var attachment in mailItem.MailAttachments)
{
msg.Attachments.Add(new Attachment(new MemoryStream(attachment.Data), attachment.Name));
}
}
return msg;
}
我只是将我的数据库类型转换为 MailMessage。它在另一个函数中发送。
代码分析告诉我我没有处理正确的“msg”。但是,如果我在这里执行此操作-尝试发送时会出现异常。
此外,它抱怨没有在这里处理 MemoryStream :
msg.Attachments.Add(new Attachment(new MemoryStream(attachment.Data), attachment.Name));
我不知道如何正确处理它。我尝试了不同的方法,但在发送邮件说“流已关闭”时出现异常