【问题标题】:.NET Core - Attachment causing Failure Sending Mail.NET Core - 附件导致发送邮件失败
【发布时间】:2018-11-08 17:04:32
【问题描述】:

我有以下代码,当我没有 attachment.add 行时可以使用。 (我已将地址和密码设为空白)。

请帮忙,我想我的附件有问题我只是不确定是什么!

内部异常是

{System.ObjectDisposedException:无法访问已处置的对象。 对象名称:'ReferenceReadStream'。在 System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) 在 System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) 在 System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult 结果)}

    [HttpPost]
    public async Task<String> PostProfilePicture(IFormFile file, int ID)
    {
        var name = Guid.NewGuid().ToString("N").ToUpper() + ".png";
        try
        {
            var stream = file.OpenReadStream();
            await sendEmail(stream, name);
        }
        catch (Exception ex)
        {
            return ex.Message.ToString();
        }

       return ""
    }

  public async Task sendEmail(Stream stream, String filename){
        var attachment = new Attachment(stream, filename);

        var smtpClient = new SmtpClient

        {
            Host = "smtp.gmail.com", // set your SMTP server name here
            Port = 587, // Port 
            EnableSsl = true,
            Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxxxx")
        };

        var message = new MailMessage("xxxxxxx@gmail.com", "xxxxxxx@gmail.com");


            message.Subject = "Hello Alec!!";
            message.Body = "How are you doing.";
            message.Attachments.Add(attachment);




            await smtpClient.SendMailAsync(message);
        }
    }

【问题讨论】:

  • 错误信息在标题中,“发送邮件失败”
  • 糟糕。 {System.ObjectDisposedException:无法访问已处置的对象。对象名称:'ReferenceReadStream'。在 System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) 在 System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) 在 System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult 结果)}

标签: c# .net-core smtp


【解决方案1】:

我认为流是没有读到结束的:

试试:

    var fileContent = stream.ReadToEnd();
    var attachment = new Attachment(fileContent, filename);

    var smtpClient = new SmtpClient

也许您必须在调用 SendEmail 之前阅读文件内容

【讨论】:

    【解决方案2】:

    我认为流需要可读,添加

    [HttpPost]
    public async Task<String> PostProfilePicture(IFormFile file, int ID)
    {
        var name = Guid.NewGuid().ToString("N").ToUpper() + ".png";
        try
        {
            await sendEmail(file, name); //send the file not the open read stream
        }
        catch (Exception ex)
        {
            return ex.Message.ToString();
        }
    
       return "";
    }
    
    
    
    public async Task sendEmail(IFormFile file, String filename){
    
            using(var stream = file.OpenReadStream()){ //You open your stream here
            var attachment = new Attachment(stream, filename);
    
            var smtpClient = new SmtpClient
    
            {
                Host = "smtp.gmail.com", // set your SMTP server name here
                Port = 587, // Port 
                EnableSsl = true,
                Credentials = new NetworkCredential("xxxxxxx@gmail.com", "xxxxxxxx")
        };
    
        var message = new MailMessage("xxxxxxx@gmail.com", "xxxxxxx@gmail.com");
    
    
            message.Subject = "Hello Alec!!";
            message.Body = "How are you doing.";
            message.Attachments.Add(attachment);
    
            await smtpClient.SendMailAsync(message);
        }
    }
        }
    

    【讨论】:

    • 我的文件来自 IFormFile,不知道如何实现,因为我没有文件的物理副本。
    • 您遇到的异常 ObjectDisposedException 与 OpenReadStream() 处置的对象有关。您应该将 IFormFile 发送到您的 sendEmail() 方法而不是流,然后在 SendEmail 方法中打开readstream。
    猜你喜欢
    • 1970-01-01
    • 2016-08-07
    • 2012-08-21
    • 1970-01-01
    • 2013-08-26
    • 2016-04-11
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多