none
Send attachment from infopath form to email - C# RRS feed

  • Question

  • Hello,
    i'm here again.
    Today I try to send an attachment from infopath form to and email.
    Howto ?
    I have found this article, but i'm unsure if I need to include that class.

    What I want is something like:

    XPathNavigator root = MainDataSource.CreateNavigator();
    string attachment = root.SelectSingleNode("/my:mineFelter/my:grp5/my:fileAttachment", NamespaceManager).Value;
    msg.Attachment.Add(attachment);

    But this fails of course, attachment should propably by a file or attachment datatype, but how ?

    Jakob



    Jakob Olsen
    Friday, February 5, 2010 11:07 AM

Answers

All replies

  • Hello again,
    I have solved this one.
    Used the decode code from this site: http://support.microsoft.com/kb/892730
    And then read the "attachment" into a MemoryStream like this:
    http://www.csharp411.com/c-convert-string-to-stream-and-stream-to-string/

    And attached the file like:

    msg.Attachment.Add(memoryStreamAttachment);

    And it just works like a charm.
    Jakob Olsen
    • Marked as answer by Jakobdo Friday, February 5, 2010 3:46 PM
    Friday, February 5, 2010 3:46 PM
  • Hey Jakobdo,

           Can you please give me full code for this. I just want to send a file attachment to any email ID from the info path form. I am using "file Attachment" control of info path. Actually i am a beginner to this technology. So please help me to fix it. I have created the info path form and published on to my desktop and going to outlook File>>New>>Choose InfoPath Form>> and here choosing the form i have created. Actually after i publish my infopath form on to my desktop it is getting populated here, thus i am choosing. As the next step i am filling all the fields on the form and browsing for attachment through "File Attachment" control and clicking on the submit button which i did not create and is already there by default on the top of the form and typing the emailid to send the form.

         After that, the form is reaching to that emailid successfully with all the correct data and we are getting a file attachment to that email which is in the XML format of the infopathform and i found actual attachment's data inside that in some encoded format.

         Do i need to decode that data using object model of the outlook and using the class that you have specified on the MS forum and save as a file again on my system? Anyhow i need to use object model of outlook in my project to read this unread mail's body. If i have just a file attachment to this unread mail i can just download it through code. If not i need to just decode it and save it.

    OR

    there is a way that we can get attachment to the mail directly without encoding?

     

    Please give me a solution asap.

     

    Thanks,

    Naveen.

    Friday, May 21, 2010 1:39 PM
  • Jakobdo, can you help me with this. I just want the same but i don't get it.

    Plz help me.

     

    Monday, May 23, 2011 3:00 PM
  • Hello Rian,

    i added a file attachment field to my form.

    Added this code:

    using Microsoft.Office.InfoPath;
    using System;
    using System.Xml;
    using System.Xml.XPath;
    using System.Net.Mail;
    using System.Web;
    using System.IO;
    using System.Text;

    namespace fejlmelding
    {
        public partial class FormCode
        {
            private const int FIXED_HEADER = 16;

            private int fileSize
            {
                get
                {
                    return (int)FormState["_fileSize"];
                }
                set
                {
                    FormState["_fileSize"] = value;
                }
            }

            private int attachmentNameLength
            {
                get
                {
                    return (int)FormState["_attachmentNameLength"];
                }
                set
                {
                    FormState["_attachmentNameLength"] = value;
                }
            }

            private string attachmentName
            {
                get
                {
                    return (string)FormState["_attachmentName"];
                }
                set
                {
                    FormState["_attachmentName"] = value;
                }
            }

            private byte[] decodedAttachment;

            public void InternalStartup()
            {
                EventManager.FormEvents.Submit += new SubmitEventHandler(FormEvents_Submit);
            }

            /// <summary>
            /// Accepts the Base64 encoded string
            /// that is the attachment.
            /// </summary>
            public void InfoPathAttachmentDecoder(string theBase64EncodedString)
            {
                byte [] theData = Convert.FromBase64String(theBase64EncodedString);
                using(MemoryStream ms = new MemoryStream(theData))
                {
                    BinaryReader theReader = new BinaryReader(ms);           
                    DecodeAttachment(theReader);
                }
            }

            private void DecodeAttachment(BinaryReader theReader)
            {
                //Position the reader to get the file size.
                byte[] headerData = new byte[FIXED_HEADER];
                headerData = theReader.ReadBytes(headerData.Length);

                fileSize = (int)theReader.ReadUInt32();
                attachmentNameLength = (int)theReader.ReadUInt32() * 2;
               
                byte[] fileNameBytes = theReader.ReadBytes(attachmentNameLength);
                //InfoPath uses UTF8 encoding.
                Encoding enc = Encoding.Unicode;
                attachmentName = enc.GetString(fileNameBytes, 0, attachmentNameLength - 2);
                decodedAttachment = theReader.ReadBytes(fileSize);
            }

            public string Filename
            {
                get { return attachmentName; }
            }

            public byte[] DecodedAttachment
            {
                get { return decodedAttachment; }
            }

            public string getInfoPathValue(string field)
            {
                XPathNavigator root = MainDataSource.CreateNavigator();
                return root.SelectSingleNode(field, NamespaceManager).Value;
            }

            public void FormEvents_Submit(object sender, SubmitEventArgs e)
            {
                MailMessage msg = new MailMessage(SENDER, RECIPIENT);

                msg.Subject = subject;
                msg.Body = body;
                msg.IsBodyHtml = false;

                if (attach.Length > 0)
                {
                    InfoPathAttachmentDecoder(attach);
                    MemoryStream myStream = new MemoryStream(decodedAttachment);
                    Attachment myAttach = new Attachment(myStream, attachmentName);
                    msg.Attachments.Add(myAttach);
                }
               

                SmtpClient smtp = new SmtpClient("mx.ringsted.int");
                try
                {
                    smtp.Send(msg);
                    e.CancelableArgs.Cancel = false;
                }
                catch (Exception myEx)
                {
                    e.CancelableArgs.Message = "Fejl : " + myEx.ToString();
                    e.CancelableArgs.Cancel = true;
                }
            }
        }
    }

     

    If you use a webbrowser formula, remember to add the form tag enctype="multipart/form-data"


    Jakob Olsen
    • Proposed as answer by AZandbergen Monday, February 6, 2012 6:31 PM
    Friday, May 27, 2011 7:13 AM
  • Hey Jakob,

    can you help me out with more step by step instructions.. i'm not very familiar with vb/C# codes, so need help figuring this one out. I need to do exactly the same thing you described in the problem.

     

    thanks,

    ankpatel.

    Thursday, June 23, 2011 8:33 PM
  • Hello Ankpatel,

    do you want a working example then ?

     

    Jakob


    Jakob Olsen
    Friday, June 24, 2011 12:00 PM
  • Jakob, excellent work on the coding.  Not to hijack the thread, but where do I place the code and will it work without having to add/remove anything?

    Thanks for your help!

    Wednesday, July 6, 2011 5:00 PM
  • Try this sample: http://www.megaupload.com/?d=2G04M9T0

     

    Remember, this form requires full trust. (signing).

    If used as a webform, you should remeber to add enctype="multipart/form-data" to the form.

    Can be added with jquery, feature or through sharepoint designer.


    Jakob Olsen
    Thursday, July 7, 2011 8:21 AM
  • Hi Jakob,

    I am not able to open your example file, can you please tell me how to open the file. I can not able to open the file with the code its just showing only infopath.

    Thanks!

    Thursday, September 15, 2011 11:08 PM
  • What if you rightclick and then choose design?

    And what happens, if you just open the file?

    Dig you get an error or ?


    Jakob Olsen
    Monday, September 19, 2011 4:00 PM
  • It just opens me the Infopath form and not the code. It shows an error message saying that the file does not exist in the selected path.
    Wednesday, September 28, 2011 2:13 PM
  • The above code may not work if you are decoding the base64 string in a Visual Studio workflow or somewhere outside InfoPath because InfoPath adds header information to the base64, you need to strip that off or you get a corrupt file.  See the following post:

    http://benvanmol.blogspot.com/2007/10/accessing-infopath-file-attachments-in.html

    Thursday, June 21, 2012 8:47 PM
  • There is no declaration of "attach", I am new to infopath so Could you please tell me what type of it is?
    Monday, October 8, 2012 5:30 AM
  • Can you please tell me where to put this code?  I only need to include file attachment with the InfoPath form that is emailed.  Not the just the form but the attachment that has been added.  Words alone would not express how bad I need this solution.
    Thursday, November 8, 2012 3:17 AM
  • Try to download the file attached and see the code.

    The code should be placed into the "submit_form" event.


    Jakob Olsen

    Thursday, November 8, 2012 8:40 AM
  • Thanks Jokabdo.  No file is attached.  Feel free to zip and email the file to me at tricidsyoung@att.net.
    Thursday, November 8, 2012 4:30 PM
  • Kokanutt: See entire code above.

    Jakob Olsen

    Friday, November 9, 2012 9:12 AM
  • I used the code above given by you. But its throwing some error message for attach. I tried to download the form as well but the link is broken. I would appreciate if you still have the form Thanks

    Yaseen


    • Edited by Yaseeno Tuesday, November 19, 2013 5:24 PM personal info
    Wednesday, February 27, 2013 12:59 AM
  • How about this one: Attachment Form

    Remember to change the email, smtp-server.


    Jakob Olsen

    Wednesday, February 27, 2013 7:57 AM
  • Finally I get to Download the file. But I couldnt able to View the code. Also The xsn file you send has an attachment file.

    I actually want the attachment.xsn to be appearing in the email message on click(Submit). Is it possible to do so.

    So that when user on click submit the current form will appear inside email body then user should able to edit the email body to include additional messages.

    Thanks

    Yaseen

    Thursday, February 28, 2013 6:45 PM
  • What if you right click the xsn file and choose design ?

    Jakob Olsen

    Thursday, February 28, 2013 8:48 PM
  • I can get into design mode but how do I see the code its asking for C# files
    Thursday, February 28, 2013 9:54 PM
  • Hi Jakob,

    I used the code above given by you. But its throwing some error message for attach. I tried to download the form as well but the link is broken. I would appreciate your help!


    • Edited by AmparoM Tuesday, August 27, 2013 7:44 PM
    Tuesday, August 27, 2013 7:44 PM
  • Hi, I know it has been a long time since you posted this tread.. However, I would like to ask how did you get the attachments to be send along in the emails? I have tried all the urls you posted above but all don't seams to be working anymore :(

    I am totally new to both SharePoint, Infocomm 2010 (actually just touched one week ago) and all forms of programming languages. I was requested to get this attachment feature up by this Wednesday, so would really appreciate any help given!!! If possible, do allow me to download a copy of your project and I will try to figure out how things work :)

    Thanks!

    Monday, May 25, 2015 1:17 AM
  • Hi Yaseeno,

    Would like to know if you are still keeping a copy of the url files from this tread? I am really desparate to get this attachment thingy done as my deadline is on this coming Wednesday... I have no intention to misuse the files.. Do please help me!!!! Thanks!!!!

    T^T

    -yunzi


    • Proposed as answer by joysofyz Monday, May 25, 2015 2:47 AM
    • Unproposed as answer by joysofyz Monday, May 25, 2015 2:47 AM
    • Edited by joysofyz Monday, May 25, 2015 2:47 AM
    Monday, May 25, 2015 2:46 AM