程序使用G-mail帐户从您的计算机发送短信。此代码工作正常,但需要一些总时间,并且它当前正在向4个不同的电子邮件地址发送文本,只有一个正常工作。所以,想法或帮助会很棒。
帮助编写一个循环或类似的东西来简化我的代码?
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 Sprint As String
Dim Verizon As String
Dim TMobile As String
Dim ATT As String
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("blank@gmail.com", "password")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
Sprint = "@pm.sprint.com"
Verizon = "@vtext.com"
TMobile = "@tmomail.net"
ATT = "@txt.att.net"
If txtTo.Text.Length > 1 Then
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add(txtTo.Text + Sprint) 'Spint
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add(txtTo.Text + ATT) ' AT&T
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add(txtTo.Text + Verizon) ' Verizon
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add(txtTo.Text + TMobile) ' T-mobial
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
End If
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
答案 0 :(得分:1)
这无助于加快您的发送速度...但它会使用您请求的循环减少您的代码行...
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)
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("blank@gmail.com", "password")
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")
If txtTo.Text.Length > 1 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
我可以建议使用线程或异步调用来同时发送多封电子邮件吗?