Friday, March 1, 2013

Sending email via ASP. NET page


ASP.NET

Sending email with your web page in ASP.NET

in your config file enter :

<customErrors mode="Off"/>

before : </system.web>

contact.aspx

<%@ Page Title="" Language="C#"MasterPageFile="~/site.master" AutoEventWireup="true"CodeFile="Contact.aspx.cs" Inherits="Contact" %>

<asp:Content ID="Content1"ContentPlaceHolderID="head" Runat="Server">

<styletype="text/css">

.style1

{

color: #596D8E;

}

</style>

</asp:Content>

<asp:Content ID="Content2"ContentPlaceHolderID="ContentPlaceHolder1"Runat="Server">

<div class="post">

<h2class="title">

Contact Us</h2>

<div class="entry">

<p>

<spanclass="style1"><strong>Telephone :</strong></span> +441633853205</p>

<p>

<spanclass="style1"><strong>Mobile :</strong></span> +447908861828</p>

<p>

<spanclass="style1"><strong>Email :</strong></span> admin@richiecreative.co.uk<br/>

</p>

<table border = "0" style="width: 409px">

<tr>

<td>

<asp:Label ID="Label1"runat="server" Text="Name*"></asp:Label><br/>

</td>

<td>

<asp:TextBoxID="txtName" runat="server" ValidationGroup ="contact"></asp:TextBox><br />

<asp:RequiredFieldValidator ID="RequiredFieldValidator1"runat="server" ErrorMessage="*"

ControlToValidate ="txtName"></asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td>

<asp:LabelID="Label2" runat="server"Text="Subject*"></asp:Label><br />

</td>

<td>

<asp:TextBoxID="txtSubject"runat="server"></asp:TextBox><br />

<asp:RequiredFieldValidator ID="RequiredFieldValidator2"runat="server" ErrorMessage="*"

ControlToValidate ="txtSubject"></asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td>

<asp:LabelID="Label3" runat="server"Text="Email*"></asp:Label><br />

</td>

<td>

<asp:TextBoxID="txtEmail" runat="server"></asp:TextBox><br/>

<asp:RegularExpressionValidator id="valRegEx"runat="server"

ControlToValidate="txtEmail"

ValidationExpression=".*@.*\..*"

ErrorMessage="*InvalidEmail address."

display="dynamic">

</asp:RegularExpressionValidator>

<asp:RequiredFieldValidator ID="RequiredFieldValidator3"runat="server" ErrorMessage="*"

ControlToValidate ="txtEmail"></asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td valign ="top" >

<asp:LabelID="Label4" runat="server"Text="Content*"></asp:Label>

</td>

<td>

<asp:TextBoxID="RichTextBox" runat="server" TextMode ="MultiLine"

Height="164px" Width="343px" ></asp:TextBox><br />

<asp:RequiredFieldValidator ID="RequiredFieldValidator4"runat="server" ErrorMessage="*"

ControlToValidate ="RichTextBox"></asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td> Upload</td>

<td>

<asp:FileUploadID="FileUpload1" runat="server" />

</td>

</tr>

<tr>

<td></td>

<td>

<asp:ButtonID="btnSend" runat="server" Text="Send"OnClick="btnSend_Click" />

</td>

</tr>

<tr>

<td></td>

<td>

<asp:LabelID="lblMessage" runat="server" Text="" ForeColor= "Green"></asp:Label>

</td>

</tr>

</table>

</div>

</div>

</asp:Content>

----------------------------------------------------------------------------------------------------------------------------------

contact.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net;

using System.Net.Mail;

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

{

protected voidPage_Load(object sender, EventArgs e)

{

}

protected voidbtnSend_Click(object sender, EventArgs e)

{

MailMessage message = newMailMessage();

SmtpClient smtpClient =new SmtpClient();

string msg = string.Empty;

try

{

message.From = newMailAddress("youremail@yourdomain.co.uk");

message.To.Add("youremail@yourdomain.co.uk");

message.Subject =txtSubject.Text;

message.IsBodyHtml =true;

message.Body ="Name: " + txtName.Text + "<br /><br />Email: "+ txtEmail.Text + "<br /><br />Content: " +RichTextBox.Text;

if (FileUpload1.HasFile)

{

string FileName =System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

message.Attachments.Add(newAttachment(FileUpload1.PostedFile.InputStream, FileName));

}

System.Net.Mail.SmtpClient client = newSystem.Net.Mail.SmtpClient("mail.WebSiteLive.net");// smtp mail server

System.Net.NetworkCredential crdPassword = newSystem.Net.NetworkCredential("youremailusername", "youremailpassword");

client.Credentials =crdPassword;

client.Send(message);

msg = "Manythanks for contacting us , We will endeavour to respond to your email enquirieswithin 24 hours. <BR>";

lblMessage.Text = msg;

}

catch (Exception ex)

{

lblMessage.Text =ex.Message;

}

}

}

 

No comments:

Post a Comment