Sending mail with multiple file attachments using gmail in Asp.Net
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Send mail with multiple file attachments using Gmail in Asp.Net</title>
<script type="text/javascript">
function addTypeFile() {
if (!document.getElementById || !document.createElement)
return false;
var uploadArea = document.getElementById("upload-area");
if (!uploadArea)
return;
var newLine = document.createElement("br");
uploadArea.appendChild(newLine);
var newTypeFile = document.createElement("input");
newTypeFile.type = "file";
newTypeFile.size = "60";
if (!addTypeFile.lastAssignedId)
addTypeFile.lastAssignedId = 100;
newTypeFile.setAttribute("id", "file1" + addTypeFile.lastAssignedId);
newTypeFile.setAttribute("name", "file1:" + addTypeFile.lastAssignedId);
uploadArea.appendChild(newTypeFile);
addTypeFile.lastAssignedId++;
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<p>To:</p>
<p>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</p>
<p>Subject:</p>
<p>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</p>
<p>Message Body:</p>
<p>
<asp:TextBox ID="txtMsgBody" runat="server" TextMode="MultiLine" Columns="80" Rows="10"></asp:TextBox>
</p>
<p>
Attachment:
</p>
<p id="upload-area">
<input id="File1" type="file" runat="server" size="60" />
</p>
<p>
<a href="#" onclick="addTypeFile()">Attach another file</a>
</p>
<p><asp:Button ID="btnSubmit" runat="server" Text="Send" OnClick="btnSubmit_Click" /></p>
<span id="Span1" runat="server" />
</form>
</body>
</html>
<head>
<title>Send mail with multiple file attachments using Gmail in Asp.Net</title>
<script type="text/javascript">
function addTypeFile() {
if (!document.getElementById || !document.createElement)
return false;
var uploadArea = document.getElementById("upload-area");
if (!uploadArea)
return;
var newLine = document.createElement("br");
uploadArea.appendChild(newLine);
var newTypeFile = document.createElement("input");
newTypeFile.type = "file";
newTypeFile.size = "60";
if (!addTypeFile.lastAssignedId)
addTypeFile.lastAssignedId = 100;
newTypeFile.setAttribute("id", "file1" + addTypeFile.lastAssignedId);
newTypeFile.setAttribute("name", "file1:" + addTypeFile.lastAssignedId);
uploadArea.appendChild(newTypeFile);
addTypeFile.lastAssignedId++;
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<p>To:</p>
<p>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</p>
<p>Subject:</p>
<p>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</p>
<p>Message Body:</p>
<p>
<asp:TextBox ID="txtMsgBody" runat="server" TextMode="MultiLine" Columns="80" Rows="10"></asp:TextBox>
</p>
<p>
Attachment:
</p>
<p id="upload-area">
<input id="File1" type="file" runat="server" size="60" />
</p>
<p>
<a href="#" onclick="addTypeFile()">Attach another file</a>
</p>
<p><asp:Button ID="btnSubmit" runat="server" Text="Send" OnClick="btnSubmit_Click" /></p>
<span id="Span1" runat="server" />
</form>
</body>
</html>
aspx.cs code
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class Mailsending2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
List<Attachment> attachment = new List<Attachment>();
HttpFileCollection uploads = HttpContext.Current.Request.Files;
for (int i = 0; i < uploads.Count; i++)
{
if (uploads[i].FileName.Length == 0)
continue;
attachment.Add(new Attachment(uploads[i].InputStream, uploads[i].FileName));
}
MailSender.SendMail(txtTo.Text, txtSubject.Text, txtMsgBody.Text, attachment);
Span1.InnerHtml = "Mail send successfully." ;
}
catch (Exception ex)
{
Span1.InnerHtml = ex.Message;
}
}
}
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class Mailsending2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
List<Attachment> attachment = new List<Attachment>();
HttpFileCollection uploads = HttpContext.Current.Request.Files;
for (int i = 0; i < uploads.Count; i++)
{
if (uploads[i].FileName.Length == 0)
continue;
attachment.Add(new Attachment(uploads[i].InputStream, uploads[i].FileName));
}
MailSender.SendMail(txtTo.Text, txtSubject.Text, txtMsgBody.Text, attachment);
Span1.InnerHtml = "Mail send successfully." ;
}
catch (Exception ex)
{
Span1.InnerHtml = ex.Message;
}
}
}
No comments:
Post a Comment