Fun with collections



  • Something I came across in one of the applications i have to maintain. I am sure there could be a reason to keep moving those e-mail addresses around but i definately am not smart enough to find a reason why just yet.

    Private Sub SendEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendEmail.Click
        Dim emailDB As New EmailDB
        Dim strEmail As String = ""
        Dim drEmail As SqlDataReader
    
        drEmail = emailDB.GetAllEmails()
        ' Snip
        Do While drEmail.Read()
            strEmail &= drEmail.Item("strEmail") & ";"
        Loop
        ' Snip
        Dim arrRecipients() As String
        ' snip
        arrRecipients = Split(strEmail, ";")\
        'snip
        For i = 0 To UBound(arrRecipients) - 1
            EmailAdressen.Add(arrRecipients(i).ToString)
        Next
        ' Snip
        For Each EmailAddress In EmailAdressen
            ' snip: Create and send e-mails
        Next
        ' snip
    
    End Sub

    And yes, i know, TRWTF's are of course the use of VB.net 2003 and the forum software



  • @coentje said:

    and the forum software

    Hey — at least your post has linebreaks and monospaced code.



  • It does but i felt obligated to say it :-)@Spectre said:

    @coentje said:
    and the forum software

    Hey — at least your post has linebreaks and monospaced code.

    It does but i felt obligated to say it :-)



  • @coentje said:

    strEmail &= drEmail.Item("strEmail") & ";"
    @coentje said:
    arrRecipients = Split(strEmail, ";")

    You could want to do something like that, if the strEmail items can contain multiple addresses separated by ';'. Of course you could also split the individual strEmail-s or restructure your EmailDB in this case.



  • I just dealt with something similar. Some customers wanted to get e-mails sent to multiple addresses, so they put in two addresses, separated by ';'. Of course System.Net.Mail.MailAddress does not allow something like this, and only takes the first one. I had to write my own e-mail address classes that could deal with this.

    In this code it would be much better to split the string first and put all addresses in a list or something.

    But TRWTF is that you can have a semicolon in the e-mail address when you use a quoted address: "semicolon;jerk"@foo.com



  • @SlyEcho said:

    But TRWTF is that you can have a semicolon in the e-mail address when you use a quoted address: "semicolon;jerk"@foo.com

    TRWTF is email addresses in general.  The insane rules that govern the local part make it impossible to validate all email addresses by regex and trying to write your own parser is a PITA.  Luckily, de facto standards for MTAs have effectively made the more obscure addresses unusable.



  • @morbiuswilters said:

    TRWTF is email addresses in general.  The insane rules that govern the local part make it impossible to validate all email addresses by regex and trying to write your own parser is a PITA.
    There's a simple solution to that problem. (as suggested by "Mastering Regular Expressions" First edition, Appendix B, (Friedl / O'Reilly))

    Just try to send email to the address.  You should be doing closed-loop opt-in anyway.  If you can send email to the address, and get an opt-in response, then it's valid.  No parsing necessary.



  • @merreborn said:

    @morbiuswilters said:

    TRWTF is email addresses in general.  The insane rules that govern the local part make it impossible to validate all email addresses by regex and trying to write your own parser is a PITA.
    There's a simple solution to that problem. (as suggested by "Mastering Regular Expressions" First edition, Appendix B, (Friedl / O'Reilly))

    Just try to send email to the address.  You should be doing closed-loop opt-in anyway.  If you can send email to the address, and get an opt-in response, then it's valid.  No parsing necessary.

    I don't deal with "sign up to get our newsletter" junk, but that's a pretty decent idea, so long as you know your MTA is bullet-proof.  You don't want the piece of crap having a buffer-overflow in some obscure corner of the address-parsing logic.  Anyway, I deal with mail services so that doesn't work for me.


Log in to reply