×

Sending Email from IIS using SMTP in ASP.NET

To send email from an Asp.Net application using the System.Net.Mail you must configure Simple Mail Transfer Protocol e-mail. Mail can be delivered immediately, or it can be delivered to a file location on disk where it is configured.

Sending Email from IIS using SMTP in ASP.NET

Prerequisites

  1. Windows server 2008/2012/2014/2016 and above

  2. Visual Studio 2017 Installed on Windows server.

  3. IIS Manager Installed in Windows Server with all the features especially SMTP Server.

  4. A mail account (into which the mail will be pooled having less secure apps turned on and having application password)

Step 1: Create a Windows Server in Google Compute Engine

To create a basic Windows instance:

  1. In the GCP Console, go to the VM Instances page.

GO TO THE VM INSTANCES PAGE

  1. Click Create instance.

  2. In the Boot disk section, click Change to begin configuring your boot disk.

  3. In the OS images tab, choose a Windows image.

  4. Click Select.

  5. Click Create to create the instance.

Enter into the instance with the RDP after setting the password and desired username

Step 2:  Install IIS Manager in Windows Server

To install IIS 8, use the following steps:

  1. Open Server Manager.

  2. Under Manage menu, select Add Roles, and Features

  3. Select Role-based or Feature-based Installation

  4. Select the appropriate server (local is selected by default), as shown below and click next.

  5. Select Web Server (IIS)

  6. No additional features are needed for IIS, so click Next

  7. Click Next

  8. Customize your installation of IIS, or accept the default settings that have already been selected for you, and then click Next(Make sure all, the checkboxes are checked and selected)

  9. Click Install

  10. When the IIS installation completes, the wizard reflects the installation status. Click Close to exit the wizard.

Step 3: Install Microsoft Visual Studio 2017 IDE

Download Link:https://visualstudio.microsoft.com/downloads/

Instruction: Add a ASP.NET plugin during the installation and before starting with the development.


Step 4: Creating a ASP.Net Project

  1. Go to Files > New > Project

  2. Select ASP.NET Web Application having the template selected as C# and Web selected(Name - SendEmail).

  3. Select Empty project

  4. When the project is created you will notice that on the right hand side panel right-click on it and Add New Web Form(Name - index.aspx)

  5. In index.aspx copy the Code:


  1.                    <td>:</td>

  2.                    <td>

  3.                        <asp:TextBox ID="txtTo" runat="server" Width="300"></asp:TextBox>

  4.                    </td>

  5.                </tr>

  6.                <tr>

  7.                    <td>Subject</td>

  8.                    <td>:</td>

  9.                    <td>

  10.                        <asp:TextBox ID="txtSubject" runat="server" Width="300"></asp:TextBox>

  11.                    </td>

  12.                </tr>

  13.                <tr>

  14.                    <td>Message</td>

  15.                    <td>:</td>

  16.                    <td>

  17.                        <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Rows="5" Columns="40"></asp:TextBox>

  18.                    </td>

  19.                </tr>

  20.                <tr><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="SendEmail.index" %>


  21. <!DOCTYPE html>


  22. <html>

  23. <head runat="server">

  24.    <title></title>

  25. </head>

  26. <body>

  27.    <form id="form1" runat="server">

  28.        <div>

  29.            <table style="font-family: Arial,'Segoe UI';">

  30.                <tr>

  31.                    <td colspan="3">

  32.                        <span align="left">

  33.                            <asp:Image ID="imgMail" runat="server"  Width="50px" Height="30px" />

  34.                        </span>

  35.                        <span style="font-size: 20px; margin-top: -5px">Sending Email through SMTP IIS

  36.                        </span>

  37.                    </td>

  38.                </tr>

  39.                <tr>

  40.                    <td>From</td>

  41.                    <td>:</td>

  42.                    <td>

  43.                        <asp:TextBox ID="txtFrom" runat="server" Width="300"></asp:TextBox>

  44.                    </td>

  45.                </tr>

  46.                <tr>

  47.                    <td>To</td>


  48.                    <td colspan="3" align="Right">

  49.                        <asp:Button ID="btnSend" runat="server" Text="Send mail" OnClick="btnSend_Click" BackColor="#009999" ForeColor="White" Font-Bold="true" BorderColor="#009999" />

  50.                    </td>

  51.                </tr>

  52.                <tr>

  53.                    <td colspan="3">

  54.                        <asp:Label ID="lblStatus" runat="server" />

  55.                    </td>

  56.                </tr>

  57.            </table>

  58.        </div>

  59.    </form>

  60. </body>

  61. </html>



6. In index.aspx.cs copy the Code:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net.Mail;

namespace SendEmail

{

   public partial class index : System.Web.UI.Page

   {

       protected void Page_Load(object sender, EventArgs e)

       {


       }


       protected void btnSend_Click(object sender, EventArgs e)

       {


           try

           {

               string from = txtFrom.Text.Trim();

               string to = txtTo.Text.Trim();

               string subject = txtSubject.Text.Trim();

               string message = txtMessage.Text.Trim();


               SmtpClient objSmtpClient = new SmtpClient();

               objSmtpClient.Host = "smtp.gmail.com";

               objSmtpClient.Port = 587;

               objSmtpClient.EnableSsl = true;

               objSmtpClient.Send(from, to, subject, message);



               lblStatus.Text = "<b style='color:green'>Your Email has been sent successfully!!!</b>";

           }

           catch (SmtpException ex) { lblStatus.Text = "<b style='color:red'>" + ex.Message + "</b>"; }

       }



   }

}



Step 5: Creating Application Pool in IIS Manager

  1. Go to IIS Manager.

  2. Select your Server from the servers available on the left-hand side panel.

  3. Expand Sites, right click on the default application and select Add Application
    Give a Alias Name - SendEmail(Suggested).

  4. Select the physical path { C://Users/user/source/repos/SendEmail/SendEmail(with the bin folder) }

  5. Right click on the Application[SendEmail] > Edit Permissions... > Security > Edit > Add > Advanced > Find Now

Find: IUSR and IIS_IUSR and allow all permissions to these user. Repeat the step for the inetpub folder in C:\\ Directory also.





     6. Browse the Application from your local browser you will find error 0x80070005


Step 6: Setting up SMTP Server


In the ASP.NET Section click SMTP EMail

In SMTP Server give smtp.gmail.com

In PORT give 587

In Authentication Setting select Specify credentials

Username - your_email

Password - App Password from gmail settings.

Notice that your web.config file will automatically reflect the changes you have made in the smtp console.





In the IIS Section click Directory Browsing and set it to enable.

Now Browse your application




Step 7: Send Mail




Make sure the email you put in the ‘To’ have to be the email which you configured in the smtp server.

Suggestion: please tell your developer to embed the ‘To’ as a hidden field or hard code with the configured email address for smtp, so that the client does not have to put the ‘To’ address every time.




Trendy