locked
How do I generate an email with an Excel attachment? RRS feed

  • Question

  • User743508062 posted

    Hi,

     I am able to generate Excel files using Mike's export samples, however I have not found anything regarding attaching that Excel file to an email and sending ito a pre-determined destination such as the helpdesk.

    I would be grateful for assistance in this matter, a tutorial would be great.

    Thank you.

    Tuesday, July 30, 2013 10:04 AM

Answers

  • User-1980594115 posted

    Here are a couple of examples using filesToAttach:

    @*
    Example1:
    			var file = Server.MapPath("~/path to Pdf file"); 
    			WebMail.Send( 
    				to: "target-email-address-here", 
    				subject: "some subject", 
    				body: "some text", 
    				filesToAttach: new[]{file} 
    			);
    *@
    
    @{ 
    Example2:
        var customerName = Request["customerName"]; 
        var subjectLine = Request["subjectLine"]; 
        var fileAttachment = Request["fileAttachment"]; 
      
        try { 
            // Initialize WebMail helper 
            WebMail.SmtpServer = "your-SMTP-host"; 
            WebMail.SmtpPort = 25; 
            WebMail.EnableSsl = true; 
            WebMail.UserName = "your-user-name-here"; 
            WebMail.From = "your-email-address-here"; 
            WebMail.Password = "your-account-password"; 
              
            // Create array containing file name 
            var filesList = new string [] { fileAttachment }; 
              
            // Attach file and send email 
            WebMail.Send(to: "target-email-address-here",  
                subject: subjectLine, 
                body: "File attached. <br />From: " + customerName, 
                filesToAttach: filesList); 
        } 
        catch (Exception ex) { 
            <text> 
                <b>The email was <em>not</em> sent.</b> 
                The code in the ProcessFile page must provide an 
                SMTP server name, a user name, a password, and 
                a "from" address.  
            </text>
        } 
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 30, 2013 10:45 AM

All replies

  • User-265225665 posted

    Here you will find good example for attachments.

    http://www.codeproject.com/Articles/10828/Sending-Email-with-attachment-in-ASP-NET-using-SMT

    and here is the example to attach from the directory to attach files from where you saved your files.

    http://stackoverflow.com/questions/8173410/attach-word-document-from-folder-c-sharp-mail-message

    Tuesday, July 30, 2013 10:05 AM
  • User-1980594115 posted

    Here are a couple of examples using filesToAttach:

    @*
    Example1:
    			var file = Server.MapPath("~/path to Pdf file"); 
    			WebMail.Send( 
    				to: "target-email-address-here", 
    				subject: "some subject", 
    				body: "some text", 
    				filesToAttach: new[]{file} 
    			);
    *@
    
    @{ 
    Example2:
        var customerName = Request["customerName"]; 
        var subjectLine = Request["subjectLine"]; 
        var fileAttachment = Request["fileAttachment"]; 
      
        try { 
            // Initialize WebMail helper 
            WebMail.SmtpServer = "your-SMTP-host"; 
            WebMail.SmtpPort = 25; 
            WebMail.EnableSsl = true; 
            WebMail.UserName = "your-user-name-here"; 
            WebMail.From = "your-email-address-here"; 
            WebMail.Password = "your-account-password"; 
              
            // Create array containing file name 
            var filesList = new string [] { fileAttachment }; 
              
            // Attach file and send email 
            WebMail.Send(to: "target-email-address-here",  
                subject: subjectLine, 
                body: "File attached. <br />From: " + customerName, 
                filesToAttach: filesList); 
        } 
        catch (Exception ex) { 
            <text> 
                <b>The email was <em>not</em> sent.</b> 
                The code in the ProcessFile page must provide an 
                SMTP server name, a user name, a password, and 
                a "from" address.  
            </text>
        } 
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 30, 2013 10:45 AM
  • User743508062 posted

    Hi,

    Thanks for the reply, that is very useful and much appreciated to pointing me to these solutions, now I will start reading and playing with the samples and I will report back later once I start coding and most likely, ahem, falling over... Laughing

     

    Tuesday, July 30, 2013 11:04 AM