0

我在这里有一个使用 SMTP 的工作代码,我使用 gmail 作为我的服务器,但我想加快发送速度。我尝试将超时设置为 500,但它仍然很慢。

这是我的完整代码:

Function SendEmail(ByVal Recipients As List(Of String), _
                  ByVal FromAddress As String, _
                  ByVal Subject As String, _
                  ByVal Body As String, _
                  ByVal UserName As String, _
                  ByVal Password As String, _
                  Optional ByVal Server As String = "smtp.gmail.com", _
                  Optional ByVal Port As Integer = 587, _
                  Optional ByVal Attachments As List(Of String) = Nothing) As String


    Dim Email As New MailMessage()
    Try
        Dim SMTPServer As New SmtpClient
        For Each Attachment As String In Attachments
            Email.Attachments.Add(New Attachment(Attachment))
        Next
        Email.From = New MailAddress(FromAddress)
        For Each Recipient As String In Recipients
            Email.To.Add(Recipient)
        Next
        Email.Subject = Subject
        Email.Body = Body
        SMTPServer.Host = Server
        SMTPServer.Port = Port
        SMTPServer.Credentials = New System.Net.NetworkCredential(UserName, Password)
        SMTPServer.EnableSsl = True
        SMTPServer.Send(Email)
        SMTPServer.Timeout = 500
        Email.Dispose()

        Return "Email to " & Recipients(0) & " from " & FromAddress & " was sent."
    Catch ex As SmtpException
        Email.Dispose()
        Return "Sending Email Failed. Smtp Error."
    Catch ex As ArgumentOutOfRangeException
        Email.Dispose()
        Return "Sending Email Failed. Check Port Number."
    Catch Ex As InvalidOperationException
        Email.Dispose()
        Return "Sending Email Failed. Check Port Number."
    End Try
End Function


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim Recipients As New List(Of String)
    Recipients.Add("put recipient email here")
    Dim FromEmailAddress As String = Recipients(0)
    Dim Subject As String = "subject goes here"
    Dim Body As String = "Email body goes here"
    Dim UserName As String = "put username here"
    Dim Password As String = "put password here"
    Dim Port As Integer = 587
    Dim Server As String = "smtp.gmail.com"
    Dim Attachments As New List(Of String)
    MsgBox(SendEmail(Recipients, FromEmailAddress, Subject, Body, UserName, Password, Server, Port, Attachments))

End Sub

无论如何我可以加快速度还是这是我无法控制的?提前致谢。

4

2 回答 2

1

您的电子邮件发送类似乎正确。取决于您的网络连接或您添加到MailMessage班级的附件的大小,它会持续更少或更多的时间。

顺便说一句,您正在分配:

SMTPServer.Timeout = 500

发送电子邮件后:

SMTPServer.Send(Email)

因此,它没有被分配给班级,也没有被考虑在内。

TimeOut当您的客户端失去网络连接(因此它不会等待永远发送)或服务器电子邮件不起作用(这种情况是 gmail,不太可能发生)时,它很有用。

TimeOut 与发送速度无关,它只会Throw Exception在涉及连接或服务器错误时加速。

于 2013-10-22T07:35:39.833 回答
0
  smtp.Host = "smtp.gmail.com"; //host name
    smtp.Port = 587; //port number
    smtp.EnableSsl = true; //whether your smtp server requires SSL
    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
    smtp.Timeout = 20000;

使用 ASP.NET 和 C#(smtp.Send() 方法)通过发送邮件 (SMTP)了解详细信息

于 2013-11-26T08:27:49.490 回答