0
votes

I need to send an attachment user is sending from bot framework emulator to a mail id via smtp server. I am successful at retrieving the attachment from user, but the same attachment is not getting transferred via smtp mail. The time of attachment received from the bot is of type Microsoft.Bot. Connector.Attachments but smtp attachment needs to be in the type System.Net.Mail.Attachment. I have tried the answers from this link :- Ability to receive files with the MS bot framework.
But this did not solve my problem completely. I have tried the conversion by using the Name value of Bot attachment.

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

I passed the attachment and then tried to add the name of attachment on message as below:

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

But I am getting the below error message:

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 ..

Please help.

1
Could not find file ‘C:\Program Files (x86)\IIS Express\colorado-sunrise.jpg’ pretty much says it all.bashis
So do I need to copy the attachments in C:\Program Files (x86)\IIS Express? if yes then how do I copy from bot attachments to any particular location?Khushi4.net
I found the solution now. I actually first had to download the file. @stuartd, please remove the question as duplicate so that I can provide answer.Khushi4.net
@Khushi4.net donestuartd

1 Answers

0
votes

The attachment in smtp mail form was searched in location 'C:\Program Files (x86)\IIS Express\',but the file was not found there, I guess the attachment was not saved permanently. For this, I first downloaded the attachment from the bot connection using WebClient Class as below:

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

Then I passed the name of the attachment while creating a fresh SMTP mail attachment instance.

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

And bingo! the mail is sent successfully with the attachment.