We help IT Professionals succeed at work.

ASP.net Email  All or none

Murray Brown
Murray Brown asked
on
120 Views
Last Modified: 2018-08-28
Hi

In my ASP.net web app I use the following code to send an emails. My problem is that I need to send up to ten emails at a time as
and I need them all to either go through or not. Is it possible to set up emails so that if one of the ten fails
they all fail.


    Public Function oSendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc As String,
                                 ByVal cc As String, ByVal subject As String, ByVal body As String,
                                 ByVal blnUseHTML As Boolean) As String


        Try

            ' 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
            If blnUseHTML = True Then
                mMailMessage.IsBodyHtml = True
            Else
                mMailMessage.IsBodyHtml = False
            End If

            ' 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)
            oSendMailMessage = "Email Success!"
        Catch ex As Exception
            oSendMailMessage = "Email Not Sent! " & ex.Message
        End Try
    End Function

Open in new window

Comment
Watch Question

Senior Consultant
CERTIFIED EXPERT
Awarded 2017
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks very much
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.