2016-06-22 42 views
0

我試圖構建一個Web服務來從外部獲取郵件服務器的附件並從那裏發送它,但是我得到下面錯誤:要成爲可序列化的XML,從IEnumerable繼承的類型必須具有Add(System.Object)的實現

To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. System.Collections.Specialized.StringDictionary does not implement Add(System.Object). [InvalidOperationException: There was an error reflecting type 'System.Net.Mail.Attachment'.]

我的代碼是象下面這樣:

[WebMethod] 
    public String SendMailWithAttachment(string mail_sender, string[] mail_receiver, string mail_subject, string mail_text, Attachment att) 
... 

回答

0

System.Net.Mail.Attachment的目的不是要被序列化爲XML。您需要發送構建附件所需的數據,並將其用於Web方法的主體中。例如,發送帶有數據和字符串的字節數組以及附件的名稱:

[WebMethod] 
public String SendMailWithAttachment(string mail_sender, string[] mail_receiver, string mail_subject, string mail_text, byte[] attachment_data, string attachment_name){ 
    using(var ms = new MemoryStream(attachment_data)){ 
     var attachment = new System.Net.Mail.Attachment(ms,attachment_name); 
     ... 
    } 
} 
+0

非常感謝,這工作得很好。 –

相關問題