如何通过smtp服务器使用gmail凭据在vb.net windows application 2010中发送电子邮件

时间:2013-04-30 09:35:22

标签: vb.net email

我正在尝试在我的VB.Net Windows应用程序(VS 2010)中发送电子邮件,但我正在

  

找不到SMTP主机

我的代码如下,

Dim SmtpServer As New SmtpClient()
SmtpServer.Credentials = New Net.NetworkCredential("mymailid@gmail.com", "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("mymailid@gmail.com", "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)

4 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

为了测试,我已经快速编写了这段成功发送的代码 发邮件到我的测试帐户。仅供参考,我已将第二个参数发送为Nothing 在SmtpServer.SendAsync函数中。我猜您可以快速了解如何在ASYNC environemnt中实现它。

尝试

        Dim SmtpServer As New SmtpClient()
        SmtpServer.Credentials = New Net.NetworkCredential("EMAIL FROM@gmail.com", "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("test@gmail.com")

        SmtpServer.SendAsync(omail, Nothing)

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

答案 2 :(得分:0)

请尝试

 Dim SmtpServer As New SmtpClient("smtp.gmail.com", 465)
 SmtpServer.EnableSsl = True
 SmtpServer.Credentials = New Net.NetworkCredential("name@gmail.com", "password")
 Dim mail As New MailMessage("name@gmail.com", "name@gmail.com", title, content)
 SmtpServer.Send(mail)

答案 3 :(得分: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("username@gmail.com", "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