mike_980
mike_980

Reputation: 23

lists in vb.net, struggling with structure

I'm having a bit of trouble with a list in vb.net.

Basically what I am trying to do is send an email and the user can select from 4 options to send a carbon copy to. I started out with a series of if statements checking to see if a checkbox was checked and if it was then it added it to CC, but that seems to only ever add one. So I created ccList, the idea is it creates a list of strings and adds that to cc instead. I'm not sure if it will work but if I add a break in the second, third or fourth if, it doesn't fall into it and only ever passes through the first (if every check box is true). If I only check one then it falls into that one correctly.

 If FSEmailAddress <> "" Then
        Dim OutlookMessage As outlook.MailItem
        Dim AppOutlook As New outlook.Application
        Dim ccList As List(Of String) = Nothing
        Try

OutlookMessage = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
                Dim Recipents As outlook.Recipients = OutlookMessage.Recipients
            Recipents.Add(FSEmailAddress)

            If email1.Checked = True Then
                ccList.Add("[email protected]")
            End If

            If email2.Checked = True Then
                ccList.Add("[email protected]; ")
            End If

            If email3.Checked = True Then
                ccList.Add("[email protected]; ")
            End If

            If email4.Checked = True Then
                ccList.Add("[email protected]; ")
            End If

            OutlookMessage.CC = ccList.ToString

            OutlookMessage.Subject = responseVIN.Text & " was sent back to you by " & GetUserName()
            'link = archive_dir & responseVIN.Text
            OutlookMessage.Body = responseVIN.Text & " was returned To you" & vbCrLf & "Navigate To the following location To view the comments In the ReadMe file:" & vbCrLf & vbCrLf & archive_dir & responseVIN.Text & vbCrLf & vbCrLf & resAdvice.Text
            OutlookMessage.BodyFormat = outlook.OlBodyFormat.olFormatHTML
            OutlookMessage.Send()

Upvotes: 0

Views: 51

Answers (1)

mike_980
mike_980

Reputation: 23

For anybody interested:

Here is the solution to both of the problems I was having:

        Dim FSEmailAddress As String = ""
    Dim link As String = ""
    Dim ccJoin As String = ""

    If FSEngineerName = "Email 1" Then
        FSEmailAddress = "[email protected]"
    ElseIf FSEngineerName = "Email 2" Then
        FSEmailAddress = "[email protected]"
    ElseIf FSEngineerName = "Email 3" Then
        FSEmailAddress = "[email protected]"
    ElseIf FSEngineerName = "Email 4" Then
        FSEmailAddress = "[email protected]"
    End If

    If FSEmailAddress <> "" Then
        Dim OutlookMessage As outlook.MailItem
        Dim AppOutlook As New outlook.Application
        Dim ccList As New List(Of String)
        Try

            OutlookMessage = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
            Dim Recipents As outlook.Recipients = OutlookMessage.Recipients

            Recipents.Add(FSEmailAddress)

        If email1.Checked Then
            ccList.Add("[email protected]")
        End If

        If email2.Checked Then
            ccList.Add("[email protected]; ")
        End If

        If email3.Checked Then
            ccList.Add("[email protected]; ")
        End If

        If email4.Checked Then
            ccList.Add("[email protected]; ")
        End If

            ccJoin = String.Join("; ", ccList.ToArray())

            OutlookMessage.CC = ccJoin

            OutlookMessage.Subject = responseVIN.Text & " was sent back to you by " & GetUserName()
            OutlookMessage.Body = responseVIN.Text & " was returned To you" & vbCrLf & "Navigate To the following location To view the comments In the ReadMe file:" & vbCrLf & vbCrLf & archive_dir & responseVIN.Text & vbCrLf & vbCrLf & resAdvice.Text
            OutlookMessage.BodyFormat = outlook.OlBodyFormat.olFormatHTML
            OutlookMessage.Send()
        Catch ex As Exception
            MessageBox.Show("Mail could Not be sent")
        Finally
            OutlookMessage = Nothing
            AppOutlook = Nothing
        End Try
    End If

Upvotes: 1

Related Questions