MailCoding in HTML with attachment using asp.net C#
HTML Page
<!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>
<title>Untitled Page</title>
<script src="Script/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function SendEmail() {
var name="TestUser";
var fileName="";
$.ajax({
type: "post",
url: 'SendEmail.aspx/SendMail',
data: "{name:'" + name + "',fileName:'"+ fileName +"'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(data.d);
},
error: function() {
alert('Problem...');
}
});
return false;
}
</script>
</head>
<body>
<input id="Button1" type="button" value="button" onclick="SendEmail()" />
</body>
</html>
C# Page
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.Net.Mail;
using System.Net;
public partial class SendEmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod()]
public static string SendMail(string name, string fileName)
{
string msg = string.Empty;
try
{
SendEmailProcess(name, fileName);
msg = "mail send suscessfully...";
}
catch (Exception ex)
{
}
return msg;
}
private static void SendEmailProcess(string name, string fileName)
{
System.Net.Mail.Attachment attachment = null;
MailMessage message = new MailMessage();
try
{
string tomailids = "athi.litztech@gmail.com"; //.....if add multiple mailid seperated by commas.....
fileName = @"D:\athi.txt"; //file path
string smtpUsername = "athirarajinesh@gmail.com"; // Client Mail Id
string smtpPwd = "password"; // Client Mail Pwd
message.From = new MailAddress(smtpUsername);
message.To.Add(tomailids);
message.Bcc.Add("levale.xlevale@gmail.com");
message.Subject = "Testing Mail";
message.Body = BodyMessage(name);
message.IsBodyHtml = true;
attachment = new System.Net.Mail.Attachment(fileName);
message.Attachments.Add(attachment);
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(smtpUsername, smtpPwd);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
attachment.Dispose();
}
catch (Exception ex)
{
if (attachment != null)
{
attachment.Dispose();
}
}
}
// Body Message Alignment
private static string BodyMessage(string name)
{
string strHTML = string.Empty;
strHTML = "<HTML>";
strHTML = strHTML + "<head>";
strHTML = strHTML + "<style>";
strHTML = strHTML + "<!--";
strHTML = strHTML + " /* Style Definitions */";
strHTML = strHTML + "p.MsoNormal, li.MsoNormal, div.MsoNormal";
strHTML = strHTML + " {margin:0in;";
strHTML = strHTML + " margin-bottom:.0001pt;";
strHTML = strHTML + " font-size:10.0pt;";
strHTML = strHTML + " font-family:'Arial';}";
strHTML = strHTML + "@page Section1";
strHTML = strHTML + " {size:8.5in 11.0in;";
strHTML = strHTML + " margin:1.0in 1.25in 1.0in 1.25in;}";
strHTML = strHTML + "div.Section1";
strHTML = strHTML + " {page:Section1;}";
strHTML = strHTML + "p.disclaimer";
strHTML = strHTML + " {margin:0in;";
strHTML = strHTML + " margin-bottom:.0001pt;";
strHTML = strHTML + " font-size:6.0pt;";
strHTML = strHTML + " font-family:'Arial';}";
strHTML = strHTML + "-->";
strHTML = strHTML + "</style>";
strHTML = strHTML + "</head>";
strHTML = strHTML + "<body lang=EN-US>";
strHTML = strHTML + "<div class=Section1>";
strHTML = strHTML + "<p class=MsoNormal> </p>";
strHTML = strHTML + "<p class=MsoNormal>Dear <b>" + name + "</b></p>";
strHTML = strHTML + "<p class=MsoNormal> </p>";
strHTML = strHTML + "<p class=MsoNormal>Thanks & Regards</p>";
strHTML = strHTML + "<p class=MsoNormal> </p>";
strHTML = strHTML + "<p class=MsoNormal>Client Name </p>";
strHTML = strHTML + "<p class=MsoNormal> </p>";
strHTML = strHTML + "</div>";
strHTML = strHTML + "</body>";
strHTML = strHTML + "</BODY>";
strHTML = strHTML + "</HTML>";
return strHTML;
}
}
No comments:
Post a Comment