0% found this document useful (0 votes)
192 views

Sending Email Using Third Party SMTP Ex (SMTP - Gmai.coml)

The document provides instructions to create a basic contact form application in ASP.NET with email functionality. It involves 5 steps: 1) Creating a contact.aspx page with form fields, 2) Adding code to the contact.aspx.cs file to send email using SMTP, 3) Adding SMTP settings to the web.config file, 4) Publishing the website, and 5) Uploading the files to a server. The contact form allows users to enter name, email, comments, and submit the information via email.

Uploaded by

Marcia Glore
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views

Sending Email Using Third Party SMTP Ex (SMTP - Gmai.coml)

The document provides instructions to create a basic contact form application in ASP.NET with email functionality. It involves 5 steps: 1) Creating a contact.aspx page with form fields, 2) Adding code to the contact.aspx.cs file to send email using SMTP, 3) Adding SMTP settings to the web.config file, 4) Publishing the website, and 5) Uploading the files to a server. The contact form allows users to enter name, email, comments, and submit the information via email.

Uploaded by

Marcia Glore
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

‘Step1 . Create a new contact.aspx page.

‘Code the following.

I. ‘<%@ Page Language="C#" AutoEventWireup="true"


‘CodeFile="contact.aspx.cs" Inherits="contact" %>
II. ‘<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
‘"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
III. ‘<html xmlns="http://www.w3.org/1999/xhtml">
IV. ‘<head id="Head1" runat="server">
V. ‘<title></title>
VI. ‘</head>
VII. ‘<body>
VIII. ‘<form id="form1" runat="server">
IX. ‘<h3>My Contact Form</h3>
X. ‘<table id="contact" cellspacing="0">
XI. ‘<tr>
XII. ‘<td style="width: 74px">First Name:</td>
XIII. ‘<td><asp:TextBox ID="FNameTB" runat="server" /></td>
XIV. ‘</tr>
XV. ‘<tr>
XVI. ‘<td style="width: 74px">Last Name:</td>
XVII. ‘<td><asp:TextBox ID="LNameTB" runat="server" /></td>
XVIII. ‘</tr>
XIX. ‘<tr>
XX. ‘<td style="width: 74px">Email:</td>
XXI. ‘<td><asp:TextBox ID="EmailTB" runat="server" /></td>
XXII. ‘</tr>
XXIII. ‘<tr>
XXIV. ‘<td style="width: 74px"></td>
XXV. ‘</tr>
XXVI. ‘<tr>
XXVII. ‘<td style="width: 74px">Comments:</td>
XXVIII. ‘<td><asp:TextBox ID="CommentsTB" runat="server"
‘TextMode="MultiLine" /></td>
XXIX. ‘</tr>
XXX. ‘<tr>
XXXI. ‘<td colspan="2"><asp:Button ID="btnSubmit" runat="server"
‘Text="Submit" OnClick="SendMail"/>
XXXII. ‘<asp:Button ID="btnReset" runat="server" Text="Reset" /></td>
XXXIII. ‘</tr>
XXXIV. ‘</table>
XXXV. ‘</form>
XXXVI. ‘</body>
XXXVII. ‘</html>

‘Step 2. Code the following in code behind file i.e. contact.aspx.cs

I. ‘using System;
II. ‘using System.Data;
III. ‘using System.Configuration;
IV. ‘using System.Collections;
V. ‘using System.Web;
VI. ‘using System;
VII. ‘using System.Collections.Generic;
VIII. ‘using System.Web;
IX. ‘using System.Web.UI;
X. ‘using System.Web.UI.WebControls;
XI. ‘using System.Net.Mail;
XII. ‘using System.Net;

XIII. ‘public partial class contact : System.Web.UI.Page


XIV. ‘{
XV. ‘protected void Page_Load(object sender, EventArgs e)
XVI. ‘{

XVII. ‘}
XVIII. ‘protected void SendMail(object sender, EventArgs e)
XIX. ‘{
a. ‘MailMessage mail = new MailMessage();
b. ‘mail.From = new MailAddress(EmailTB.Text);
c. ‘mail.To.Add("[email protected]");
d. ‘mail.Subject = "Contact Us";
e. ‘mail.IsBodyHtml = true;
f. ‘mail.Body = "First Name: " + FNameTB.Text + "<br />";
g. ‘mail.Body += "Last Name: " + LNameTB.Text + "<br />";
h. ‘mail.Body += "Email: " + EmailTB.Text + "<br />";
i. ‘mail.Body += "Comments: " + CommentsTB.Text + "<br />";

j. ‘smtpClient smtp = new SmtpClient();


k. ‘smtp.Host = "smtp.gmail.com";
l. ‘smtp.Credentials = new
m. ‘NetworkCredential("[email protected]", "password");
n. ‘smtp.Port = 587;
o. ‘smtp.EnableSsl = true;
p. ‘smtp.Send(mail);

‘}
‘}

‘Step 3. Code the following to web.config file

I. ‘<?xml version="1.0"?>
II. ‘<!--
III. ‘Note: As an alternative to hand editing this file you can use
‘the
IV. ‘web admin tool to configure settings for your application. Use
V. ‘the Website->Asp.Net Configuration option in Visual Studio.
VI. ‘A full list of settings and comments can be found in
VII. ‘machine.config.comments usually located in
VIII. ‘\Windows\Microsoft.Net\Framework\v2.x\Config
IX. ‘-->
X. ‘<configuration>
XI. ‘<system.net>
XII. ‘<mailSettings>
XIII. ‘<smtp deliveryMethod="Network" from="SomeWebsite Admin
‘&lt;[email protected]&gt;">
XIV. ‘<network host="smtp.gmail.com" port="587"
‘defaultCredentials="true" userName="[email protected]"
‘password="password" />
XV. ‘</smtp>
XVI. ‘</mailSettings>
XVII. ‘</system.net>
XVIII. ‘<system.web>
XIX. ‘<customErrors mode="Off"/>
XX. ‘</system.web>

XXI. </configuration>

Step 4. Publish the website.

Step 5. Upload the files.

Alternatively you can pass the relative IP address of your SMTP relay host.

You might also like