Results 1 to 15 of 15

Thread: Help with For Each Email Sending

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Help with For Each Email Sending

    I need some help please. I am trying to write code to send emails from a .txt file. The problem I am having is sending an email when there is an invalid formatted emailaddress string. it appears from the documentation that .net has a built in validator and will throw an exception when an address is not in the correct format. I want to be able to either only add valid emailaddress's to my array or skip the invalid address when I start my for each loop. the way my code is now sending stops upon an invalid emailaddress and I am not sure how to write the code properly to continue the For Each and only count the successful emails sent,any help would be appreciated

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            emailadd.Clear()
            GetEmailAdd() 'function to readtable from txt file
            Dim attach As Attachment = Nothing
            If CheckBox1.Checked Then
                Dim filename As String = OpenFileDialog2.FileName
                attach = New Attachment(filename)
            End If
            Dim i As Integer = 0
            Dim pauseTime As Integer = CDbl(txttimesecs.Text) * 1000
            Dim result As DialogResult = MessageBox.Show("There are" & " " & emailadd.Count & " " & "Messages to be Sent", "Send Email", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
            If result = DialogResult.Cancel Then
                Return
            End If
            For Each email In emailadd
                Try
                    Dim sslValue As String = ComboBox1.Text
                    Dim SmtpServer As New SmtpClient()
                    Dim mail As New MailMessage()
                    SmtpServer.Credentials = New Net.NetworkCredential(txtusername.Text, txtpassword.Text)
                    SmtpServer.Port = CDbl(txtserverport.Text)
                    SmtpServer.Host = txtservername.Text
                    SmtpServer.EnableSsl = sslValue
                    mail = New MailMessage()
                    mail.From = New MailAddress(txtfrom.Text)
                    mail.To.Add(email)
                    mail.Subject = txtsubj.Text
                    If CheckBox1.Checked Then
                        mail.Attachments.Add(attach)
                    End If
                    mail.Body = txtmsgbox.Text
                    If btnhtml.Checked = True Then
                        mail.IsBodyHtml = True
                    Else
                        mail.IsBodyHtml = False
                    End If
                    SmtpServer.Send(mail)
                    i += 1
                Catch ex As Exception
                    MsgBox(ex.ToString)
                    MsgBox("No Messages Sent")
                    Return
                End Try
    
                wait(pauseTime)
            Next
            MsgBox(i.ToString & " " & "Messages Sent")
        End Sub
    Code:
     Public Function GetEmailAdd()
            If Table.Rows.Count <= 0 Then
                MsgBox("There is No Data")
            End If
            Dim columnName = txttocol.Text
            If Table.Columns.Contains(columnName) Then
            Else
                MsgBox("Column Name Not Found")
                Return Nothing
            End If
            For Each row As DataRow In Table.Rows
                If Not (row(columnName)) Is Nothing Then
                    emailadd.Add(row(columnName).ToString)
                End If
            Next
            Return emailadd
    
        End Function

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Help with For Each Email Sending

    Your overall code is extremely messy. To simply validate a email we can use regex. I don't know of any net method to validate an email.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object,
    4.                           ByVal e As System.EventArgs) Handles Button1.Click
    5.         ' Using crap like input box jmc for an example ;)
    6.         Dim email As String = InputBox("Email")
    7.         MessageBox.Show(ValidateEmail(email).ToString)
    8.     End Sub
    9.  
    10.  
    11.     Private Function ValidateEmail(ByVal Email As String) As Boolean
    12.         Return System.Text.RegularExpressions.Regex.IsMatch(Email, "^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$",
    13.                                                             System.Text.RegularExpressions.RegexOptions.Compiled Or
    14.                                                             System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    15.     End Function
    16.  
    17. End Class

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with For Each Email Sending

    thanks I know I can use regex but from what I have read using the built in .net validation is better

    my problem is verifying the emailaddress as much as it is cleaning up my code and have the For Each loop continue

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Help with For Each Email Sending

    Define is better? I doubt you know what better is in this situation. Can you point me to this magical net email validation?

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with For Each Email Sending

    What is with the attitude, I came here for help not to be ridiculed. Your right I don't know what is better. If I did I wouldn't be here asking questions, which by the way wasn't about validating the email address is was continuing the For Each loop, something I pointed out both in my original post as well as response.
    Here is a link to another post I read that was advising against using regex and the magical net email validation.
    http://stackoverflow.com/questions/1...-net-framework
    .

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Help with For Each Email Sending

    At a Two second glance the first post was instead of using regex we can use a try catch statement to throw an exception to determine a valid email. This is disgusting abuse. Exception handling is for exceptional circumstances. I suggest you never look at that link again.

    I still cant see this magical net email validation??? There is nothing wrong using regex.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with For Each Email Sending

    Maybe you should take more then a two second glance you might have read what was given as one response

    Don't bother with your own validation. .NET 4.0 has significantly improved validation via the MailAddress class. Just use MailAddress address = new MailAddress(input) and if it throws, it's not valid. If there is any possible interpretation of your input as an RFC 2822 compliant email address spec, it will parse it as such. The regexes above, even the MSDN article one, are wrong because they fail to take into account a display name, a quoted local part, a domain literal value for the domain, correct dot-atom specifications for the local part, the possibility that a mail address could be in angle brackets, multiple quoted-string values for the display name, escaped characters, unicode in the display name, comments, and maximum valid mail address length. I spent three weeks re-writing the mail address parser in .NET 4.0 for System.Net.Mail and trust me, it was way harder than just coming up with some regular expression since there are lots of edge-cases. The MailAddress class in .NET 4.0 beta 2 will have this improved functionality.

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Help with For Each Email Sending

    Last thing i say is what a load of ****, Abuse it it's highest order. Have fun with that link

    Oh and the regex written in that thread can you explain the difference between mine and theres?

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with For Each Email Sending

    I have no idea where you are coming from, it seems like you want an argument? I am here asking questions looking for answers and direction. I simply shared a post I read in my efforts to learn. All you have to do is say what you disagree with I am open to learning for all I know the post I read is totally wrong, again that is why I am here asking

    I have no idea what the difference in the regex is, if I did I doubt I would be here asking questions. Who said anything was wrong with your regex? My God I feel like I am talking to a wall. I wish you would have just stayed away from my post. Now I have to repost in hopes of someone else coming along who might be willing to help me and not argue, please don't offer your help, its more then I can handle LOL

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Help with For Each Email Sending

    Where is the problem exactly?

    For each mail
    If email address is valid then send mail & increment mail count
    Else add mail to list of mails rejected
    Next
    Read list of rejected emails and take any remedial action

    What am I missing?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with For Each Email Sending

    If I had an improperly formatted email address an exception is thrown as expected. Then the "For Each" stops, I wanted it to continue onto the next email address.

    Secondly I was confused by the post I read in the link in post number 5
    I have since implemented the ValidateEmail function

    Ran it this morning and got an SMTP time out error, not sure how many emails were sent before the error

  12. #12
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Help with For Each Email Sending

    If I had an improperly formatted email address an exception is thrown as expected.
    Er yes ... that's why you use something like Regex to validate the address before you allow it to be entered into the email send. I think that's pretty clear in my method.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with For Each Email Sending

    Yes that is how I am doing it now, you asked what "what am I missing?" I was responding to that

    since .net throws an exception, I didn't know if there was a way to continue with the sending of the other emails without using regex

    But like I said I have implemented the regex function and it is working

    except now I got a timeout error

    thanks

  14. #14
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Help with For Each Email Sending

    Well there's nothing you can do about a timeout. That's coming from the server (or at least it should be). You simply have to Catch the error and provide some means of retrying either immediately or at a later time. That's why I suggested you keep a record of mails that were unsuccessful.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with For Each Email Sending

    Thanks apparently bluehost was down yesterday.
    Can you give me an idea how to catch the successful emails sent ?

    I appreciate your help

    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width