Hello Friends ,
i am working on 1 assignment in which i have to send an Email from ASP.net using Formatted Text Editor.
I don't wont to use any in built controls like rich
text box etc...
Now my problem is that this code is only working in
internet explorer 6. it is not working in other broswer ?
So, why it is not working in other brosers ?
second this , i want that on "Click TO Send Email " button click,
i want that all the formatted text are to be saved in the
database along with the format.
But i am not able to get the text and also not able to get the
format of the text.
so, can you please help me.
How can i get the text enter by the user along with the format.
Please try to solve my problem.
This is the Design of my page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Editor</title>
<SCRIPT language="javascript" src="TextEdit.js"></SCRIPT>
</head>
<body>
<form id="Form1" method="post" encType="multipart/form-data" runat="server">
<asp:label id="Label1" style="Z-INDEX: 120; LEFT: 5px; POSITION: absolute; TOP: 5px" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="8pt" ForeColor="#FF6600">Compose New Mail Message</asp:label>
<asp:Label id="lblMessage" style="Z-INDEX: 120; LEFT: 300px; POSITION: absolute; TOP: 5px" runat="server" Font-Names="Verdana" Font-Size="X-Small" Width="540" Visible="False" ForeColor="#FF8000" Font-Bold="True"></asp:Label>
<HR style="Z-INDEX: 115; LEFT: 1px; WIDTH: 451px; POSITION: absolute; TOP: 22px" align="center" width="100%" color="#cccccc" SIZE="1">
<asp:label id="Label3" style="Z-INDEX: 120; LEFT: 50px; POSITION: absolute; TOP: 27px" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="8pt" ForeColor="#000000">From :</asp:label>
<asp:textbox id="TextBox2" style="Z-INDEX: 129; LEFT: 100px; POSITION: absolute; TOP: 25px" runat="server" Height="21px" Width="353px"></asp:textbox>
<HR style="Z-INDEX: 117; LEFT: 0px; WIDTH: 451px; POSITION: absolute; TOP: 47px" align="center" width="100%" color="#cccccc" SIZE="1">
<asp:label id="Label4" style="Z-INDEX: 121; LEFT: 68px; POSITION: absolute; TOP: 53px" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="8pt" ForeColor="#000000">To :</asp:label>
<asp:textbox id="TextBox3" style="Z-INDEX: 130; LEFT: 100px; POSITION: absolute; TOP: 50px" runat="server" Height="21px" Width="353px"></asp:textbox>
<HR style="Z-INDEX: 117; LEFT: 0px; WIDTH: 451px; POSITION: absolute; TOP: 72px" align="center" width="100%" color="#cccccc" SIZE="1">
<asp:label id="Label2" style="Z-INDEX: 121; LEFT: 68px; POSITION: absolute; TOP: 78px" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="8pt" ForeColor="#000000">Cc :</asp:label>
<asp:textbox id="txtcc" style="Z-INDEX: 130; LEFT: 100px; POSITION: absolute; TOP: 75px" runat="server" Height="21px" Width="353px"></asp:textbox>
<HR style="Z-INDEX: 117; LEFT: 0px; WIDTH: 451px; POSITION: absolute; TOP: 97px" align="center" width="100%" color="#cccccc" SIZE="1">
<asp:label id="Label6" style="Z-INDEX: 121; LEFT: 61px; POSITION: absolute; TOP: 102px" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="8pt" ForeColor="#000000">Bcc :</asp:label>
<asp:textbox id="txtbcc" style="Z-INDEX: 130; LEFT: 100px; POSITION: absolute; TOP: 100px" runat="server" Height="21px" Width="353px"></asp:textbox>
<HR style="Z-INDEX: 119; LEFT: 0px; WIDTH: 451px; POSITION: absolute; TOP: 122px" align="center" width="100%" color="#cccccc" SIZE="1">
<asp:label id="Label5" style="Z-INDEX: 122; LEFT: 33px; POSITION: absolute; TOP: 128px" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="8pt" ForeColor="#000000">Subject :</asp:label>
<asp:textbox id="TextBox4" style="Z-INDEX: 131; LEFT: 100px; POSITION: absolute; TOP: 125px" runat="server" Height="21px" Width="353px"></asp:textbox>
<HR style="Z-INDEX: 119; LEFT: 0px; WIDTH: 451px; POSITION: absolute; TOP: 147px" align="center" width="100%" color="#cccccc" SIZE="1">
<asp:label id="Label7" style="Z-INDEX: 122; LEFT: 6px; POSITION: absolute; TOP: 154px" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="8pt" ForeColor="#000000">Attachment :</asp:label>
<asp:button id="Button2"
style="Z-INDEX: 119; LEFT: 99px; POSITION: absolute; TOP: 149px; right: 587px;"
runat="server" Text="Add Attachments" Width="145" Height="22"></asp:button>
<%--<input id="uploadedFile" style="LEFT: 300px; POSITION: absolute; TOP: 149px" size="30" visible="true" type="file" runat="server" NAME="uploadedFile">--%>
<input id="attachFile1" style="LEFT: 300px; POSITION: absolute; TOP: 149px" size="30" type="file" visible="false" runat="server">
<asp:textbox id="TextBox5" style="Z-INDEX: 133; LEFT: 73px; POSITION: absolute; TOP: 125px" runat="server" Height="25px" Width="500px" TextMode="MultiLine" Visible="false"></asp:textbox>
<%--<input type="hidden" runat="server" id="hdnmsg" NAME="hdnmsg">--%>
<asp:button id="Button1"
style="Z-INDEX: 132; LEFT: 98px; POSITION: absolute; TOP: 468px" runat="server"
Text="Click to Send Email" Width="145" Height="22"
onclick="Button1_Click"></asp:button>
<br />
<br />
<br />
<asp:Panel ID="pnlContentMgmt" runat="server" Width="550px" style="Z-INDEX: 133;
LEFT: 100px; POSITION:absolute; TOP: 175px" >
<table style="WIDTH: 500px; BORDER-COLLAPSE: collapse; HEIGHT: 77px" borderColor="black" cellSpacing="0" cellPadding="0" width="500" align="left" bgColor="buttonface" border="1">
<tr>
<td height="20">
<table height="18" cellSpacing="1" cellPadding="0" border="0">
<tr>
<td><IMG height="21" src="Images/VertLine2.jpg" width="10">
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('cut')" onmouseout="button_out(this);"><IMG height="19" alt="Cut" hspace="1" src="Images/Cut.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('copy')" onmouseout="button_out(this);"><IMG height="19" alt="Copy" hspace="1" src="Images/Copy.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('paste')" onmouseout="button_out(this);"><IMG height="19" alt="Paste" hspace="1" src="Images/Paste.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td><IMG height="21" src="Images/VertLine.jpg" width="10">
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('createLink')" onmouseout="button_out(this);"><IMG height="19" alt="Link" hspace="2" src="Images/Link.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('Unlink')" onmouseout="button_out(this);"><IMG height="19" alt="Unlink" hspace="2" src="Images/unlink.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<%--<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('SelectAll')" onmouseout="button_out(this);">
<IMG height="19" alt="Select All" hspace="2" src="Images/SelectAll.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>--%>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('RemoveFormat')" onmouseout="button_out(this);"><IMG height="19" alt="Remove Format" hspace="2" src="Images/UnSelect.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="setImage()" onmouseout="button_out(this);"><IMG height="19" alt="Insert Image" hspace="2" src="Images/InsertImage.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<!--<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="vbscript:NewsBody_rich.document.body.innerText=SpellChecker(NewsBody_rich.document.body.innerText)" onmouseout="button_out(this);">
<IMG id="spellcheck" height="19" alt="Spell Checker" hspace="2" src="Images/spell.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>-->
<td><IMG height="21" src="Images/VertLine.jpg" width="10">
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('bold')" onmouseout="button_out(this);"><IMG height="19" alt="Bold" hspace="1" src="Images/Bold.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('italic')" onmouseout="button_out(this);"><IMG height="19" alt="Italic" hspace="1" src="Images/Italic.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('underline')" onmouseout="button_out(this);"><IMG height="19" alt="Underline" hspace="1" src="Images/Under.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td><IMG height="21" src="Images/VertLine.jpg" width="10"></td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('justifyleft')" onmouseout="button_out(this);"><IMG height="19" alt="Justify Left" hspace="1" src="Images/Left.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('justifycenter')" onmouseout="button_out(this);"><IMG height="19" alt="Center" hspace="1" src="Images/Center.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('justifyright')" onmouseout="button_out(this);"><IMG height="19" alt="Justify Right" hspace="1" src="Images/Right.gif" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td><IMG height="21" src="Images/VertLine.jpg" width="10"></td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('insertorderedlist')" onmouseout="button_out(this);"><IMG height="19" alt="Ordered List" hspace="2" src="Images/numlist.GIF" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="cmdExec('insertunorderedlist')" onmouseout="button_out(this);"><IMG height="19" alt="Unordered List" hspace="2" src="Images/bullist.GIF" width="20" align="absMiddle" vspace="1">
</div>
</td>
<td><IMG height="21" src="Images/VertLine.jpg" width="10"></td>
<td>
<div onmouseup="button_up(this);" class="cbtn" onmousedown="button_down(this);" onmouseover="button_over(this);" onclick="foreColor()" onmouseout="button_out(this);"><IMG id="BtnColor" height="19" alt="Forecolor" hspace="2" src="Images/fgcolor.gif" width="20" align="absMiddle" vspace="1"></div>
</td>
<td><IMG height="21" src="Images/VertLine.jpg" width="10"></td>
<td>
<select style="WIDTH: 80px"
onchange="cmdExec('fontname',this[this.selectedIndex].value);"
name="selfontname" runat="server" id="selfontname">
<option selected>Font</option>
<option value="Arial">Arial</option>
<option value="Arial Black">Arial Black</option>
<option value="Arial Narrow">Arial Narrow</option>
<option value="Comic Sans MS">Comic Sans MS</option>
<option value="Courier New">Courier New</option>
<option value="System">System</option>
<option value="Tahoma">Tahoma</option>
<option value="Times">Times</option>
<option value="Shruti">Shruti</option>
<option value="Verdana">Verdana</option>
<option value="Wingdings">Wingdings</option>
</select>
</td>
<td><select onchange="cmdExec('fontsize',this[this.selectedIndex].value);" name="selfontsize">
<option selected>Size</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</td>
<td><IMG height="21" src="Images/VertLine2.jpg" width="10"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="5"></td>
</tr>
<tr id="tr1" runat="server">
<td id="mam" height="180" runat="server">
<iframe id="NewsBody_rich" runat="server"
width="100%"
height="100%" >
</iframe>
<asp:HiddenField ID="HiddenField1" runat="server" />
</td>
</tr>
</table>
</asp:Panel>
<div>
</div>
</form>
</body>
</html>
This is the code of my file
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Web.Mail;
using System.IO;
using System.ComponentModel;
using System.Drawing;
using System.Web.SessionState;
using System.Data.SqlClient;
using System.Windows.Forms;
public partial class Default2 : System.Web.UI.Page
{
//Protected hdnmsg As System.Web.UI.HtmlControls.HtmlInputHidden
HtmlInputButton hdmsg2;
protected void Page_Load(object sender, EventArgs e)
{
attachFile1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Attributes.Add("onClick", "javascript:fillTxt();");
string attach1 = "";
string strFileName = "";
MailMessage message = new MailMessage();
if ((!string.IsNullOrEmpty(attachFile1.PostedFile.FileName)))
{
HttpPostedFile ulFile = attachFile1.PostedFile;
Int64 nFileLen = ulFile.ContentLength;
if ((nFileLen > 0))
{
strFileName = Path.GetFileName(attachFile1.PostedFile.FileName);
strFileName = "Uploads/" + strFileName;
attachFile1.PostedFile.SaveAs(Server.MapPath(strFileName));
MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
message.Attachments.Add(attach);
attach1 = strFileName;
}
}
message.From = TextBox2.Text;
message.To = TextBox3.Text;
message.Cc = txtcc.Text;
message.Bcc = txtbcc.Text;
message.Subject = TextBox4.Text;
message.Body = hdnmsg.Value;
message.BodyFormat = MailFormat.Html;
SmtpMail.SmtpServer = "127.0.0.1";
SmtpMail.Send(message);
if ((!string.IsNullOrEmpty(attach1)))
{
File.Delete(Server.MapPath(attach1));
}
Response.Flush();
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
txtcc.Text = "";
txtbcc.Text = "";
lblMessage.Visible = true;
lblMessage.ForeColor = Color.Red;
lblMessage.Text = "Your email has been sent";
}
protected void Button2_Click(object sender, EventArgs e)
{
attachFile1.Visible = true;
}
}
Code of Java Script File :
var type;
function button_over(eButton)
{
eButton.style.backgroundColor = "LightSlateGray";
eButton.style.borderColor = "darkblue darkblue darkblue darkblue";
}
function button_out(eButton)
{
eButton.style.backgroundColor = "threedface";
eButton.style.borderColor = "threedface";
}
function button_down(eButton)
{
eButton.style.backgroundColor = "#8494B5";
eButton.style.borderColor = "darkblue darkblue darkblue darkblue";
}
function button_up(eButton)
{
eButton.style.backgroundColor = "#B5BDD6";
eButton.style.borderColor = "darkblue darkblue darkblue darkblue";
eButton = null;
}
var isHTMLMode=false
function document.onreadystatechange()
{
NewsBody_rich.document.designMode="On";
}
function cmdExec(cmd,opt)
{
if (isHTMLMode)
{
alert("Formatting happens only in Normal mode");
return;
}
NewsBody_rich.focus();
NewsBody_rich.document.execCommand(cmd,"",opt);
NewsBody_rich.focus();
} // end cmdExec
function ChangeImage (ImageName,FileName) {
document[ImageName].src = FileName;
}
var intHCount = 0;
var intNCount = 0;
function setMode(Nbut, Hbut)
{
var sTmp;
isHTMLMode = Hbut;
isNormalMode = Nbut;
if(Hbut == 1)
{
intHCount++;
intNCount = 0;
}
if(Nbut == 1)
{
intNCount++;
intHCount = 0;
}
if (isHTMLMode && intHCount == 1)
{
sTmp=NewsBody_rich.document.body.innerHTML;NewsBody_rich.document.body.innerText=sTmp;
ChangeImage ('HTMLbut','images/HTML_on.jpg');
ChangeImage ('Normalbut','images/Normal_off.jpg'); return true;
}
if(isNormalMode && intNCount == 1)
{
sTmp=NewsBody_rich.document.body.innerText;NewsBody_rich.document.body.innerHTML=sTmp;
ChangeImage ('HTMLbut','images/HTML_off.jpg');
ChangeImage ('Normalbut','images/Normal_on.jpg'); return true;
}
NewsBody_rich.focus();
} // end setMode
function createLink()
{
if (isHTMLMode){alert("Please uncheck 'Edit HTML'");return;}
cmdExec("CreateLink");
} // end createLink
function foreColor()
{
var arr = showModalDialog("ColorPalette.htm","","font-family:Verdana; font-size:12; dialogWidth:18; dialogHeight:18" );
if (arr != null) cmdExec("ForeColor",arr);
} // end foreColor
function setImage()
{
var imgSrc = " ";
var flag = 0;
while(imgSrc == " ")
{
imgSrc = prompt('Enter image location with image name ', '');
if (imgSrc == " ") alert("Please enter the required URL");
if(imgSrc != "")
{
cmdExec('insertimage', imgSrc);
}
} // end while
} // end setImage
function ShowCM(t,headerText)
{
NewsBody_rich.document.designMode='On';
if (divContent.style.display=="block")
{
divContent.style.display="none";
}
else
{
divContent.style.display="block";
}
type=t;
spnHeader.innerText=headerText;
if(document.getElementById(type).value != '')
{
NewsBody_rich.document.body.innerHTML=document.getElementById(type).value;
}
}
function fillTxt()
{
var State = new Object()
var aa;
document.getElementById("hdnmsg").innerText=NewsBody_rich.document.body.innerHTML;
}
function ClearIFrame()
{
NewsBody_rich.document.body.innerHTML ='';
divContent.style.display="none";
}
function DisableSave(bool)
{
document.Form1.btnSaveHead.style.display="none";
if(bool=='show')
{
if(document.Form1.btnSaveHead.style.display=="none")
{
document.Form1.btnSaveHead.style.display="block";
}
else
{
document.Form1.btnSaveHead.style.display="none";
}
}
}