C# MVC Create POST Email Using SMTP WebMail
SMTP WebMail is the usage of the Simple Mail Transfer Protocol (SMTP) in combination with web-based email services, allowing users to send and receive emails using a web interface. SMTP is the standard protocol for sending emails over the Internet, which makes it easier to transmit messages between email servers. In a WebMail environment, users access their email accounts via a web browser rather than a specialized email program, making them handy and available from any device with an Internet connection.
Webmail services, such as Gmail, Yahoo Mail, and Outlook.com, use SMTP for outgoing mail delivery while retrieving incoming mail using protocols such as IMAP (Internet Message Access Protocol) or POP3 (Post Office Protocol). When a user sends an email using a WebMail interface, the message is routed to an SMTP server, which processes it and delivers it to the recipient’s email server. Users may handle their email without having to install any particular software on their devices.
One of the primary benefits of SMTP WebMail is its accessibility; users may check and send emails from any place and device with Internet connectivity. Furthermore, WebMail providers often include advanced features such as spam filtering, integrated calendaring, and contact management. The user-friendly design simplifies email handling, allowing individuals of various technical backgrounds to connect successfully. Overall, SMTP WebMail combines the dependability of email protocols with the convenience of online access, making it an essential component of contemporary digital communication.
Now, let’s go through an example so you can apply this to your projects. The first thing we need to do is create a model for our webmail. Create a .cs file and name it “Contact.”
public class Contact { public string FromName { get; set; } public string FromEmail { get; set; } public string FromDropDown { get; set; } public string Message { get; set; } }
So, in your C# controller, copy and paste the code. Make sure to add “[HttpPost]” and add the required namespace. You will have to provide your email and password in order for this to work. If you are concerned about storing the password safely. You can store it in ConfigurationManager or the web config; depends on what .NET framework you are using. Test it to make sure everything works, if it does, you can customize the response message to your liking. Enjoy!
[HttpPost] public async Task<ActionResult> Contact(Contact model) { try { var body = \"<p>Email From: <b style=\'color:red\'>{0}</b> ({1})</p>What\'s the customer need: <b style=\'color:red\'>{2}</b><p>Message:</p><p>{3}</p>\" + \"<br/>\" + \"<hr>\" + \"<p>{YOUR HEADING}</p>\" + \"<footer>\" + \"<address>\" + \"<p>John Doe, LLC<br/>\" + \"123 P.O. BOX <br/>\" + \"Lawndale, California 90261</p>\" + \"</address>\" + \"<img src=\'{YOUR FOOTER IMAGE}\' />\" + \"</footer>\"; //create the mail message MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress(\"{BUSINESS EMAIL}\"); //IMPORTANT: This must be same as your smtp authentication address. mail.To.Add(\"BUSINESS EMAIL\"); //set the content mail.Subject = \"Alert Email\"; mail.Body = string.Format(body, model.FromName, model.FromEmail, model.FromDropDown, model.Message); //send the message SmtpClient smtp = new SmtpClient(\"mail.{YOUR EMAIL}.com\"); //IMPORANT: Your smtp login email MUST be same as your FROM address. NetworkCredential Credentials = new NetworkCredential(\"{BUSINESS EMAIL}\", \"{EMAIL PASSWORD}\"); smtp.UseDefaultCredentials = false; smtp.Credentials = Credentials; smtp.Port = 25; //alternative port number is 8889 smtp.EnableSsl = false; smtp.Send(mail); ViewBag.Status = \"Message Sent Successfully!\"; // Create notification } //Fails catch (Exception) { ViewBag.Status = \"Problem while sending email, Please check details.\"; } return View(model); }
For more information, please visit the official Microsoft website.
Want to read more about the Top 5 Programming Languages? Click here.