A Class for sending e-mails

Rate It (1)

Last post 01-04-2009 1:44 PM by rami_nassar. 14 replies.

Sort Posts:

  • A Class for sending e-mails

    12-03-2008, 5:57 PM
    • Contributor
      3,472 point Contributor
    • rami_nassar
    • Member since 10-07-2008, 3:01 AM
    • U.A.E.
    • Posts 788

    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.
  • Re: A Class for sending e-mails

    12-04-2008, 8:57 AM
    • Member
      22 point Member
    • HelloBlues
    • Member since 10-17-2008, 8:47 AM
    • Posts 8

     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

  • Re: A Class for sending e-mails

    12-04-2008, 7:54 PM
    • Contributor
      3,472 point Contributor
    • rami_nassar
    • Member since 10-07-2008, 3:01 AM
    • U.A.E.
    • Posts 788

    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 :)

    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.
  • Re: A Class for sending e-mails

    12-09-2008, 5:52 AM
    Answer
    • All-Star
      62,829 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,265
    • TrustedFriends-MVPs

     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
    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.
  • Re: A Class for sending e-mails

    12-10-2008, 5:52 PM
    • Contributor
      3,472 point Contributor
    • rami_nassar
    • Member since 10-07-2008, 3:01 AM
    • U.A.E.
    • Posts 788

    Thanks man for your feedback and notes

    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.
  • Re: A Class for sending e-mails

    12-17-2008, 8:53 AM
    • All-Star
      62,829 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,265
    • TrustedFriends-MVPs

     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.

    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.
  • Re: A Class for sending e-mails

    12-17-2008, 1:53 PM
    • Contributor
      3,472 point Contributor
    • rami_nassar
    • Member since 10-07-2008, 3:01 AM
    • U.A.E.
    • Posts 788

    Dear TATWORTH

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

    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.
  • Re: A Class for sending e-mails

    12-19-2008, 2:50 PM
    • Contributor
      3,472 point Contributor
    • rami_nassar
    • Member since 10-07-2008, 3:01 AM
    • U.A.E.
    • Posts 788

    The post now in  Announcements...

    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.
  • Re: A Class for sending e-mails

    12-20-2008, 4:39 AM
    • All-Star
      62,829 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,265
    • TrustedFriends-MVPs

     Rami

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

    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.
  • Re: A Class for sending e-mails

    01-04-2009, 5:47 AM
    • Contributor
      4,623 point Contributor
    • Joël Hébert
    • Member since 07-20-2005, 6:07 PM
    • Ottawa Canada
    • Posts 679
    • TrustedFriends-MVPs

    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.

    Joël Hébert [MVP ASP.NET]

    Opulent ASP Development Inc.
    www.opulentasp.com
    Ottawa,Canada

    Click "Mark as Answer" on the posts that helped you to help future readers to get the solutions
  • Re: A Class for sending e-mails

    01-04-2009, 6:03 AM
    • Contributor
      3,472 point Contributor
    • rami_nassar
    • Member since 10-07-2008, 3:01 AM
    • U.A.E.
    • Posts 788

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

    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.
  • Re: A Class for sending e-mails

    01-04-2009, 6:29 AM
    • All-Star
      62,829 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,265
    • TrustedFriends-MVPs

     >I know that I left the exception handling

    Very sensible considering the divergence of opinion over doing it.

    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.
  • Re: A Class for sending e-mails

    01-04-2009, 7:47 AM
    • Contributor
      3,472 point Contributor
    • rami_nassar
    • Member since 10-07-2008, 3:01 AM
    • U.A.E.
    • Posts 788

    whatever...

    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.
  • Re: A Class for sending e-mails

    01-04-2009, 8:09 AM
    • Member
      28 point Member
    • Sparks01
    • Member since 12-29-2008, 7:25 AM
    • Posts 31
    Nice work, I like it.
  • Re: A Class for sending e-mails

    01-04-2009, 1:44 PM
    • Contributor
      3,472 point Contributor
    • rami_nassar
    • Member since 10-07-2008, 3:01 AM
    • U.A.E.
    • Posts 788

    Thanks Sparks... :)

    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.
Page 1 of 1 (15 items)