简体   繁体   English

一段时间后,电子邮件中的附件开始丢失

[英]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个主要问题:

  1. 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) :你吞下任何异常,如果有的话,它返回一个空字节数组,所以没有附件。
  2. 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.

相关问题 如何在一段时间内启动线程 - how to start a thread for a period of time Mimekit:签名的电子邮件附件缺少邮箱地址 - Mimekit: MailboxAddress missing for signed email attachment 一段时间后执行应用程序 - Executing an application after a period of time 发送带附件的电子邮件后正在使用的文件 - file in use after sending email with attachment 向电子邮件添加参考附件会响应缺少的 SourceUrl 参数 - Adding reference attachment to email responds missing SourceUrl parameter 通过 MailKit 发送电子邮件失败 - 连接尝试失败,因为连接方在一段时间后没有正确响应 - Sending of email via MailKit fails - A connection attempt failed because the connected party did not properly respond after a period of time 尝试使用 MailKit 发送时出错 Email “连接尝试失败,因为连接方在一段时间后没有正确响应” - Error trying to use MailKit to send Email "A connection attempt failed because the connected party did not properly respond after a period of time" 在一段时间后强制终止外部进程 - Force external process to be killed after time period 如何在一段时间后取消异步任务 - How to cancel async Task after a period of time 一定时间后停止计时器 - Stopping timer after a certain time period
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM