gridview.rendercontrol returns System.Web.UI.HtmlTextWriter - c#

I am trying to send grid in my email, It sends email with a only System.Web.UI.HtmlTextWriter, It does not populate grid.
here is my code:
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
StringBuilder emailBody = new StringBuilder();
StringWriter emailStringWriter = new StringWriter();
HtmlTextWriter htmlGrid = new HtmlTextWriter(new HtmlTextWriter(new StringWriter(emailBody, CultureInfo.InvariantCulture)));
mail.Dispose();
mail = new System.Net.Mail.MailMessage();
mail.To.Add('abc#xyz.com');
mail.From = 'def#yzy.com';
mail.Subject = 'grid example';
myGrid.RenderControl(htmlGrid);
mail.Body = htmlGrid.ToString();
mail.IsBodyHtml = true;
smtp = new System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["smtpSetting"].ToString());
smtp.Port = 25;
smtp.Send(mail);
mail.Dispose();
I received the email but grid was not there , only this text was there: System.Web.UI.HtmlTextWriter

You need to declare a StringBuilder for your HtmlTextWriter.
StringBuilder sb = new StringBuilder();
HtmlTextWriter htmlGrid = new HtmlTextWriter(new StringWriter(sb, CultureInfo.InvariantCulture));
You may need to flush the writer(not certain), but you will use sb.ToString() to get the rendered html.
mail.Body = sb.ToString();

Related

How to send dymanically generated html as a email attachment in C#

I am using following code to send email but it is failed if I used html attachment.
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer2 = new System.IO.StreamWriter(ms);
writer2.Write("<html><head></head><body>Invoice 1</body></html>");
writer2.Flush();
writer2.Dispose();
MailMessage mm = new MailMessage();
mm.To.Add("test#gmail.com");
mm.CC.Add("test2#gmail.com");
mm.From = new MailAddress("test3#gmail.com");
mm.Subject = "नोटिस";
mm.Body = sb.ToString();
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Html);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
attach.ContentDisposition.FileName = "myFile.html";
mm.Attachments.Add(attach);
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "rcms.pdf"));
ms.Close();
mm.IsBodyHtml = true;
only one line needed.
var a = System.Net.Mail.Attachment.CreateAttachmentFromString("‌​'"+sb.ToString()+"'<‌​/body>", "notices.html");

Send email attachment created in HttpContext.Current.Response

I am creating a pdf using itextsharp. The pdf created normally and post it to the browser so the user can easily save the file to his computer . Now i want this generated pdf automatically send it also with email . I have try to convert the doc before post it to context and attach it to an email but with no any success. My code is:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=mypdf.pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
string imagepath = Server.MapPath(".") + "/assets/myimages/myimage.png";
Document Doc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(Doc);
PdfWriter pdfwriter= PdfWriter.GetInstance(Doc, HttpContext.Current.Response.OutputStream);
Doc.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath);
image.ScalePercent(106f,90f);
Doc.Add(image);
//adding elements using itextshart pdf
AddPDf(pdfwriter,Doc);
//to add html in pdf
// htmlparser.Parse(stringReader);
OnEndPage(pdfwriter, Doc);
Doc.Close();
email_send(Doc.ToString());
HttpContext.Current.Response.End();
public void email_send( string filename)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("myemail#gmail.com");
mail.To.Add("reciever#gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment((Server.MapPath(filename.ToString())));
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "mypass");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
The error i get is :
Could not find file
{"Could not find file 'C:\\Admin\\iTextSharp.text.Document'.":"C: \\Admin\\iTextSharp.text.Document"}
The reason for the error is that you're passing the Doc object reference to your email_send method, instead of the file path.
I made a somewhat similar emailing solution by reading the document to a MemoryStream and passing that as the attachment.
public void email_send(Document d)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("myemail#gmail.com");
mail.To.Add("reciever#gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.IO.MemoryStream ms = new System.IO.MemoryStream();
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(d, ms);
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
Attachment attach = new Attachment(ms, ct);
mail.Attachments.Add(attach);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail#gmail.com", "mypass");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
writer.Flush();
writer.Dispose();
ms.Dispose();
}
Call it email_send(Doc).

ASPX email HTML format

I have this aspx function which send email. Unfortunetely, it does not render html tag on gmail or outlook, but on my android phone looks fine. How to fix it? thanks.
protected void SendMail()
{
var fromAddress = "device#email.com";
var toAddress = "toEMAIL#gmail.com";
const string fromPassword = "****";
var subject = "subject";
string body = " <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\">";
body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">";
body += "</HEAD><BODY>";
body += "<b>some text some text</b><br></br>";
body += "</BODY></HTML>";
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
smtp.Send(fromAddress, toAddress, subject, body);
}
EDITED:
So here is what i have now. But the compiler gives this error:
Line 392: mail.From = fromAddress;
Compiler Error Message: CS0029: Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress'
var fromAddress = "device#***";
var toAddress = "*****#gmail.com";
const string fromPassword = "******";
var subject = "New Project Started!";
string body = "<b>TA:</b>";
MailMessage mail= new MailMessage();
mail.From = fromAddress;
mail.To = toAddress;
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
smtp.Send(mail);
EDITED:
My working solution:
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
var fromAddress = "****"; // mail Address from where you send the mail
var toAddress = "****";
const string fromPassword = "***"; //Password of your gmail address
var subject = "New Job Created!";
// set smtp-client with basicAuthentication
SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com");
mySmtpClient.Port = 587;
mySmtpClient.EnableSsl = true;
mySmtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new
System.Net.NetworkCredential(fromAddress, fromPassword);
mySmtpClient.Credentials = basicAuthenticationInfo;
// add from,to mailaddresses
MailAddress from = new MailAddress(fromAddress, "");
MailAddress to = new MailAddress(toAddress, "Devision");
MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
// set subject and encoding
myMail.Subject = subject;
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
// set body-message and encoding
myMail.Body = "<b>TEST</b>";
myMail.BodyEncoding = System.Text.Encoding.UTF8;
// text or html
myMail.IsBodyHtml = true;
mySmtpClient.Send(myMail);
Create MailMessage Class. The key thing is to specify that the Body of the is HTML this is done with property IsBodyHtml
MailMessage mail= new MailMessage();
mail.From = fromAddress;
mail.To = toAddress;
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
//other stuff
smtp.Send(mail);

Insert image in email

I'm trying to insert an image in an email following this http://www.codeproject.com/Tips/326346/How-to-embed-a-image-in-email-body
but for some images are not showing in the mail.
Here is the code.
var message = new MailMessage();
message.From = new MailAddress("noreply#happy.com");
message.To.Add("testmail#gmail.com");
var body = new StringBuilder();
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
var path = #"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg";
AlternateView altView = AlternateView.CreateAlternateViewFromString("testing mail: <img src=cid:myImage>", null, MediaTypeNames.Text.Html);
LinkedResource imageToSend = new LinkedResource(path);
imageToSend.ContentId = "myImage";
altView.LinkedResources.Add(imageToSend);
message.AlternateViews.Add(altView);
var smtp = new SmtpClient();
smtp.Send(message);

problem with send mail method

In this method i send mail
var mail = new MailMessage();
mail.Subject = subject;
mail.SubjectEncoding = Encoding.UTF8;
mail.IsBodyHtml = mailServer.EsHtml;
mail.From = new MailAddress(mailFrom, nomFrom,Encoding.UTF8);
foreach(var item in mailTo)
mail.To.Add(new MailAddress(item.Key, item.Value, Encoding.UTF8));
foreach(var item in mailCC)
mail.Bcc.Add(new MailAddress(item.Key, item.Value, Encoding.UTF8));
mail.Body = message;
mail.BodyEncoding = Encoding.Unicode;
mail.Attachments.Add(new Attachment(attachments));
var clientMail = new SmtpClient();
clientMail.Credentials = new System.Net.NetworkCredential(mailFrom, passMail);
if(mailServer.PuertoCorreo.HasValue)
clientMail.Port = mailServer.PuertoCorreo.Value;
clientMail.Host = mailServer.ServidorCorreo;
clientMail.EnableSsl = mailServer.HabilitarSSL;
clientMail.Send(mail);
And it works fine but in gmail in the body i got this text਍猀搀搀昀ഀ more text and hotmail in attach name i got this 牰敵慢瀮晤.What is wrong in the method?
Try this:
mail.BodyEncoding = System.Text.Encoding.UTF8;

Categories