2012-05-23 91 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