CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Skip Navigation LinksHome » Article » ASP.Net Article » Send Email with Attachment directly from FileUpload control in ASP.Net   You are not logged in.
Search
 

Technologies
 

Sponsors
 

CodeDigest Navigation
 

Technology News
 

Community News
No News Feeds available at this time.
 
Send Email with Attachment directly from FileUpload control in ASP.Net

By Bala Murugan
Posted On Dec 08, 2010
Article Rating: (Login)
Be first to rate
this article.
No of Comments: 0
Print this article.
Category: ASP.Net

Subscribe to our feed!

Send Email with Attachment directly from FileUpload control in ASP.Net

Sending email is one of the most repeated tasks we do in any asp.net website we develop. At times, we may need to allow users to attach some files when sending the email in our applications. Normally, we will use the FileUpload control available in the asp.net control set to upload or attach files to the server. Moving forward, this little article will help us to attach the files directly from the FileUpload control without saving to a temporary location and send it as an attachment in the email.

 

Steps

1.      In your Visual Studio 2008, click File > New > WebSite. Select "Asp.Net WebSite" and name it as per your need. In the Language DropDownlist, select C#.

2.      Now, design your email sending form in Default.aspx with a FileUpload control for file attachment.

Refer the below code,

<div>

        To <b>Admin</b> <br>

        From <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>

        <br />

        Subject <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>

        <br />

        Message <asp:TextBox ID="txtBody" runat="server" Height="70px" TextMode="MultiLine"

            Width="190px"></asp:TextBox>  <br />

        Attachment <asp:FileUpload ID="fuAttachment" runat="server" />

        <br />

        <asp:Button ID="btnSend" runat="server" Text="Send" onclick="btnSend_Click" />

        <br />   

    </div>

 

The above code will help you to compose the email content with attachment.

txtFrom – From Email ID.

txtSubject – Subject of your email.

fuAttachment – file Upload control to attach files.

btnSend – On clicking this button, you will be able to send the email.

 

3.      In the Send button click, we can pick up the attachment and the email content dynamically to send as an email.

Refer the below code,

protected void btnSend_Click(object sender, EventArgs e)

    {

        MailMessage mail = new MailMessage();

        try

        {

            mail.To.Add("admin@yourdomain.com");

            mail.From = new MailAddress(txtFrom.Text);

            mail.Subject = txtSubject.Text;

            string Body = txtBody.Text;

            mail.Body = Body;

            mail.Attachments.Add(new Attachment(fuAttachment.FileContent, System.IO.Path.GetFileName(fuAttachment.FileName)));

            SmtpClient smtp = new SmtpClient();

            smtp.Host = ConfigurationManager.AppSettings["SMTP"];

            smtp.Send(mail);

        }

        catch (Exception ex)

        {

            Response.Write(ex.Message);

        }

        finally

        {

            mail.Dispose();

        }

    }

 

The above code will pick your attachment directly from the FileUpload control without saving it to any temporary location. Thanks to the Attachment class which made this to happen with very less effort.

4.      Now, configure your SMTP server in the AppSettings of your Web.config file for sending the email.



ASP.Net Hosting

Recent Articles

Refer below,

<appSettings>

      <add key="SMTP" value="localhost"/>    

    </appSettings>

 

In my case, I have used localhost; it may be a different server in your case. Without this configuration, you may not be able to send the email in your application.

 

5.      That’s it! Execute the application and see it in action.

 

Conclusion

The introduction Attachment class in .Netframework 2.0 made sending attachment more secure and flexible by providing a way to attach the file directly without saving it to a temporary location. Since, it is an example; I have not done any validation on the user input (email id, content and files). You have to make sure there is a proper validation is in place in order to stay secured! You can read more on sending emails in asp.net from here.

 

Happy Mailing!!

 

Similar Articles
  • You can contribute to CodeDigest.Com:
    Article Feedback
    Title  
    Submitted By  
    Comment  
    Enter the verification number
     
    Comments