Best way to send a provisional email address

I want to send a message to asp.net. I use this code and it works fine.

mail.From = new MailAddress(" mail@gmail.com ", "sender", System.Text.Encoding.UTF8); string to = 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 need regular and beautiful mail. Like emails sent from facebook, google team, etc. I know that you can use the html tag in mail.Body , but is it good? What is the best way?

+4
source share
2 answers

This is ready to use the code snippet that I use to send an email that contains text content and content based on the html template:

  // first we create a plain text version and set it to the AlternateView // then we create the 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 we create the text version AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, Encoding.UTF8, "text/plain"); msg.AlternateViews.Add(plainView); //now create the HTML version MailDefinition message = new MailDefinition(); message.BodyFileName = "~/MailTemplates/template1.htm"; message.IsBodyHtml = true; message.From = " from@email "; message.Subject = "Subject"; //Build replacement collection to replace fields in template1.htm file ListDictionary replacements = new ListDictionary(); replacements.Add("<%USERNAME%>", "ToUsername");//example of dynamic content for Username //now create mail message using the mail definition object //the CreateMailMessage object takes a source control object as the last parameter, //if the object you are working with is webcontrol then you can just pass "this", //otherwise create a dummy control as below. MailMessage msgHtml = message.CreateMailMessage(" to@email ", replacements, new LiteralControl()); AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, Encoding.UTF8, "text/html"); //example of a 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 the SMPT params from Web.config smtp.Send(msg); 

and these are the key parts of the html template:

 <p>Username: <%USERNAME%></p> <img src="cid:imgA"/> 
+4
source

I need regular and beautiful mail.

I know that you can use the html tag in mail.Body , but is it good? What is the best way?

I do not know exactly what this means, but, as a rule, there are two ways to do this. (If we talk about images or sources in email, etc.)

You can use the LinkedResource class of the .NET Framework.

Represents an embedded external resource in an email application, such as an image in an HTML attachment.

Alternatively and, more simply, in my opinion, if you want to use some images in your letter, put the image in a public place, and then just indicate this location in the HTML email.

+1
source

Source: https://habr.com/ru/post/1479123/


All Articles