在VB.NET中发送批量电子邮件Reciepients电子邮件将由excel上传

Send Bulk Email in VB.NET Reciepients Emails will uploaded by excel

这是我用来给客户发送电子邮件的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Imports System.Net.Mail
Public Class Form1
    Dim file(2) As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim smtpserver As New SmtpClient()
        Dim mail As New MailMessage()
        smtpserver.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox2.Text)
        smtpserver.Host = TextBox3.Text
        smtpserver.Port = TextBox4.Text
        mail = New MailMessage
        mail.From = New MailAddress(TextBox1.Text)
        mail.To.Add(TextBox5.Text)
        mail.To.Add(TextBox12.Text)
        mail.Subject = TextBox6.Text
        mail.Body = TextBox10.Text
        If Not TextBox7.Text = Nothing Then
            Dim attach As New Attachment(TextBox7.Text)
            mail.Attachments.Add(attach)
        End If
        If Not TextBox8.Text = Nothing Then
            Dim attach As New Attachment(TextBox8.Text)
            mail.Attachments.Add(attach)
        End If
        If Not TextBox9.Text = Nothing Then
            Dim attach As New Attachment(TextBox9.Text)
            mail.Attachments.Add(attach)
        End If
        smtpserver.EnableSsl = True
        Try
            smtpserver.Send(mail)
        Catch ex As SmtpException
            MsgBox("either you typed something wrong or something is wrong with the program...most likely it was something you typed so try again")
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        file = Nothing
        OpenFileDialog1.ShowDialog()
        file = OpenFileDialog1.FileNames
        TextBox7.Text = file(0)
        Try
            TextBox8.Text = file(1)
        Catch ex As IndexOutOfRangeException
        End Try
        Try
            TextBox9.Text = file(2)
        Catch ex As IndexOutOfRangeException

        End Try
    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

在这里,我只能发送电子邮件,在这两个文本框中,但我需要向20个客户发送更多的电子邮件,他们的电子邮件存储在Excel文件中。


从我的头顶

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
' Create new Application.
Dim excel As Application = New Application

' Open Excel spreadsheet.
Dim w As Workbook = excel.Workbooks.Open("C:\file.xls")

' Loop over all sheets.
For i As Integer = 1 To w.Sheets.Count

    ' Get sheet.
    Dim sheet As Worksheet = w.Sheets(i)

    ' Get range.
    Dim r As Range = sheet.UsedRange

    ' Load all cells into 2d array.
    Dim array(,) As Object = r.Value(XlRangeValueDataType.xlRangeValueDefault)

    ' Scan the cells.
    If array IsNot Nothing Then

    ' Get bounds of the array.
    Dim bound0 As Integer = array.GetUpperBound(0)
    Dim bound1 As Integer = array.GetUpperBound(1)  


    ' Loop over all elements.
    For j As Integer = 1 To bound0
        For x As Integer = 1 To bound1
        Dim address As String = array(j, x)

                     'Send your email to **address** here

        Next

    Next
    End If
Next

' Close.
w.Close()