1

我想使用 gmail id 发送邮件,我的代码如下所示,但是发生了错误。我该如何解决这个问题?

Imports System.Net
Imports System.Net.Mail

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("This will send a mail...")
        Try

            Dim smtpserver As New SmtpClient()
            Dim mail As New MailMessage()
            smtpserver.Credentials = New Net.NetworkCredential("myname@gmail.com", "password")
            smtpserver.Port = 465
            smtpserver.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("myname@gmail.com")
            mail.To.Add("to mail id")
            mail.Subject = "Test by bharat"
            mail.Body = "hello ooooooooooooooooooooooo"
            smtpserver.Send(mail)
            MsgBox("Mail Sent")
        Catch ex As Exception
            MsgBox(ex.ToString)
            Close()
        End Try


    End Sub
End Class
4

3 回答 3

0

下面是使用 Visual Basic 10 发送电子邮件的示例,首先您需要构建一个包含字段 to、from、text 和 email_send 按钮的表单,现在下面的代码

Private Smtp_Server As New SmtpClient()

Private Sub email_send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles email_send.Click
Try

Dim e_mail As New MailMessage()
Smtp_Server.Credentials = New Net.NetworkCredential("yener.turkeli@v-basic.net", "password")
Smtp_Server.Port = 25
Smtp_Server.Host = "mail.v-basic.net"
e_mail = New MailMessage()
e_mail.From = New MailAddress(from_txt.Text)
e_mail.To.Add(to_txt.Text)
e_mail.Subject = "VB.NET Email Sending"
e_mail.Body = text_txt.Text
Smtp_Server.Send(e_mail)

Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
于 2013-06-13T02:09:31.640 回答
0

您可以创建一个名为“邮件”的新类。如果您上课,如果您想发送超过 1 封电子邮件,则无需再次输入所有代码。

Imports System.Net.Mail
Public NotInheritable Class Mail
    Public Property subject As String
    Public Property body As String
    Public Property receiver As String

    Public Sub send()
        Try
            Dim smtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            smtpServer.UseDefaultCredentials = False
            smtpServer.Credentials = New Net.NetworkCredential("yener.turkeli@v-basic.net", "password"))
            smtpServer.Port = 587
            smtpServer.EnableSsl = True
            smtpServer.Host = "smtp.gmail.com"

            mail = New MailMessage()
            mail.From = New MailAddress("yener.turkeli@v-basic.net")
            mail.To.Add(receiver)
            mail.Subject = subject
            mail.Body = body
            smtpServer.Send(mail)
        Catch ex As Exception
            MsgBox(ex.Message & vbNewLine & ex.StackTrace)
        End Try

    End Sub
End Class

然后你可以在你的代码中创建一个新对象,在你想发送电子邮件的地方和时间:

Dim email as new Mail
email.receiver = "receiver@example.com"
email.subject = "Subject"
email.body = "Message"
email.send()
于 2013-09-07T12:54:10.047 回答
-1
Imports System.Net.Mail
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    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("myemail@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(TextBox1.Text)
            e_mail.To.Add(TextBox2.Text)
            e_mail.Subject = "Email Sending"
            e_mail.IsBodyHtml = False
            e_mail.Body = txtmsg.Text
            Smtp_Server.Send(e_mail)
            MsgBox("Mail Sent")

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

    End Sub

End Class
于 2013-09-27T20:44:26.350 回答