[英]Attachment start missing in an Email after a period of time
I am having issues with the attachment in an email. 我在电子邮件中遇到附件问题。 After every few days, user don't find the expected attachment in there email. 每隔几天,用户就无法在电子邮件中找到预期的附件。 This seems to be happening for around 10-20 mins and then it corrected itself meaning that the later email will contain the attachments. 这似乎发生了大约10-20分钟,然后它纠正了自己意味着后来的电子邮件将包含附件。 I am not sure what could be the reason behind this. 我不确定这背后的原因是什么。 This is how my code looks like 这就是我的代码的样子
Model 模型
public class EmailAttachment
{
public string FileName { get; set; }
public byte[] FileContent { get; set; }
}
Code trigger to send an Email 代码触发器发送电子邮件
var emailAttachment= new EmailAttachment();
emailAttachment.FileContent = CreatePDFFile();
emailAttachment.FileName = "file.pdf";
EmailGeneratedCertificate(emailAttachment);
Email Preparation Code 电子邮件准备代码
public void EmailGeneratedCertificate(EmailAttachment file)
{
//file.FileContent is a byte array
var ms = new MemoryStream(file.FileContent);
ms.Position = 0;
var contentType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
var from = "xx@x.com";
var fromTargetName = "XXX";
var recepient="xx2@x.com"
var subject = "Attachment";
var body="<strong>Please find attachment.</strong>"
var attachment = new Attachment(ms, contentType);
attachment.ContentDisposition.FileName = file.FileName;
var attachments = new List<Attachment>();
attachments.Add(attachment);
_mailService.Send(recepient, null, subject, body, attachments);
}
Another thing I wanted to point out, I have two websites running within a different APP POOL
and both have the same email sending code like above and when this issue occur, it seems to be happening on both websites at same time for 10-15 mins and then corrected itself. 我想指出的另一件事是,我有两个网站在不同的APP POOL
运行,并且都有相同的电子邮件发送代码,如上所述,当这个问题发生时,它似乎同时发生在两个网站上10-15分钟然后纠正自己。 Please suggest. 请建议。
In your question you don't write all the code of CreatePDFFile()
that IMHO is the cause of the strange behavior so I can only guess from the code you post. 在你的问题中,你没有编写CreatePDFFile()
所有代码,恕我直言,这是奇怪的行为的原因,所以我只能从你发布的代码猜测。
I see 2 main problem: 我看到2个主要问题:
private byte[] ReadFile(string path)
: you are swallowing any exception and if there are some it returns an empty byte array so no attachment. private byte[] ReadFile(string path)
:你吞下任何异常,如果有的话,它返回一个空字节数组,所以没有附件。 MemoryStream
in EmailGeneratedCertificate(EmailAttachment file)
: you are not disposing the stream and this could case some unexpected behavior EmailGeneratedCertificate(EmailAttachment file)
MemoryStream
:您没有处理流,这可能会出现一些意外行为
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.