2013-10-22 104 views
0

我在這裏有一個使用SMTP的工作代碼,我使用Gmail作爲我的服務器,但我想加快發送速度。我曾嘗試將Timeout設置爲500,但仍然很慢。加快SMTP發送速度

這裏是我的完整代碼:

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 

反正對我來說,加快這或這是在我的掌握?提前致謝。

回答

1

您的電子郵件發送類似乎是正確的。根據您的網絡連接或您添加到MailMessage類中的附件的大小,它將持續更少或更多的時間。您發送電子郵件後

SMTPServer.Timeout = 500 

順便說一句,你是分配

SMTPServer.Send(Email) 

所以它沒有被分配到類,並在考慮到不採取。

TimeOut當你的客戶端失去網絡連接(所以它不會等到永遠發送)或服務器電子郵件不可用(這種情況是gmail,不太可能發生)時很有用。

TimeOut與發送速度無關,只會在涉及連接或服務器的某些內容出錯時加速Throw Exception

+0

感謝您的信息。我覺得沒有什麼我可以做的發送速度然後。現在是時候進入另一個實驗:D –

+0

電子郵件類很簡單,這是正確的:)。轉到另一個實驗;) –

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; 

去通過Send Mail (SMTP) using ASP.NET and C# (smtp.Send() method)的細節