如何从visual basic 2010专业人员发送邮件

时间:2013-02-22 07:12:22

标签: email smtp system.net.mail system.net

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

导入System.Net 进口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

结束班

3 个答案:

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

答案 1 :(得分:0)

您可以创建一个名为“Mail”的新类。 如果您要创建课程,则如果要发送超过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()

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