Sometimes we need to send mail with attachment. so if you need so here is the code.
First Create a common class to get the template that you want to use to create a PDF to send in attachement.
{
WebClient client = new WebClient();
string content = client.DownloadString(path);
return content;
}
}
Now write the code on button click to send the mail
First Create a common class to get the template that you want to use to create a PDF to send in attachement.
public class CommonFunction
{
public string GetTemplate(string path){
WebClient client = new WebClient();
string content = client.DownloadString(path);
return content;
}
}
Now write the code on button click to send the mail
protected void btnSend_Click(object sender, EventArgs e)
{
StringBuilder InvoiceTemplate = new StringBuilder();
CommonFunctions Functions = new CommonFunctions();
InvoiceTemplate.Append(Functions.GetTemplate(Server.MapPath("~/Templates/Invoice.htm")));
InvoiceTemplate.Replace("##MS##", "John");
InvoiceTemplate.Replace("##BILLNO##", "34");
InvoiceTemplate.Replace("", "");
string To = cutomer23@gmail.com;
SendInvoice(To, "", "", "Invoice", "This is Invoice", InvoiceTemplate);
}
Now create a method SendInvoice
public int SendInvoice(string To, string CC, string AttachmentFilePath, string Subject, string BodyText, StringBuilder InvoiceText)
{
string From = ConfigurationManager.AppSettings["from"].ToString();
string Password = ConfigurationManager.AppSettings["password"].ToString();
SmtpClient mailClient = new SmtpClient();
NetworkCredential basicAuthenticationInfo = new NetworkCredential();
basicAuthenticationInfo.UserName = From;
basicAuthenticationInfo.Password = Password;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicAuthenticationInfo;
mailClient.Host = ConfigurationManager.AppSettings["host"].ToString();
mailClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["port"].ToString());
System.Net.Mail.MailMessage mailmessage = new System.Net.Mail.MailMessage(From, To, Subject, BodyText);
if (CC.Trim() != "")
mailmessage.CC.Add(CC);
using (FileStream fs = new FileStream(Server.MapPath("~/Templates/Invoice.pdf"), FileMode.Create))
{
StringReader sr = new StringReader(Convert.ToString(InvoiceText));
Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlWorker = new HTMLWorker(document);
PdfWriter.GetInstance(document, fs);
document.Open();
htmlWorker.Parse(sr);
document.Close();
fs.Close();
fs.Dispose();
}
AttachmentFilePath = Server.MapPath("~/Templates/Invoice.pdf");
System.Net.Mail.Attachment Attach = null;
if (AttachmentFilePath.Trim() != "")
Attach = new System.Net.Mail.Attachment(AttachmentFilePath);
if (Attach != null)
mailmessage.Attachments.Add(Attach);
mailmessage.IsBodyHtml = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.EnableSsl = true;
mailmessage.SubjectEncoding = System.Text.Encoding.UTF8;
mailmessage.BodyEncoding = System.Text.Encoding.UTF8;
mailClient.Send(mailmessage);
mailmessage.Dispose();
File.Delete(Server.MapPath("~/Templates/Invoice.pdf"));
}
}
No comments:
Post a Comment