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

时间:2013-12-18 11:09:38

标签: vb.net excel email

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

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 个答案:

答案 0 :(得分:4)

脱离我的头顶

' 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()