How To Send Email With Attachment in ASP.Net Web Pages 2

Introduction

Today we'll learn to send an Email from a website using WebMatrix in ASP.NET Web Pages 2. You can also send an email with an attachment of the file using this article.

Brief

There are many types of scenarios in which you might need to send an email from the website. In that case the WebMail helper is used to send the email to the users. If you are authorized to access the SMTP server then you can use WebMail. The (Simple Mail Transfer Protocol (SMTP) Server is an email server that only forwards messages to the recipient's server; it's the outbound side of email. If you are working on a network, then you can get the SMTP server from the administrator of the IT department. If you are trying to send the email when working at home then you might even be able to test using the ordinary email provider.

There are the following points that are useful:

  • SMTP Server name
  • Port number. Usually the port number is 25 or may be 587. In the case of Secure Socket Layer (SSL) for email,  contact the administrator.
  • User Credentials

So, let's proceed with the following sections:

  • Create an Email Page
  • Working with Email Process
  • Email with File Attachment
  • Working with Email Attachment

Note: Have a look at Create Website in WebMatrix using ASP.NET Web Pages 2.

Create an Email Page

As the caption describes, we'll create a page with which we can send the email. Use the following procedure to do that.

Step 1: Create a new "CSHTML" page named EmailPage.

Step 2: Replace the code with the code below:

  1. @{  
  2.     Layout = "~/_LayoutPage.cshtml";  
  3.     Page.Title = "Emailing";  
  4. }  
  5. <!DOCTYPE html>  
  6. <html lang="en">  
  7.     <head>  
  8.         <meta charset="utf-8" />  
  9.     </head>  
  10.     <body>  
  11.         <form method="post" action="ProcessEmail.cshtml">  
  12.             <table>  
  13.                 <tr>  
  14.                     <td>Your Name:</td>  
  15.                     <td><input type="text" name="Name" /></td>  
  16.                 </tr>  
  17.                 <tr>  
  18.                     <td>To:</td>  
  19.                     <td><input type="text" name="UserEmail" /></td>  
  20.                 </tr>  
  21.                 <tr>  
  22.                     <td>Message:</td>  
  23.                     <td><textarea name="UserMessage" cols="35" rows="4"></textarea></td>  
  24.                 </tr>  
  25.                 <tr>  
  26.                     <td colspan="2"><input type="submit" value="Send" /></td>  
  27.                 </tr>  
  28.             </table>  
  29.         </form>  
  30.     </body>  
  31. </html> 

In the code above, the page layout is set to the Layout page that is used to set the layout of this current page in the code block. The action attribute is used in the form that is set to the ProcessEmail page which means that the form will be submitted to that page instead of back to the current page.

Step 3: Now run the page, it will look like this:

Email Page in WebMatrix

Working with Email Process

In this section, we'll create a ProcessEmail page in which we add the code to send the email. Use the following procedure to do that.

Step 1: Create a new "CSHTML" page named ProcessEmail.

Step 2: Replace the code with the code below:

  1. @{  
  2.    Layout = "~/_LayoutPage.cshtml";   
  3.    Page.Title = "Emailing";  
  4.    var Name = Request["Name"];  
  5.    var UserEmail = Request["UserEmail"];  
  6.    var UserMessage = Request["UserMessage"];  
  7.    var errormsg="";  
  8.    var debuggingflag=false;  
  9.    try{  
  10.        WebMail.SmtpServer = "Your-smtp-server-name";  
  11.        WebMail.SmtpPort= 25;  
  12.        WebMail.EnableSsl = true;  
  13.        WebMail.UserName = "Your-UserName";  
  14.        WebMail.Password = "Your-Password";  
  15.        WebMail.From = "Your-Email-Address";  
  16.        WebMail.Send(to: UserEmail,  
  17.            subject: "Hello: "+ Name,  
  18.            body: UserMessage  
  19.        );  
  20.    }  
  21.    catch(Exception ex){  
  22.        errormsg = ex.Message;  
  23.    }   
  24. }  
  25. <!DOCTYPE html>  
  26. <html lang="en">  
  27.    <head>  
  28.        <meta charset="utf-8" />  
  29.    </head>  
  30.    <body>  
  31.        <p>Dear <b>@Name</b>.</p>  
  32.    @if(errormsg == ""){  
  33.      <p>An email message has been sent to @UserEmail. Please check your email. It has the following message:</p>        
  34.      <p>@UserMessage</p>  
  35.    }  
  36.    else{  
  37.        <p><b>The email was <em>not</em> sent.</b></p>  
  38.        <p>Please check that the code in the ProcessEmail page has   
  39.           correct settings for the SMTP server name, a user name,   
  40.           a password, and a "from" address.  
  41.        </p>  
  42.        if(debuggingflag){  
  43.            <p>The following error was occured:</p>  
  44.            <p><em>@errormsg</em></p>  
  45.        }  
  46.    }  
  47.    </body>  
  48. </html> 

In the code above, the WebMail helper is used to create and send the email to the user. The try/catch block is used here in case of any exception. If any exception occurs then the catch block runs and sets the errormsg variable to the error that has occurred.

Note: You need to modify the following email related settings:

  • Add the SMTP-Server-Name like "smtp.gmail.com".
  • Add the user-name to the name for your SMTP Server account.
  • Add the password of your SMTP Server account.
  • Add the email-address with which the email is sent from.

Step 3: Run the Email Page again and enter the details.

Email Page with Details in WebMatrix

You can see in the following screenshot that the mail has been sent and the next page opens with the confirmation and email message.

Email Message in WebMatrix

Email with File Attachment

Now in this section we'll send the email with the attachment of the text file. Use the following procedure to do that.

Step 1: Add the Text file to the website named SampleFile.

Adding Text File in WebMatrix

Step 2: Add the following text in that file:

Hello this is the sample file that is used to attach with the email. Thanks.

Note: You can add your own text also.

Step 3: Add the new CSHTML page named EmailWithAttachment.

Step 4: Replace the code with the code below:

  1. @{  
  2.   Layout = "~/_LayoutPage.cshtml";   
  3.   Page.Title = "Emailing with File Attachment";  
  4. }  
  5. <!DOCTYPE html>  
  6. <html lang="en">  
  7.    <head>  
  8.        <meta charset="utf-8" />  
  9.    </head>  
  10.    <body>  
  11.        <form method="post" action="EmailProcessAttachment.cshtml">  
  12.            <table>  
  13.                <tr>  
  14.                    <td>Your Name:</td>  
  15.                    <td><input type="text" name="UserName" /></td>  
  16.                </tr>  
  17.                <tr>  
  18.                    <td>To:</td>  
  19.                    <td><input type="text" name="UserEmail" /></td>  
  20.                </tr>  
  21.                <tr>  
  22.                    <td>Subject:</td>  
  23.                    <td><input type="text" size="20" name="EmailSubject" /></td>  
  24.                </tr>  
  25.                <tr>  
  26.                    <td>File to Attach:</td>  
  27.                    <td><input type="text" name="AttachedFile" size="20" /></td>  
  28.                </tr>  
  29.                <tr>  
  30.                    <td colspan="2"><input type="submit" value="Send" /></td>  
  31.                </tr>  
  32.            </table>  
  33.        </form>  
  34.    </body>  
  35. </html> 

Step 5: Run the page and it'll look like this:

Email Page with Attachment in WebMatrix

Working with Email Attachment

In this section we'll create the page in which we add the code to send the email with the file attachment. Use the following procedure to do that.

Step 1: Create another CSHTML page named EmailProcessAttachment.

Step 2: Replace the code with the code below:

  1. @{  
  2.   Layout = "~/_LayoutPage.cshtml";   
  3.   Page.Title = "Emailing with File Attachment";  
  4.   var Name = Request["UserName"];  
  5.   var Email = Request["UserEmail"];  
  6.   var EmailSubject = Request["EmailSubject"];  
  7.   var AttachedFile = Request["AttachedFile"];  
  8.   var errormsg = "";  
  9.   var debuggingflag = false;  
  10.   try{  
  11.       WebMail.SmtpServer = "Your-smtp-server-name";  
  12.       WebMail.SmtpPort= 25;  
  13.       WebMail.EnableSsl = true;  
  14.       WebMail.UserName = "Your-User-Name";  
  15.       WebMail.Password = "Your-Password";  
  16.       WebMail.From = "Your-Email-Address";  
  17.       var attachfile = new string[] {AttachedFile};  
  18.       WebMail.Send(to: Email,  
  19.           subject: EmailSubject,  
  20.           body: "The File is attached from:" + Name,  
  21.           filesToAttach: attachfile  
  22.       );  
  23.    }  
  24.    catch(Exception ex){  
  25.       errormsg = ex.Message;  
  26.    }    
  27. }  
  28. <!DOCTYPE html>  
  29. <html lang="en">  
  30.    <head>  
  31.        <meta charset="utf-8" />  
  32.    </head>  
  33.    <body>  
  34.        <p>Dear <b>@Name</b>.</p>  
  35.    @if(errormsg == ""){  
  36.      <p>An email message has been sent to @Email. Please check your email. It has the following attached file:</p>        
  37.      <p>@AttachedFile</p>    
  38.    }  
  39.    else{  
  40.        <p><b>The email was <em>not</em> sent.</b></p>  
  41.        <p>Please check that the code in the EmailProcessAttachment page has   
  42.           correct settings for the SMTP server name, a user name,   
  43.           a password, and a "from" address.  
  44.        </p>  
  45.        if(debuggingflag){  
  46.            <p>The following error was occurred:</p>  
  47.            <p><em>@errormsg</em></p>  
  48.        }  
  49.    }  
  50.    </body>  
  51. </html> 

Step 3: Run the EmailWithAttachment page and enter the details as shown below:

Attached File Page With Details in WebMatrix

The mail will send successfully and you will receive the next page as shown below:

Email Sent with Attached File

Summary

This article described how to send an email in WebMatrix using WebMail. You can also learn to send the email with a file attachment. Thanks for reading.


Similar Articles