附件开始一段时间后缺少在电子邮件 [英] Attachment start missing in an Email after a period of time

查看:186
本文介绍了附件开始一段时间后缺少在电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在一封电子邮件附件的问题。每隔几天后,用户没有找到在那里电子邮件预期附件。这似乎是发生了10-20分钟,然后纠正它本身即后来的电子邮件包含附件。我不知道这可能是这背后的原因。这是我的代码看起来像

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. I am not sure what could be the reason behind this. This is how my code looks like

型号

public class EmailAttachment
{
    public string FileName { get; set; }
    public byte[] FileContent { get; set; }
} 



代码触发发送电子邮件

var emailAttachment= new EmailAttachment();
emailAttachment.FileContent = CreatePDFFile();
emailAttachment.FileName = "file.pdf";
EmailGeneratedCertificate(emailAttachment);

电子邮件准备代码

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);
}



另一件事我想指出,我在不同的运行两个网站应用程序池键,都具有相同的电子邮件发送代码上面一样,当这个问题出现,这似乎是在上同时这两个网站的发生10-15分钟,然后更正本身。请建议。

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. Please suggest.

推荐答案

在你的问题,你不写的所有代码CreatePDFFile()恕我直言,这是奇怪的行为的原因,所以我只能从您发布的代码猜。

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.

我看到两个主要问题:


  1. 私人字节[] ReadFile的(字符串路径):你在吞咽任何异常,如果有一些它返回一个空字节数组所以没有附件

  2. <$ C。 $ C>的MemoryStream 在 EmailGeneratedCertificate(EmailAttachment文件):你是不是处理流,这可能情况下,一些意外行为

  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.
  2. MemoryStream in EmailGeneratedCertificate(EmailAttachment file): you are not disposing the stream and this could case some unexpected behavior

这篇关于附件开始一段时间后缺少在电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆