<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Email.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 245px;
}
.style3
{
width: 124px;
}
.style4
{
width: 245px;
height: 134px;
}
.style5
{
width: 124px;
height: 134px;
}
.style6
{
height: 134px;
}
.style7
{
width: 245px;
height: 23px;
}
.style8
{
width: 124px;
height: 23px;
}
.style9
{
height: 23px;
}
</style>
<script language="javascript" type="text/javascript">
function ValidateFileUploadExtension(Source, args) {
var fupData = document.getElementById('<%= FileUpload1.ClientID %>');
var FileUploadPath = fupData.value;
if (FileUploadPath == '') {
// There is no file selected
alert("Please select image");
}
else {
var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
if (Extension == "gif" || Extension == "jpeg" || Extension == "jpg" || Extension == "png") {
args.IsValid = true; // Valid file type
}
else {
args.IsValid = false; // Not valid file type
}
}
}
function ValidateFileUploadExtension1(Source, args) {
var fupData = document.getElementById('<%= FileUpload2.ClientID %>');
var FileUploadPath = fupData.value;
if (FileUploadPath == '') {
// There is no file selected
alert("Please select image");
}
else {
var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
if (Extension == "gif" || Extension == "jpeg" || Extension == "jpg" || Extension == "png") {
args.IsValid = true; // Valid file type
}
else {
args.IsValid = false; // Not valid file type
}
}
}
function ValidateFileUploadExtension2(Source, args) {
var fupData = document.getElementById('<%= FileUpload3.ClientID %>');
var FileUploadPath = fupData.value;
if (FileUploadPath == '') {
// There is no file selected
alert("Please select image");
}
else {
var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
if (Extension == "gif" || Extension == "jpeg" || Extension == "jpg" || Extension == "png") {
args.IsValid = true; // Valid file type
}
else {
args.IsValid = false; // Not valid file type
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div style="height: 426px">
<br />
<br />
<table class="style1">
<tr>
<td class="style2">
</td>
<td class="style3">
Welcome Admin,</td>
<td align ="right" >
<asp:LinkButton ID="LinkLogout" runat="server" CausesValidation="False"
onclick="LinkLogout_Click">Logout</asp:LinkButton>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
To</td>
<td>
<asp:TextBox ID="txtToAddress" runat="server" Width="318px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtToAddress" ErrorMessage="Enter a Email Address">*</asp:RequiredFieldValidator>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
Subject</td>
<td>
<asp:TextBox ID="txtSubject" runat="server" Width="318px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtSubject" ErrorMessage="Enter a Subject">*</asp:RequiredFieldValidator>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style4">
</td>
<td class="style5">
Message</td>
<td class="style6">
<asp:TextBox ID="txtMessage" runat="server" Height="96px" TextMode="MultiLine"
Width="349px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtMessage" ErrorMessage="Enter a Message">*</asp:RequiredFieldValidator>
</td>
<td class="style6">
</td>
<td class="style6">
</td>
</tr>
<tr>
<td class="style7">
</td>
<td class="style8">
Select Image</td>
<td class="style9">
<asp:FileUpload ID="FileUpload1" runat="server" Width="223px" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction="ValidateFileUploadExtension"
ErrorMessage="Please select valid image file 1" Font-Size="16px" ForeColor="red"
ControlToValidate="FileUpload1">*</asp:CustomValidator>
<asp:LinkButton ID="LinkAdd1" runat="server" onclick="LinkAdd1_Click"
CausesValidation="False">Add Additional Image</asp:LinkButton>
</td>
<td class="style9">
</td>
<td class="style9">
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td>
<asp:FileUpload ID="FileUpload2" runat="server" Visible="False" Width="222px" />
<asp:CustomValidator ID="CustomValidator2" runat="server"
ClientValidationFunction="ValidateFileUploadExtension1" ErrorMessage="Please select valid image file 2"
Font-Size="16px" ForeColor="red" ControlToValidate="FileUpload2">*</asp:CustomValidator>
<asp:LinkButton ID="LinkAdd2" runat="server" onclick="LinkAdd2_Click"
Visible="False" CausesValidation="False">Add Additional Image</asp:LinkButton>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td>
<asp:FileUpload ID="FileUpload3" runat="server" Visible="False" Width="219px" />
<asp:CustomValidator ID="CustomValidator3" runat="server"
ClientValidationFunction="ValidateFileUploadExtension2" ErrorMessage="Please select valid image file 3"
Font-Size="16px" ForeColor="red" ControlToValidate="FileUpload3">*</asp:CustomValidator></td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send Mail" />
</td>
<td>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Cancel"
CausesValidation="False" Height="26px" />
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="True" ShowSummary="False" />
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Mail;
using System.Net;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == "")
{
Response.Redirect("Login.aspx");
}
else
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string to = txtToAddress.Text.ToString().Trim();
string from = "yourmailid@gmail.com";
string password = "yourpassword";
string subject = txtSubject.Text.ToString().Trim();
string Message = txtMessage.Text.ToString().Trim();
string host = "smtp.gmail.com";
int port = 587;
MailMessage email = new MailMessage();
email.From = new MailAddress(from);
email.Subject = subject;
email.Body = Message;
SmtpClient smtp = new SmtpClient(host, port);
smtp.UseDefaultCredentials = false;
NetworkCredential nc = new NetworkCredential(from, password);
smtp.Credentials = nc;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
email.IsBodyHtml = true;
email.To.Add(new MailAddress(to));
string fileName = "";
if (FileUpload1.PostedFile != null)
{
HttpPostedFile attchment = FileUpload1.PostedFile;
int FileLength = attchment.ContentLength;
if (FileLength > 0)
{
fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Path.Combine(Server.MapPath("~/SendEmail/EmailAttachment"), fileName));
Attachment attachment = new Attachment(Path.Combine(Server.MapPath("~/SendEmail/EmailAttachment"), fileName));
email.Attachments.Add(attachment);
}
}
if (FileUpload2.PostedFile != null)
{
HttpPostedFile attchment = FileUpload2.PostedFile;
int FileLength = attchment.ContentLength;
if (FileLength > 0)
{
fileName = Path.GetFileName(FileUpload2.PostedFile.FileName);
FileUpload2.PostedFile.SaveAs(Path.Combine(Server.MapPath("~/SendEmail/EmailAttachment"), fileName));
Attachment attachment = new Attachment(Path.Combine(Server.MapPath("~/SendEmail/EmailAttachment"), fileName));
email.Attachments.Add(attachment);
}
}
if (FileUpload3.PostedFile != null)
{
HttpPostedFile attchment = FileUpload3.PostedFile;
int FileLength = attchment.ContentLength;
if (FileLength > 0)
{
fileName = Path.GetFileName(FileUpload3.PostedFile.FileName);
FileUpload3.PostedFile.SaveAs(Path.Combine(Server.MapPath("~/SendEmail/EmailAttachment"), fileName));
Attachment attachment = new Attachment(Path.Combine(Server.MapPath("~/SendEmail/EmailAttachment"), fileName));
email.Attachments.Add(attachment);
}
}
smtp.Send(email);
email.Dispose();
string[] fileNames = Directory.GetFiles(Server.MapPath("~/SendEmail/EmailAttachment"));
foreach (string fileName1 in fileNames)
File.Delete(Path.Combine(Server.MapPath("~/SendEmail/EmailAttachment"), fileName1));
clear();
}
private void clear()
{
LinkAdd2.Visible = false;
FileUpload2.Visible = false;
FileUpload3.Visible = false;
txtToAddress.Text = "";
txtSubject.Text = "";
txtMessage.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
clear();
}
protected void LinkAdd1_Click(object sender, EventArgs e)
{
LinkAdd2.Visible = true;
FileUpload2.Visible = true;
}
protected void LinkAdd2_Click(object sender, EventArgs e)
{
FileUpload3.Visible = true;
}
protected void LinkLogout_Click(object sender, EventArgs e)
{
Session["user"] = "";
Response.Redirect("Login.aspx");
}
}