【问题标题】:Convert Bot framework attachment to SMTP attachment将 Bot 框架附件转换为 SMTP 附件
【发布时间】:2017-02-01 15:41:32
【问题描述】:

我需要发送一个附件,用户正在通过 smtp 服务器从 bot 框架模拟器发送到邮件 ID。我成功地从用户那里检索了附件,但没有通过 smtp 邮件传输相同的附件。 从 bot 收到的附件时间是 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 ..

请帮忙。

【问题讨论】:

  • Could not find file ‘C:\Program Files (x86)\IIS Express\colorado-sunrise.jpg’ 几乎说明了一切。
  • 那么我需要复制 C:\Program Files (x86)\IIS Express 中的附件吗?如果是,那么我如何从机器人附件复制到任何特定位置?
  • 我现在找到了解决方案。我实际上首先必须下载文件。 @stuartd,请删除重复的问题,以便我提供答案。
  • @Khushi4.net 完成

标签: c# smtp email-attachments botframework


【解决方案1】:

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

还有宾果游戏!带有附件的邮件发送成功。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多