HOW TO: Send email using System.Net.Mail - preCharge Forums
It shows that you are unregistered. Please register with us by clicking Here
preCharge Forums


Nav Green LeftNav Right
preCharge Forums > Website Design & Development > Programming > .NET » HOW TO: Send email using System.Net.Mail


Reply
Tcat Right
 
LinkBack Thread Tools Display Modes Tcat Right
Old 07-06-2006   #1 (permalink)
shawn
Junior Member
 
Join Date: Jul 2006
Age: 38
Posts: 4
Default HOW TO: Send email using System.Net.Mail

Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.
Download C# System.Net.Mail Helper
Download VB.NET System.Net.Mail Helper
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
{
/// <summary>
/// Sends an mail message
/// </summary>
/// <param name="from">Sender address</param>
/// <param name="to">Recepient address</param>
/// <param name="bcc">Bcc recepient</param>
/// <param name="cc">Cc recepient</param>
/// <param name="subject">Subject of mail message</param>
/// <param name="body">Body of mail message</param>
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
''' <summary>
''' Sends an mail message
''' </summary>
''' <param name="from">Sender address</param>
''' <param name="recepient">Recepient address</param>
''' <param name="bcc">Bcc recepient</param>
''' <param name="cc">Cc recepient</param>
''' <param name="subject">Subject of mail message</param>
''' <param name="body">Body of mail message</param>
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>
This is what I could do. But, the script is not running. Please help.
Thanks
Shawn
shawn is offline   Reply With Quote


Old 02-11-2008   #2 (permalink)
TraderMo
Junior Member
 
Join Date: Feb 2008
Posts: 15
Default Re: HOW TO: Send email using System.Net.Mail

Thanks for the Code
it's reallly help
such kind of Code help a lot
thanks
TraderMo is offline   Reply With Quote

Old 02-11-2008   #3 (permalink)
Marksmen
Junior Member
 
Join Date: Feb 2008
Posts: 10
Default Re: HOW TO: Send email using System.Net.Mail

Thanks for the code!!
Props
Looks very good
you design it yourself?
Wonferful
Marksmen is offline   Reply With Quote

Old 02-17-2008   #4 (permalink)
Subesh
Moderator
 
Join Date: Feb 2008
Posts: 249
Default Re: HOW TO: Send email using System.Net.Mail

Thanks for the tutorial. Kind of useful to me although its going to take me some time to understand how it works. Lol.
__________________
File Hosting Like no other
Subesh is offline   Reply With Quote

Old 05-15-2008   #5 (permalink)
driansmith
Junior Member
 
Join Date: May 2008
Posts: 1
Default Re: HOW TO: Send email using System.Net.Mail

Hiya,
Could anyone explain to a non-VB programmer exactly what "VB.NET System.Net.Mail Helper" is and how it should be used?
I am NOT a programmer.
Thanks.
driansmith is offline   Reply With Quote

Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Does Google Send Checks Anywhere? GuyKels Google 10 06-20-2007 06:47 PM
How to send cookies to the client Tom Tom .NET 1 06-13-2007 11:02 PM
What's the difference between the System.Array.CopyTo() and System.Array.Clone()? agaba175 .NET 0 08-01-2006 04:15 PM
What! No E-mail? anusha All Things General 0 07-22-2006 02:45 AM


footer left
All times are GMT. The time now is 03:35 AM.

DISCLAIMER: preCharge Risk Management is not responsible for any opinions, advice or comments expressed on the preCharge Community Forums.
preCharge® is a registered trademark of preCharge Risk Management | chargeback protection | Merchant Account Blog

Powered by vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC6

Healthy Recipes | Cheap Car Insurance | Consumer information | CreditCards | Internet security software

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49