1. Home
  2. Windows
  3. Sending emails via .NET

Sending emails via .NET

To send e-mail using ASP.NET, you can use the System.Net.Mail library. Here you will find examples of code that can be used for this.

Send a text message

If you want to send e-mail messages, formatted as plain text, through ASP.NET, you need to use the System.Net.Mail library. Here you can access some simple functions of the MailMessage and SMTPClient classes.

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Net.Mail" %>
<%
 MailMessage mail = new MailMessage();
 mail.From = new MailAddress("info@mindoman.se");
 mail.To.Add("min.epost@mindoman.se");
 mail.Subject = "Testmeddelande";
 mail.Body = "Detta är innehållet i meddelandet.";
 SmtpClient smtp = new SmtpClient("smtp.websupport.se");
 smtp.Port = 587;
 try
 {
 smtp.Send(mail);
 }
 catch (Exception ex)
 {
 Response.Write("Caught exception: " + ex.ToString());
 }
%>

Send HTML message

It is easy to send the message as HTML instead. All you need to do is set mail.IsBodyHtml to true and then use HTML in mail.Body.

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Net.Mail" %>
<%
 MailMessage mail = new MailMessage();
 mail.From = new MailAddress("info@mindoman.se");
 mail.To.Add("min.epost@mindoman.se");
 mail.Subject = "Testmeddelande";
 mail.Body = "<em style=\"color: #f11;\">Detta är innehållet i meddelandet.</em>";
 mail.IsBodyHtml = true;
 SmtpClient smtp = new SmtpClient("smtp.websupport.se");
 smtp.Port = 587;
 try
 {
 smtp.Send(mail);
 }
 catch (Exception ex)
 {
 Response.Write("Caught exception: " + ex.ToString());
 }
%>

Send emails with an authenticated user

When sending emails, it is recommended to authenticate to the SMTP server to avoid the message being classified as spam. This requires the inclusion of the System.Net library containing the NetworkCredential class.

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<%
 MailMessage mail = new MailMessage();
 mail.From = new MailAddress("info@mindoman.se");
 mail.To.Add("min.epost@mindoman.se");
 mail.Subject = "Testmeddelande";
 mail.Body = "<em style=\"color: #f11;\">Detta är innehållet i meddelandet.</em>";
 mail.IsBodyHtml = true;
 SmtpClient smtp = new SmtpClient("smtp.websupport.se");
 smtp.Port = 465;
 smtp.Credentials = new NetworkCredential("info@mindoman.se", "lösenord");
 try
 {
 smtp.Send(mail);
 }
 catch (Exception ex)
 {
 Response.Write("Caught exception: " + ex.ToString());
 }
%>

Send email with authenticated user + SSL

To enable SSL, use the smtp.EnableSsl component set to true.

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<%
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress("info@mindoman.se");
    mail.To.Add("min.epost@mindoman.se");
    mail.Subject = "Testmeddelande";
    mail.Body = "<em style=\"color: #f11;\">Detta är innehållet i meddelandet.</em>";
    mail.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient("smtp.websupport.se");
    smtp.Port = 465;
    smtp.Credentials = new NetworkCredential("info@mindoman.se", "lösenord");
    smtp.EnableSsl = true;
    try
    {
        smtp.Send(mail);
    }
    catch (Exception ex)
    {
        Response.Write("Caught exception: " + ex.ToString());
    }
%>
Updated on 25. March 2024
Was this article helpful?

Related Articles