2017-02-01 66 views
0

我需要發送附件用戶通過smtp服務器從bot框架模擬器發送郵件標識。我成功地從用戶那裏檢索附件,但是沒有通過smtp郵件傳輸相同的附件。 從機器人收到的附件時間是Microsoft.Bot類型。 Connector.Attachments但smtp附件需要在System.Net.Mail.Attachment類型中。 我試過這個鏈接的答案: - Ability to receive files with the MS bot framework
但這並沒有完全解決我的問題。 我已經嘗試使用Bot附件的名稱值進行轉換。將Bot框架附件轉換爲SMTP附件

MailMessage message = new MailMessage(EntityValues.FromEmail, To, Subject, MessageBody); 

      if (attachment != null) 
      { 
      System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(attachment.Name); 
       message.Attachments.Add(data); 


public virtual async Task SaveAttachment(IDialogContext context, IAwaitable<IMessageActivity> result) 

    { 

     var message = await result; 

     if (message.Attachments != null && message.Attachments.Any()) 
     { 
      var attachment = message.Attachments.First(); 


      string sendresult = SendEmail(EntityValues.ToEmail, EntityValues.ProblemHeader, EntityValues.problemStatement,attachment); 

我通過附件,然後嘗試在消息如下添加附件的名稱:

 public string SendEmail(string To, string Subject, string MessageBody, Microsoft.Bot.Connector.Attachment attachment) 

    { 
     try 
     { 

      MailMessage message = new MailMessage(EntityValues.FromEmail, To, Subject, MessageBody); 
      message.IsBodyHtml = true; 
      message.BodyEncoding = Encoding.Default; 
      if (attachment != null) 
      { 
      System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(attachment.Name); 
       message.Attachments.Add(data); 

但我收到以下錯誤信息:

System.IO.FileNotFoundException: Could not find file ‘C:\Program Files (x86)\IIS Express\colorado-sunrise.jpg’ jpg’. File name: ‘C:\Program Files (x86)\IIS Express\colorado-sunrise.jpg’ at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.Net.Mail.AttachmentBase.SetContentFromFile(String fileName, String mediaType) at System.Net.Mail.AttachmentBase..ctor(String fileName) at System.Net.Mail.Attachment..ctor(String fileName) at MyBotApp.StateDialogTest.SendEmail(String To, String Subject, String MessageBody, Attachment attachment) in ..

請幫忙。

+1

'無法找到文件'C:\ Program Files(x86)\ IIS Express \ colorado-sunrise.jpg''幾乎可以說明一切。 – bashis

+0

那麼我需要複製C:\ Program Files(x86)\ IIS Express中的附件嗎?如果是,那麼我如何從機器人附件複製到任何特定位置? –

+0

我現在找到了解決方案。我實際上首先必須下載文件。 @stuartd,請將問題重複刪除,以便我可以提供答案。 –

回答

0

在位置'C:\ Program Files(x86)\ IIS Express \'中搜索到附件中的smtp郵件表單,但該文件未找到,我猜這個附件沒有永久保存。對於這一點,我先下載使用WebClient類從機器人連接附件如下:

string ContentURL = attachment.ContentUrl; 
      string name = attachment.Name; 
      using (var client = new WebClient()) 
      { 
      client.DownloadFile(ContentURL, name); } 

然後,我通過了附件的名稱,同時創建一個新的SMTP郵件附件實例。

public string SendEmail(string To, string Subject, string MessageBody, Microsoft.Bot.Connector.Attachment attachment) 

    { 
     try 
     {HttpClient httpClient = new HttpClient(); 
     MailMessage message = new MailMessage(EntityValues.FromEmail, To, Subject, MessageBody); 
     if (attachment != null) 
     { System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(attachment.Name); 

     message.Attachments.Add(data); 
     } 

和賓果!郵件與附件一起成功發送。