Database String Values as Email Attachments with SmtpMailHelper RRS feed

  • Question

  • To whom it may concern,


    1. I make use of C# and LightSwitch in Visual Studio 2012

    2. I store Templates in a table and replace placeholders with actual data from the DB, this is then saved back to the database as part of the "communication" history per client.  I then need to attach these "Files" to an Email.  The data is stored as RTF in a Column (String) in my CommunicationAttachment Table.

    3. I make use of the following syntax to successfully send the mail:

    SmtpMailHelper.SendMail(entity.SenderMail, entity.RecipientMail, entity.Subject, entity.Body, null, null);

    The last 2 null parameters is basically the following:

            byte[] attachment,
            string filename)

    I therefore need to run through my CommunicationAttachment Table and generate the above for all the records applicable to the Communication.

    My code to read through the applicable attachment table is as follows:

    IDataServiceQueryable<CRM_CommunicationAttachment> query; query = from myAttachment in this.DataWorkspace.ProDataData.CRM_CommunicationAttachments where myAttachment.CRM_Communication1.CommunicationID == entity.CommunicationID select myAttachment; foreach (CRM_CommunicationAttachment detail in query) {

    //I need to get each detail.AttachmentName and

    //detail.Attachment(this is the file Content) into the parameters mentioned above


    The Code above forms part of:

    partial void CRM_Communications_Inserted(CRM_Communication entity)

    Any assistance will be appreciated.

    Kind Regards,

    Sunday, December 30, 2012 3:07 PM


  • It does not look like the method that you are using supports multiple attachments/filenames. You need an implementation that supports multiple attachements. What is the source of your email helper? If you have the source code for SmtpMailHelper then perhaps enhance it.

    Here is an implementation that supports multiple file attachments:

    A class for sending emails with attachments in C#

    There are others available.

    Dave Baker | Xpert360 blog | twitter : @xpert360 Opinions are my own. Please mark as answer if this helps solve your problem.

    • Edited by Xpert360 Monday, December 31, 2012 10:40 AM
    • Marked as answer by SpoedNick Monday, December 31, 2012 11:14 AM
    • Edited by Yann DuranModerator Monday, December 31, 2012 11:40 AM Fixed link text
    Monday, December 31, 2012 10:09 AM