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.

Prerequisites
Windows server 2008/2012/2014/2016 and above
Visual Studio 2017 Installed on Windows server.
IIS Manager Installed in Windows Server with all the features especially SMTP Server.
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:
In the GCP Console, go to the VM Instances page.
GO TO THE VM INSTANCES PAGE
Click Create instance.
In the Boot disk section, click Change to begin configuring your boot disk.
In the OS images tab, choose a Windows image.
Click Select.
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:
Open Server Manager.
Under Manage menu, select Add Roles, and Features
Select Role-based or Feature-based Installation
Select the appropriate server (local is selected by default), as shown below and click next.
Select Web Server (IIS)
No additional features are needed for IIS, so click Next
Click Next
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)
Click Install
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
Go to Files > New > Project
Select ASP.NET Web Application having the template selected as C# and Web selected(Name - SendEmail).
Select Empty project
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)
In index.aspx copy the Code:
|
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
Go to IIS Manager.
Select your Server from the servers available on the left-hand side panel.
Expand Sites, right click on the default application and select Add Application
Give a Alias Name - SendEmail(Suggested).Select the physical path { C://Users/user/source/repos/SendEmail/SendEmail(with the bin folder) }
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.