Basic examples of sending mail using System.Net.Mail in C# and VB.NET. Settings that require updating highlighted in red.
Trust Level and TLS Requirements
The scripting will require that the Trust Level is set to Full in a site's web.config, syntax example
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<trust level="Full"/>
</system.web>
</configuration>
TLS 1.2 is also required in order to make an encrypted connection to the mail server. If TLS 1.2 is not already forced for a site / application, bootstrapping code needs to be added to the mail scripting with syntax differing slightly between C# and VB.NET. Highlighted in red in the below scripting examples.
C#
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
VB.NET
System.Net.ServicePointManager.SecurityProtocol = DirectCast(3072, System.Net.SecurityProtocolType)
C# Example
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test email sent from System.Net.Mail";
mail.Body = "Mail test";
mail.Headers.Add("Message-Id",
String.Format("<{0}@{1}>",
Guid.NewGuid().ToString(),
"domain.com"));
SmtpClient smtp = new SmtpClient("sm##.internetmailserver.net");
NetworkCredential Credentials = new NetworkCredential("[email protected]", "password");
smtp.Credentials = Credentials;
smtp.EnableSsl = true;
smtp.Port = 25;
smtp.Send(mail);
lblMessage.Text = "Mail Sent";
}
</script>
<html>
<body>
<form runat="server">
<asp:Label id="lblMessage" runat="server"></asp:Label>
</form>
</body>
</html>
VB.NET Example
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net.Mail" %>
<script runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
System.Net.ServicePointManager.SecurityProtocol = DirectCast(3072, System.Net.SecurityProtocolType)
Dim strFrom = "[email protected]"
Dim strTo = "[email protected]"
Dim Mail As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))
Mail.BodyEncoding = Encoding.Default
Mail.Subject = "Test email sent from System.Net.Mail"
Mail.Body = "Mail test"
Mail.Headers.Add("Message-Id",
String.Format("<{0}@{1}>",
Guid.NewGuid().ToString(),
"domain.com"))
Mail.Priority = MailPriority.High
Mail.IsBodyHtml = True
Dim SmtpMail As New SmtpClient
Dim basicAuthenticationInfo As New System.Net.NetworkCredential("[email protected]", "password")
SmtpMail.Host = "sm##.internetmailserver.net"
SmtpMail.UseDefaultCredentials = False
SmtpMail.Credentials = basicAuthenticationInfo
SmtpMail.EnableSsl = true
SmtpMail.Port = 25
SmtpMail.Send(Mail)
lblMessage.Text = "Mail Sent"
End Sub
</script>
<html>
<body>
<form runat="server">
<asp:Label id="lblMessage" runat="server"></asp:Label>
</form>
</body>
</html>