c# - Best way for sending advance email -
i'd send email asp.net . use code , work fine.
mail.from = new mailaddress("mail@gmail.com", "sender", system.text.encoding.utf8); string = session["umail"].tostring(); mail.to.add(to); mail.isbodyhtml = true; mail.subjectencoding = encoding.utf8; mail.bodyencoding = encoding.getencoding("utf-8"); mail.subject = "subject"; mail.body = "body" ; smtpclient smtp = new smtpclient("smtp.gmail.com", 587); smtp.usedefaultcredentials = false; smtp.enablessl = true; smtp.credentials = new networkcredential("mail@gmail.com", "pass"); smtp.send(mail);
but i'd custom , beautiful mail. emails send facebook, google team , etc. know can use html tag in mail.body
way? best way ?
this ready use code snippet use sending email contains both text content , content based on html template:
// first create plain text version , set alternateview // create html version mailmessage msg = new mailmessage(); msg.from = new mailaddress("from@email", "from name"); msg.subject = "subject"; msg.to.add("to@email"); msg.bodyencoding = encoding.utf8; string plainbody = "body of plain email"; //first create text version alternateview plainview = alternateview.createalternateviewfromstring(plainbody, encoding.utf8, "text/plain"); msg.alternateviews.add(plainview); //now create html version maildefinition message = new maildefinition(); message.bodyfilename = "~/mailtemplates/template1.htm"; message.isbodyhtml = true; message.from = "from@email"; message.subject = "subject"; //build replacement collection replace fields in template1.htm file listdictionary replacements = new listdictionary(); replacements.add("<%username%>", "tousername");//example of dynamic content username //now create mail message using mail definition object //the createmailmessage object takes source control object last parameter, //if object working webcontrol can pass "this", //otherwise create dummy control below. mailmessage msghtml = message.createmailmessage("to@email", replacements, new literalcontrol()); alternateview htmlview = alternateview.createalternateviewfromstring(msghtml.body, encoding.utf8, "text/html"); //example of linked image linkedresource imgres = new linkedresource(server.mappath("~/mailtemplates/images/imga.jpg"), system.net.mime.mediatypenames.image.jpeg); imgres.contentid = "imga"; imgres.contenttype.name = "imga.jpg"; imgres.transferencoding = system.net.mime.transferencoding.base64; htmlview.linkedresources.add(imgres); msg.alternateviews.add(htmlview); //sending prepared email smtpclient smtp = new smtpclient();//it reads smpt params web.config smtp.send(msg);
and these key parts of html template:
<p>username: <%username%></p> <img src="cid:imga"/>
Comments
Post a Comment