...

The acronym SMTP stands for Simple Mail Transfer Protocol, a program that mail servers use to transmit, receive, and relay outbound email messages between email senders and recipients. So, I wrote a simple SMTP webmail code for you to use. If you are not familiar with the code below, I highly suggest you visit the official Microsoft website first and read the documentation.

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.

Ser

Military Veteran | Software Engineer | Cloud Engineer | & Cybersecurity Enthusiast

By Ser

Military Veteran | Software Engineer | Cloud Engineer | & Cybersecurity Enthusiast

Seraphinite AcceleratorBannerText_Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.