Standard email class?

I am browsing my application trying to clear the code sending emails. I started creating my own mailbox wrapper class, but then I realized that there should be a standard email class there. I did some searches but found nothing.

Also, is there a code base for such things somewhere?

EDIT : Sorry, let me clarify.

I do not want to have this in my code at any time when I need to send an email:

System.Web.Mail.MailMessage message=new System.Web.Mail.MailMessage(); message.From="from e-mail"; message.To="to e-mail"; message.Subject="Message Subject"; message.Body="Message Body"; System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address"; System.Web.Mail.SmtpMail.Send(message); 

I created a class called Emailer that contains functions such as:

 SendEmail(string to, string from, string body) SendEmail(string to, string from, string body, bool isHtml) 

And so I can just put this line in my code to send an email:

 Emailer.SendEmail(" name@site.com ", " name2@site.com ", "My e-mail", false); 

I mean, this is not too complicated, but I thought there was a standard, accepted solution there.

+4
source share
4 answers

Something like that?

 using System; using System.Net; using System.Net.Mail; using System.Net.Mime; using MailMessage=System.Net.Mail.MailMessage; class CTEmailSender { string MailSmtpHost { get; set; } int MailSmtpPort { get; set; } string MailSmtpUsername { get; set; } string MailSmtpPassword { get; set; } string MailFrom { get; set; } public bool SendEmail(string to, string subject, string body) { MailMessage mail = new MailMessage(MailFrom, to, subject, body); var alternameView = AlternateView.CreateAlternateViewFromString(body, new ContentType("text/html")); mail.AlternateViews.Add(alternameView); var smtpClient = new SmtpClient(MailSmtpHost, MailSmtpPort); smtpClient.Credentials = new NetworkCredential(MailSmtpUsername, MailSmtpPassword); try { smtpClient.Send(mail); } catch (Exception e) { //Log error here return false; } return true; } } 
+7
source

Perhaps you are looking for SmtpClient ?

+1
source

I am using a generic class made from this old stack overflow answer .
Try it.


 public bool SendEmail(MailAddress toAddress, string subject, string body) { MailAddress fromAddress = new MailAddress("pull from db or web.config", "pull from db or web.config"); string fromPassword = "pull from db or config and decrypt"; string smtpHost = "pull from db or web.config"; int smtpPort = 587;//gmail port try { var smtp = new SmtpClient { Host = smtpHost, Port = smtpPort, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body, IsBodyHtml = true }) { smtp.Send(message); } return true; } catch (Exception err) { Elmah.ErrorSignal.FromCurrentContext().Raise(err); return false; } } 
0
source

This is a snippet from one of my projects. This is slightly more functional than some other implementations.

Using this function, you can create an email using:

  • Tokens that can be replaced with actual values โ€‹โ€‹at run time
  • Emails containing both text and HTML representation

     public MailMessage createMailMessage(string toAddress, string fromAddress, string subject, string template) { // Validate arguments here... // If your template contains any of the following {tokens} // they will be replaced with the values you set here. var replacementDictionary = new ListDictionary { // Replace with your own list of values { "{first}", "Pull from db or config" }, { "{last}", "Pull from db or config" } }; // Create a text view and HTML view (both will be in the same email) // This snippet assumes you are using ASP.NET (works w/MVC) // if not, replace HostingEnvironment.MapPath with your own path. var mailDefinition = new MailDefinition { BodyFileName = HostingEnvironment.MapPath(template + ".txt"), IsBodyHtml = false }; var htmlMailDefinition = new MailDefinition { BodyFileName = HostingEnvironment.MapPath(template + ".htm"), IsBodyHtml = true }; MailMessage htmlMessage = htmlMailDefinition.CreateMailMessage(email, replacementDictionary, new Control()); MailMessage textMessage = mailDefinition.CreateMailMessage(email, replacementDictionary, new Control()); AlternateView plainView = AlternateView.CreateAlternateViewFromString(textMessage.Body, null, "text/plain"); AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlMessage.Body, null, "text/html"); var message = new MailMessage { From = new MailAddress(from) }; message.To.Add(new MailAddress(toAddress)); message.Subject = subject; message.AlternateViews.Add(plainView); message.AlternateViews.Add(htmlView); return message; } 

Assuming your Web.config is configured for NetMail, you can call this method from a helper method, for example:

 public bool SendEmail(MailMessage email) { var client = new SmtpClient(); try { client.Send(message); } catch (Exception e) { return false; } return true; } SendMail(createMailMessage(" to@email.com ", " from@email.com ", "Subject", "~/Path/Template")); 
0
source

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


All Articles