this is a class I had created for sending e-mails.. had three type of constractor and two methods..
1 Imports System.Net.Mail
2
3 'TODO TRY CATCH
4 Public Class MailHelper
5
6 Private _smtpClient As New SmtpClient
7
8 #Region "Constractors"
9
10 Public Sub New()
11 _smtpClient = New SmtpClient()
12 End Sub
13
14 Public Sub New(ByVal hostName As String)
15 _smtpClient = New SmtpClient(hostName)
16 End Sub
17
18 Public Sub New(ByVal hostName As String, ByVal port As Integer)
19 _smtpClient = New SmtpClient(hostName, port)
20 End Sub
21
22 #End Region
23
24
25 ''' <summary>
26 ''' Sends an mail message
27 ''' </summary>
28 ''' <param name="from">Sender address</param>
29 ''' <param name="recepient">Recepient address</param>
30 ''' <param name="bcc">Bcc recepient</param>
31 ''' <param name="cc">Cc recepient</param>
32 ''' <param name="subject">Subject of mail message</param>
33 ''' <param name="body">Body of mail message</param>
34 Public Sub SendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, _
35 ByVal subject As String, ByVal body As String)
36 ' Instantiate a new instance of MailMessage
37 Dim mMailMessage As New MailMessage()
38
39 ' Set the sender address of the mail message
40 mMailMessage.From = New MailAddress(from)
41 ' Set the recepient address of the mail message
42 mMailMessage.To.Add(New MailAddress(recepient))
43
44 ' Check if the bcc value is null or an empty string
45 If Not bcc Is Nothing And bcc <> String.Empty Then
46 ' Set the Bcc address of the mail message
47 mMailMessage.Bcc.Add(New MailAddress(bcc))
48 End If
49
50 ' Check if the cc value is null or an empty value
51 If Not cc Is Nothing And cc <> String.Empty Then
52 ' Set the CC address of the mail message
53 mMailMessage.CC.Add(New MailAddress(cc))
54 End If
55
56 ' Set the subject of the mail message
57 mMailMessage.Subject = subject
58 ' Set the body of the mail message
59 mMailMessage.Body = body
60
61 ' Secify the format of the body as HTML
62 mMailMessage.IsBodyHtml = True
63 ' Set the priority of the mail message to normal
64 mMailMessage.Priority = MailPriority.Normal
65
66 ' Instantiate a new instance of SmtpClient
67 'Dim mSmtpClient As New SmtpClient()
68 ' Send the mail message
69 'mSmtpClient.Send(mMailMessage)
70 _smtpClient.Send(mMailMessage)
71 End Sub
72 ''' <summary>
73 ''' Sends an mail message
74 ''' </summary>
75 ''' <param name="from">Sender address</param>
76 ''' <param name="recepient">Recepient address</param>
77 ''' <param name="bcc">Bcc Array Of recepients</param>
78 ''' <param name="cc">Cc Array Of recepients</param>
79 ''' <param name="subject">Subject of mail message</param>
80 ''' <param name="body">Body of mail message</param>
81 Public Sub SendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc() As String, ByVal cc() As String, _
82 ByVal subject As String, ByVal body As String)
83 ' Instantiate a new instance of MailMessage
84 Dim mMailMessage As New MailMessage()
85
86 ' Set the sender address of the mail message
87 mMailMessage.From = New MailAddress(from)
88 ' Set the recepient address of the mail message
89 mMailMessage.To.Add(New MailAddress(recepient))
90
91 ' Check if the bcc value is null or an empty string
92 Dim i As Integer
93 For i = 0 To bcc.Length - 1
94 If Not bcc Is Nothing And bcc(i) <> String.Empty Then
95 ' Set the Bcc address of the mail message
96 mMailMessage.Bcc.Add(New MailAddress(bcc(i)))
97 End If
98 Next
99
100
101 ' Check if the cc value is null or an empty value
102 Dim x As Integer
103 For x = 0 To cc.Length - 1
104 If Not cc Is Nothing And cc(x) <> String.Empty Then
105 ' Set the CC address of the mail message
106 mMailMessage.CC.Add(New MailAddress(cc(x)))
107 End If
108 Next
109
110
111 ' Set the subject of the mail message
112 mMailMessage.Subject = subject
113 ' Set the body of the mail message
114 mMailMessage.Body = body
115
116 ' Secify the format of the body as HTML
117 mMailMessage.IsBodyHtml = True
118 ' Set the priority of the mail message to normal
119 mMailMessage.Priority = MailPriority.Normal
120
121 ' Instantiate a new instance of SmtpClient
122 'Dim mSmtpClient As New SmtpClient()
123 ' Send the mail message
124 'mSmtpClient.Send(mMailMessage)
125 _smtpClient.Send(mMailMessage)
126 End Sub
127
128 End Class
129
Hope to see your feedback and comments...
Regards...
Nassar, Rami (MCP, MCTS)
My Blog ||
E-Mail Don't forget to click "Mark as Answer" on the post that helped you.This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.