如何在 vb.net Windows 应用程序 2010 中发送电子邮件,通过 smtp 服务器使用 gmail 凭据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16296635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to send email in vb.net windows application 2010,using gmail credentials via smtp server
提问by user2218316
I am trying to send email in my VB.Net windows application (VS 2010), but I am getting
我正在尝试在我的 VB.Net Windows 应用程序 (VS 2010) 中发送电子邮件,但我收到了
SMTP host not found
找不到 SMTP 主机
My code is as below,
我的代码如下,
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)
回答by Sam
Try setting SmtpServer.Portto 587...
尝试设置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)
回答by Asfand Iqbal
Just for testing I have quickly wrote this piece of code that successfully sends email to my testing account. Just for reference I have sent second parameter as Nothing in SmtpServer.SendAsync function. I guess you can quickly have a look how you can implement it in ASYNC environemnt.
只是为了测试,我快速编写了这段代码,成功地将电子邮件发送到我的测试帐户。仅供参考,我在 SmtpServer.SendAsync 函数中将第二个参数作为 Nothing 发送。我想您可以快速了解如何在 ASYNC 环境中实现它。
Try
尝试
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
回答by AnandMeena
Please try
请尝试
Dim SmtpServer As New SmtpClient("smtp.gmail.com", 465)
SmtpServer.EnableSsl = True
SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "password")
Dim mail As New MailMessage("[email protected]", "[email protected]", title, content)
SmtpServer.Send(mail)
回答by Alireza
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

