2011-04-18 152 views
1

我需要能夠從silverlight客戶端應用程序發送電子郵件。 我已經通過實現應用程序使用的web服務來實現這個工作。 問題是,現在我需要能夠將附件添加到正在發送的電子郵件中。是否可以在Silverlight電子郵件上創建電子郵件附件?

我已閱讀各種帖子,試了十幾遍自己弄明白,但沒有成功。

所以現在我發現自己想知道這是否可能?

主要問題是附件的集合需要可序列化。所以,通過這樣做,ObservableCollection - 類型(FileInfo)不工作,ObservableCollection - 類型(對象)不工作...我試過使用List - 類型(Stream),它序列化,但然後我做不知道如何在web服務端創建文件,因爲流對象沒有名稱(這是我試圖分配給Attachment對象的第一件事,然後將它添加到message.attachments中)...我在這裏陷入了一種r stuck。

任何人都可以對此有所瞭解嗎?

回答

1

我想出瞭如何做到這一點,並沒有像它出現那麼困難。 建立在你的web服務,命名空間中的以下內容: `

[Serializable] 
public class MyAttachment 
{ 
    [DataMember] 
    public string Name { get; set; } 

    [DataMember] 
    public byte[] Bytes { get; set; } 
}` 

然後將以下添加到您的網絡的方法參數: MyAttachment[] attachment

添加以下在你的web-方法的執行塊:`

  foreach (var item in attachment) 
      { 
       Stream attachmentStream = new MemoryStream(item.Bytes); 
       Attachment at = new Attachment(attachmentStream, item.Name); 

       msg.Attachments.Add(at); 
      }` 

創建以下屬性(或類似的東西),在您的客戶端: `

private ObservableCollection<ServiceProxy.MyAttachment> _attachmentCollection; 
    public ObservableCollection<ServiceProxy.MyAttachment> AttachmentCollection 
    { 
     get { return _attachmentCollection; } 
     set { _attachmentCollection = value; NotifyOfPropertyChange(() => AttachmentCollection); } 
    }` 

在構造函數中新建公共屬性(AttachmentCollection)。 添加以下在您打開文件對話框應該返回文件:`

如果,你打電話給你的網絡的方法來發送電子郵件(openFileDialog.File!= NULL)

 { 
      foreach (FileInfo fi in openFileDialog.Files) 
      { 
       var tempItem = new ServiceProxy.MyAttachment(); 
       tempItem.Name = fi.Name; 

       var source = fi.OpenRead(); 
       byte[] byteArray = new byte[source.Length]; 
       fi.OpenRead().Read(byteArray, 0, (int)source.Length); 
       tempItem.Bytes = byteArray; 
       source.Close(); 

       AttachmentCollection.Add(tempItem); 
      } 

     }` 

於是最後,添加以下(或類似的東西):

MailSvr.SendMailAsync(FromAddress, ToAddress, Subject, MessageBody, AttachmentCollection);

這個工作對我來說,附件與郵件發送,所有數據完全一樣的原始文件。

+0

這工作,但我切換到「lesnikowski」來處理我的郵件,不得不承認的人,mail.dll運行良好,而且很容易實現。 – Deceased 2011-08-01 07:08:15