将Bot框架附件转换为SMTP附件

时间:2017-02-01 15:41:33

标签: c# smtp email-attachments botframework

我需要发送附件用户通过smtp服务器从bot框架模拟器发送到邮件ID。我成功地从用户检索附件,但相同的附件没有通过smtp邮件传输。 从机器人收到的附件时间是Microsoft.Bot类型。 Connector.Attachments但smtp附件需要在System.Net.Mail.Attachment类型中。 我尝试过以下链接的答案: - Ability to receive files with the MS bot framework
但这并没有完全解决我的问题。 我已尝试使用Bot附件的名称值进行转换。

 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 个答案:

答案 0 :(得分:0)

在smtp邮件表单中的附件是在位置搜索的C:\ Program Files(x86)\ IIS Express \&#39;,但是在那里找不到该文件,我猜这个附件没有永久保存。为此,我首先使用WebClient Class从bot连接下载附件,如下所示:

 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);
       }

和宾果!邮件与附件一起成功发送。