locked
A Class for sending e-mails RRS feed

  • Question

  • User1589032278 posted

    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...

    Wednesday, December 3, 2008 5:57 PM

Answers

  • User-319574463 posted

     Dear Rami

       A helpful and interesting class!

       I would suggest the following:

    • Include a copyright notice - an LGPL notice for example shows that although the code was written by a particular author (or group of authors), it may be freely used.
    • Include XML documentation - this drives intellisense which help the developer usign your code.
    • Put your code onto either CodeProject and/or CodePlex with just a pointer posted here
    • Spell check your comments using the ComponentOne Community Spell Checker http://www.componentone.com/SuperProducts/IntelliSpell/
    • Where functions can be tested, include unit test code. for an example see http://www.CodePlex.Com/CommonData
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 9, 2008 5:52 AM

All replies

  • User1575591699 posted

     Hi rami_nassar

    Am not sure what your question is but this workes for me

    In your web config --

            <mailSettings>
                <smtp from="your_email@address.com"> ------ create an user on domain that password never expires
                    <network host ="exchange server name goes here" port ="port number" userName ="username" password ="Password123"/>
                    
                </smtp>
            </mailSettings>

     

    In your code

    import System.Net.Mail


            Dim mm As New MailMessage()
           

                mm.From = New MailAddress("from@email.com")
                mm.To.Add(New MailAddress(("to@email.com")))
            

                mm.Subject = "Test Mail"
                mm.IsBodyHtml = True

                mm.Body = mm.Body & 'Body text goes here'
               
              


                mm.Priority = MailPriority.Normal

                Dim sendr As New SmtpClient()
                sendr.Send(mm)






     Hope this helps

    Thursday, December 4, 2008 8:57 AM
  • User1589032278 posted

    dear HelloBlues

    there is no question here, its just i wanted to share my class with you... & if you had any new creative idea to imrove it, share it with us :)

    Thursday, December 4, 2008 7:54 PM
  • User-319574463 posted

     Dear Rami

       A helpful and interesting class!

       I would suggest the following:

    • Include a copyright notice - an LGPL notice for example shows that although the code was written by a particular author (or group of authors), it may be freely used.
    • Include XML documentation - this drives intellisense which help the developer usign your code.
    • Put your code onto either CodeProject and/or CodePlex with just a pointer posted here
    • Spell check your comments using the ComponentOne Community Spell Checker http://www.componentone.com/SuperProducts/IntelliSpell/
    • Where functions can be tested, include unit test code. for an example see http://www.CodePlex.Com/CommonData
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 9, 2008 5:52 AM
  • User1589032278 posted

    Thanks man for your feedback and notes

    Wednesday, December 10, 2008 5:52 PM
  • User-319574463 posted

     Rami

        One other note for you - after you have posted your code to wherever, make an Announcement at http://forums.asp.net/11.aspx and be sure (unlike my first time today) to include a reference to where the project has been loaded.

    Wednesday, December 17, 2008 8:53 AM
  • User1589032278 posted

    Dear TATWORTH

    Really thank you for your interests & your notes....

    Wednesday, December 17, 2008 1:53 PM
  • User1589032278 posted

    The post now in  Announcements...

    Friday, December 19, 2008 2:50 PM
  • User-319574463 posted

     Rami

        For the benefit of those reading this thread, please post the URL(s) here as well.

    Saturday, December 20, 2008 4:39 AM
  • User-1831161647 posted

    thanks rami, only thing i would add is exception handling, but I see that you probably left it out so people can add what they want...perhaps that and a loggin mechanisn, simple one.

    Sunday, January 4, 2009 5:47 AM
  • User1589032278 posted

    thanks joel for your note.....

    I know that I left the exception handling.. when I put this post its just like a start for beginers... you can modify it as how you see it will make it better, & I hope if you wanted to re-post it here..

    Sunday, January 4, 2009 6:03 AM
  • User-319574463 posted

     >I know that I left the exception handling

    Very sensible considering the divergence of opinion over doing it.

    Sunday, January 4, 2009 6:29 AM
  • User1589032278 posted

    whatever...

    Sunday, January 4, 2009 7:47 AM
  • User-921510314 posted
    Nice work, I like it.
    Sunday, January 4, 2009 8:09 AM
  • User1589032278 posted

    Thanks Sparks... :)

    Sunday, January 4, 2009 1:44 PM