我想发送电子邮件到多个地址,我为它创建了一个功能。
这是我的功能:
Public Function sendMail(IDnums As String, emailAdd As String)
con.Open()
Dim adapt As MySqlDataAdapter
Dim dtable As DataTable
Dim cmd As MySqlCommand
adapt = New MySqlDataAdapter("SELECT * FROM tblRequestPasscode", con)
dtable = New DataTable
adapt.Fill(dtable)
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("asukaliboguidanceoffice@gmail.com", "S1F6u1d@nc3Syst3m")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress("asukaliboguidanceoffice@gmail.com")
e_mail.To.Add(emailAdd) <<---------------------<<<<
e_mail.Subject = "Requested Passcode"
e_mail.IsBodyHtml = False
e_mail.Body = "Your requested passcode is " + final.ToString + ". Use this along with your ID Number to fully access ASU-CIT Guidance System."
Smtp_Server.Send(e_mail)
cutConn()
Catch error_t As Exception
MsgBox("Connection error. Please check you internet connection then try again.", MsgBoxStyle.Critical, "Connection Error!")
cutConn()
End Try
con.Open()
Try
cmd = New MySqlCommand
With cmd
.Connection = con
.CommandText = "UPDATE tblRequestPasscode SET Status ='0', Passcode ='" & final.ToString & "' WHERE IDNo ='" & IDnums.ToString & "'"
.ExecuteNonQuery()
End With
Catch ex As Exception
MsgBox("Error in updating passcode.", MsgBoxStyle.Critical, "")
cutConn()
End Try
final = String.Empty
cutConn()
Return IDnums = "" And emailAdd = ""
End Function
我收到错误:e_mail.To.Add(emailAdd)
&#34;指定的字符串不是电子邮件地址所需的格式。&#34;
如果是这样的话,它会工作:e_mail.To.Add(&#34; myemail@gmail.com")
如何解决这个问题?提前谢谢。
答案 0 :(得分:0)
你检查过vb论坛:Sending Multiple emails
只要以逗号分隔,您就可以添加多个电子邮件地址。
答案 1 :(得分:0)
您可以使用以下概念使用VBA进行此操作。
在Sheets(“Sheet1”)中创建一个列表:
在A栏:人民的名字 在B列:电子邮件地址 在C列中:Z:像这样的文件名C:\ Data \ Book2.xls(不必是Excel文件)
宏将遍历“Sheet1”中的每一行,如果B列中有电子邮件地址 和C列中的文件名:Z它将创建一个包含此信息的邮件并发送。
Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range
Dim FileCell As Range
Dim rng As Range
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set sh = Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
'Enter the path/file names in the C:Z column in each row
Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
If cell.Value Like "?*@?*.?*" And _
Application.WorksheetFunction.CountA(rng) > 0 Then
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = cell.Value
.Subject = "Testfile"
.Body = "Hi " & cell.Offset(0, -1).Value
For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
If Trim(FileCell) <> "" Then
If Dir(FileCell.Value) <> "" Then
.Attachments.Add FileCell.Value
End If
End If
Next FileCell
.Send 'Or use .Display
End With
Set OutMail = Nothing
End If
Next cell
Set OutApp = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub