如何在vb.net windows应用程序2010中通过smtp服务器使用gmail凭据发送电子邮件
我试图在我的VB.NETWindows应用程序(VS2010)中发送电子邮件,但我收到了 找不到SMTP主机 我的代码如下:如何在vb.net windows应用程序2010中通过smtp服务器使用gmail凭据发送电子邮件,vb.net,email,Vb.net,Email,我试图在我的VB.NETWindows应用程序(VS2010)中发送电子邮件,但我收到了 找不到SMTP主机 我的代码如下: Dim SmtpServer As New SmtpClient() SmtpServer.Credentials = New Net.NetworkCredential("mymailid@gmail.com", "mypassword") SmtpServer.Port = 25 SmtpServer.Host = "smtp.gmail.com" SmtpServe
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)
Dim SmtpServer作为新的SmtpClient()
SmtpServer.Credentials=新的Net.NetworkCredential(“mymailid@gmail.com“,“我的密码”)
SmtpServer.Port=25
SmtpServer.Host=“smtp.gmail.com”
SmtpServer.EnableSsl=True
mail=新邮件消息()
Dim addr()作为字符串=TextBox1.Text.Split(“,”)
尝试
mail.From=新邮件地址(“mymailid@gmail.com,“开发者”,System.Text.Encoding.UTF8)
作为字节的Dim i
对于i=0,添加长度-1
邮寄至地址(地址(一))
下一个
mail.Subject=TextBox3.Text
'mail.Body=TextBox4.Text
如果ListBox1.Items.Count为0,则
对于ListBox1.Items.Count-1的i=0
mail.Attachments.Add(新附件(ListBox1.Items.Item(i)))
下一个
如果结束
SmtpServer.SendAsync(邮件、邮件、主题)
尝试将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)
为了进行测试,我快速编写了这段代码,成功地发送了 发电子邮件到我的测试帐户。仅供参考,我已将第二个参数作为Nothing发送 在SmtpServer.SendAsync函数中。我想您可以快速了解如何在异步环境中实现它 试一试 请试一试
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)
我不认为端口号本身是这里的问题-25应该起作用。不过,可能是防火墙阻止了它。优秀资源:请对您的答案进行解释
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)
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