Send Email in ASP.Net 2.0 - Feed back Form
using System.Web.Mail;
using System.Net.Mail;
The Send mail functionality is similar to Dotnet 1.1 except for few changes
System.Net.Mail.SmtpClient is used instead of System.Web.Mail.SmtpMail (obsolete in Dotnet 2.0).
System.Net.MailMessage Class is used instead of System.Web.Mail.MailMessage (obsolete in Dotnet 2.0)
The System.Net.MailMessage class collects From address as MailAddress object.
The System.Net.MailMessage class collects To, CC, Bcc addresses as MailAddressCollection.
MailMessage Body Format is replaced by IsBodyHtml
The Code is Self explanatory by itself.
public void SendMailUsingSmtp()
{
SmtpClient smtpclient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
//SMTP Server Details
string smtp = "mail.emailsrvr.com";
string smtpuserid = "smtptest@magnonsolutions.com";
string smtppassword = "smtptest";
string port = "25";
//smtpclient.Host = "localhost";
//smtpclient.Port = 25;
//smtpclient.Host = "smtp.gmail.com";
//smtpclient.Port = 465;
//FROM E-Mail Address
MailAddress fromaddress = new MailAddress(txtsendmail.Text, string.Concat(txtfname.Text, txtlname.Text));
message.From = fromaddress;
//TO E-Mail Address
message.To.Add("yogesh.upreti@magnonsolutions.com");
//CC E-Mail Address
//message.CC.Add("test2@test.com");
//BCC E-Mail Address
message.Bcc.Add("test3@test.com");
//Subject Here
message.Subject = "Feedback";
message.SubjectEncoding = System.Text.Encoding.UTF8;
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;
// Message body content
message.Body = txtfname.Text;
message.BodyEncoding = System.Text.Encoding.UTF8;
//Checkout the Credentitial for smtp mail server
smtpclient.Credentials = new System.Net.NetworkCredential(smtpuserid, smtppassword);
smtpclient.Port = Convert.ToInt32(port);
smtpclient.Host = smtp;
smtpclient.EnableSsl = false;
smtpclient.Send(message);
smtpclient.Send(message);
lblStatus.Visible = true;
lblStatus.Text = "Messgae has been sent successfully.";
}
catch (Exception ex)
{
lblStatus.Visible = true;
lblStatus.Text = "Send Email Failed." + ex.Message;
}
}
Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.
Calling the function from code
MailHelper.SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")
MailHelper.cs
using System.Net.Mail;
public class MailHelper
{
///
/// Sends an mail message
///
///
Sender address
///
Recepient address
///
Bcc recepient
///
Cc recepient
///
Subject of mail message
///
Body of mail message
public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
{
// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage();
// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
mMailMessage.To.Add(new MailAddress(to));
// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
} // Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
} // Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// Send the mail message
mSmtpClient.Send(mMailMessage);
}
}
MailHelper.vb
Imports System.Net.Mail
Public Class MailHelper
''' ''' Sends an mail message
''' ''' Sender address
''' Recepient address
''' Bcc recepient
''' Cc recepient
''' Subject of mail message
''' Body of mail message
Public Shared Sub SendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String)
' Instantiate a new instance of MailMessage
Dim mMailMessage As New MailMessage()
' Set the sender address of the mail message
mMailMessage.From = New MailAddress(from)
' Set the recepient address of the mail message
mMailMessage.To.Add(New MailAddress(recepient))
' Check if the bcc value is nothing or an empty string
If Not bcc Is Nothing And bcc <> String.Empty Then
' Set the Bcc address of the mail message
mMailMessage.Bcc.Add(New MailAddress(bcc))
End If
' Check if the cc value is nothing or an empty value
If Not cc Is Nothing And cc <> String.Empty Then
' Set the CC address of the mail message
mMailMessage.CC.Add(New MailAddress(cc))
End If
' Set the subject of the mail message
mMailMessage.Subject = subject
' Set the body of the mail message
mMailMessage.Body = body
' Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = True
' Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal
' Instantiate a new instance of SmtpClient
Dim mSmtpClient As New SmtpClient()
' Send the mail message
mSmtpClient.Send(mMailMessage)
End Sub
End Class
Web.config xml version="1.0"?>
<configuration>
<system.net>
<mailSettings>
<smtp from="defaultEmail@yourdomain.com">
<network host="smtp.yourdomain.com" port="25" userName="yourUserName" password="yourPassword"/>
smtp>
mailSettings>
system.net>
configuration>