c# - Sending HTML Email - How to attach the .html file to the email? -
i have following method sending simple email:
private void welcomemail(string recipient) { mailmessage mailmessage = new mailmessage("myemail", recipient); stringbuilder sbemailbody = new stringbuilder(); sbemailbody.append("how can attach .html file here instead of writing whole code"); mailmessage.isbodyhtml = true; mailmessage.body = sbemailbody.tostring(); mailmessage.subject = "welcome domain.com"; smtpclient smtpclient = new smtpclient("smtp.gmail.com", 587); smtpclient.credentials = new system.net.networkcredential() { username = "myemail", password = "mypassword" }; smtpclient.enablessl = true; smtpclient.send(mailmessage); } what should remove/change/add send html formatted email?
the html file called responsivemail.html , contains more 100+ lines of html code (that's problem).
if have html file in site root in file called emailtemplate.html, can read html memory , assign body of email.
mailmessage.body = file.readalltext(server.mappath("~/emailtemplate.html")); i don't recommend this, embed html code directly. since html spans multiple lines, we'll need use string literal (@"this string literal"). html has double quotes in it. you'd need escape them doubling double quotes.
mailmessage.body = @" <h1>this html in email!</h1> <a href=""http://google.com\"">this test link.</a> "; long term, if you're going sending emails html , need inject values them, suggest postal or other libraries. postal can make easy embed images.
Comments
Post a Comment