2013-04-30 75 views
0

我想在我的VB.Net Windows應用程序(VS 2010)發送電子郵件,但我正在逐漸如何發送電子郵件在vb.net windows應用程序2010年,通過SMTP服務器使用Gmail憑證

SMTP找不到主機

我的代碼如下,

Dim SmtpServer As New SmtpClient() 
SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "mypassword") 
SmtpServer.Port = 25 
SmtpServer.Host = "smtp.gmail.com" 
SmtpServer.EnableSsl = True 
mail = New MailMessage() 
Dim addr() As String = TextBox1.Text.Split(",") 
Try 
    mail.From = New MailAddress("[email protected]", "Developers", System.Text.Encoding.UTF8) 
    Dim i As Byte 
    For i = 0 To addr.Length - 1 
     mail.To.Add(addr(i)) 
    Next 
    mail.Subject = TextBox3.Text 
    'mail.Body = TextBox4.Text 
    If ListBox1.Items.Count <> 0 Then 
     For i = 0 To ListBox1.Items.Count - 1 
      mail.Attachments.Add(New Attachment(ListBox1.Items.Item(i))) 
     Next 
    End If 
    SmtpServer.SendAsync(mail, mail.Subject) 
+0

優秀資源:http://www.systemnetmail.com/ – GJKH 2013-04-30 15:22:22

回答

0

嘗試設置SmtpServer.Port到587 ...

Dim SmtpServer As New SmtpClient("smtp.gmail.com", 587) 
Dim mail As New MailMessage("sender address", "destination address", "subject", "body") 
SmtpServer.Credentials = New Net.NetworkCredential("username/sender address","password") 
SmtpServer.Send(Mail) 
+0

我不認爲端口號本身是這裏的問題 - 25應該工作。它可能是一個防火牆阻止它。 – 2013-04-30 11:57:58

0

僅用於測試,我已經很快寫了這段代碼,成功地發送 電子郵件到我的測試帳戶。僅供參考,我在SmtpServer.SendAsync函數中發送第二個參數爲Nothing 。我想你可以快速看看如何在ASYNC environemnt中實現它。

嘗試

 Dim SmtpServer As New SmtpClient() 
     SmtpServer.Credentials = New Net.NetworkCredential("EMAIL [email protected]", "YOUR PASSWORD") 
     SmtpServer.Port = 25 
     SmtpServer.Host = "smtp.gmail.com" 
     SmtpServer.EnableSsl = True 
     Dim omail As New MailMessage() 


     omail.From = New MailAddress("FROM EMAIL @gmail.com", "Asfand Iqbal", System.Text.Encoding.UTF8) 

     omail.Subject = "test subject" 
     omail.To.Add("[email protected]") 

     SmtpServer.SendAsync(omail, Nothing) 

    Catch ex As Exception 
     MsgBox(ex.ToString) 
    End Try 
0
Imports System.Net.Mail 
Public Class Form1 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     ' Set the caption bar text of the form. 
     Me.Text = "tutorialspoint.com" 
    End Sub 

    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() 
      Smtp_Server.UseDefaultCredentials = False 
      Smtp_Server.Credentials = New Net.NetworkCredential("[email protected]", "password") 
      Smtp_Server.Port = 587 
      Smtp_Server.EnableSsl = True 
      Smtp_Server.Host = "smtp.gmail.com" 

      e_mail = New MailMessage() 
      e_mail.From = New MailAddress(txtFrom.Text) 
      e_mail.To.Add(txtTo.Text) 
      e_mail.Subject = "Email Sending" 
      e_mail.IsBodyHtml = False 
      e_mail.Body = txtMessage.Text 
      Smtp_Server.Send(e_mail) 
      MsgBox("Mail Sent") 

     Catch error_t As Exception 
      MsgBox(error_t.ToString) 
     End Try 

    End Sub 
'Ghaffari 
+0

請給你的答案添加一些解釋 – 2014-11-27 21:18:09

相關問題