读取数据库以列在Vb中

时间:2013-12-09 03:46:06

标签: vb.net

该程序假设采用特定列中的值并向每个列发送4封电子邮件。 4封电子邮件中的每一封都是主要的手机公司电子邮件。列表中的数据是电话号码,但我无法实现它。我究竟做错了什么?有什么建议吗?

Imports System.Net.Mail

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'ContactsDataSet.VBQuery' table. You can move, or remove it, as needed.
    Me.VBQueryTableAdapter.Fill(Me.ContactsDataSet.VBQuery)
    ' Set the caption bar text of the form.   
    Me.Text = "Texting From Email"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Try

        Dim Smtp_Server As New SmtpClient
        Dim e_mail As New MailMessage()
        Dim self As New MailAddress("blank@gmail.com")
        Dim strCarriers As New List(Of String)
        Dim colIndex As Integer
        Dim strList As New List(Of String)



        Smtp_Server.UseDefaultCredentials = False
        Smtp_Server.Credentials = New Net.NetworkCredential("blank@gmail.com", "password3")
        Smtp_Server.Port = 587
        Smtp_Server.EnableSsl = True
        Smtp_Server.Host = "smtp.gmail.com"

        strCarriers.Add("@pm.sprint.com")
        strCarriers.Add("@vtext.com")
        strCarriers.Add("@tmomail.net")
        strCarriers.Add("@txt.att.net")
        colIndex = 4


        If rad1.Checked = True Then


            Dim NumToCall As String

            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                ' dont need
                'strList.Add(DataGridView1.Item(colIndex, i).Value.ToString)

                NumToCall = DataGridView1.Item(colIndex, i).Value.ToString
                ' you cal also reference by name:


                For Each item As String In strList

                    For Each Carrier As String In strCarriers
                        ' Send Email
                        e_mail.To.Add(NumToCall & "Carrier")
                        e_mail = New MailMessage()
                        e_mail.From = self
                        e_mail.Subject = txtSubject.Text
                        e_mail.IsBodyHtml = False
                        e_mail.Body = txtMessage.Text
                        Smtp_Server.Send(e_mail)
                    Next

                Next
            Next

        ElseIf rad1.Checked = False Then

                    For Each Carrier As String In strCarriers

                        e_mail = New MailMessage()
                        e_mail.From = self
                        e_mail.To.Add(txtTo.Text + Carrier)
                        e_mail.Subject = txtSubject.Text
                        e_mail.IsBodyHtml = False
                        e_mail.Body = txtMessage.Text
                        Smtp_Server.Send(e_mail)
                    Next
        End If

        MsgBox("Mail Sent")

    Catch error_t As Exception
        MsgBox(error_t.ToString)

    End Try
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Close()
End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    txtMessage.Text = String.Empty
    txtSubject.Text = String.Empty
    txtTo.Text = String.Empty
    rad1.Checked = False

End Sub

结束班

1 个答案:

答案 0 :(得分:1)

您必须删除strList ForLoop块。我想这不再需要了

Dim NumToCall As String

For i As Integer = 0 To DataGridView1.Rows.Count - 1
     NumToCall = DataGridView1.Item(colIndex, i).Value.ToString
     'you cal also reference by name:
     For Each Carrier As String In strCarriers
        ' Send Email
        e_mail.To.Add(NumToCall & "Carrier")
        e_mail = New MailMessage()
        e_mail.From = self
        e_mail.Subject = txtSubject.Text
        e_mail.IsBodyHtml = False
        e_mail.Body = txtMessage.Text
        Smtp_Server.Send(e_mail)
     Next
Next