How to Send mail from visual basic 2010 professional

北战南征 提交于 2020-01-14 04:56:06

问题


I want to send a mail using gmail id, my code is as here under, but its occurred error...please help me out of this issue.

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


回答1:


Below is an example of sending an email with visual basic 10, First you need to build a form with fields to,from,text,and email_send button now the below code

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



回答2:


You can make a new class called 'Mail'. If you make a class, you don't have to type all of the code again if you want to send more than 1 email.

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

And then you can make a new object in your code where and when you want to send the email:

Dim email as new Mail
email.receiver = "receiver@example.com"
email.subject = "Subject"
email.body = "Message"
email.send()



回答3:


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


来源:https://stackoverflow.com/questions/15018776/how-to-send-mail-from-visual-basic-2010-professional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!