2012-05-23 95 views
0

我是SVG对象转换为字节[]和在队列中与下面的类检索存储的电子邮件附件作为字节[]从兔MQ不发送电子邮件

public class MailMessage 
{ 
    public string AttachmentName { get; set; }  
    public byte[] Attachment { get; set; } 
} 

存储该但是电子邮件永远不会被发送。我在考虑附件是损坏的,因为如果我们跳过将附件添加到电子邮件,那么它会发送罚款。

using (var stream = new MemoryStream(attachment)) 
{ 
    var mailMessage = new MailMessage(this.from, new MailAddress(recipient)) { Subject = subject, Body = message }; 

    // this is the line which if commented out allows the email to be sent 
    mailMessage.Attachments.Add(new Attachment(stream, filename)); 

    MailSender().SendAsync(mailMessage, null); 
} 

甲同事提出,字节[]可能已被破坏,由于方式兔存储消息,并因此Base64编码,字节[]使用存放前的内置函数

Convert.ToBase64String(bytes) 

Convert.FromBase64String(message.Attachment) // to retrieve 

但是,这也没有奏效。

任何人都可以想到为什么这不能发送和思考任何解决方法。

我在考虑将图像存储在数据库中,并在发送电子邮件后将其删除,但这是最后一个资源。

回答

1

问题出在MailSender.SendAsync()stream对象在发送邮件之前得到处置。

请勿在此处使用“using”。您在调用SendAsync后立即销毁内存流 ,例如,可能在SMTP之前读取它 (因为它是异步)。

要么在回调中销毁您的流。

使用MailSender.Send()代替MailSender.SendAsync()到synchronusly发送。

Refer this for more detail

+0

这太简单了,令人沮丧。感谢您的帮助 – JConstantine