Send an email on click of a button

Hi Experts,

I want to send an email on the click of a button. Here is my code. Using asp.net and VB.net
 Sub SendSimpleMail()
        Try
            Dim Smtp_Server As New SmtpClient
            Dim e_mail As New MailMessage()
            Smtp_Server.UseDefaultCredentials = False
            Smtp_Server.Credentials = New Net.NetworkCredential("mygmailID@gmail.com", "mygmailpassword")
            Smtp_Server.Port = 587
            Smtp_Server.EnableSsl = True
            Smtp_Server.Host = "smtp.gmail.com"

            e_mail = New MailMessage()
            e_mail.From = New MailAddress("Rina_mahapatra@yahoo.com")
            e_mail.To.Add("rina_mahapatra@yahoo.com")
            e_mail.Subject = "Email Sending"
            e_mail.IsBodyHtml = False
            e_mail.Body = "Hi"
            Smtp_Server.Send(e_mail)
            MsgBox("Mail Sent")

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


web.config
-----------------
<system.net>
    <mailSettings>
      <smtp from="emailID">
        <network host="smtp.localhost.com"
                 defaultCredentials="true"
                 enableSsl="true"
                 port="25"/>
      </smtp>
    </mailSettings>
  </system.net>

Please help!!

Thank u.
RadhaKrishnaKiJayaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ess KayEntrapenuerCommented:
source: https://www.emailarchitect.net/easendmail/kb/vbnet.aspx?cat=2



Imports EASendMail 'Add EASendMail namespace

Module Module1
    Sub Main()
        Dim oMail As New SmtpMail("TryIt")
        Dim oSmtp As New SmtpClient()

        ' Your gmail email address
        oMail.From = "gmailid@gmail.com"
       
        ' Set recipient email address, please change it to yours
        oMail.To = "support@emailarchitect.net"
       
        ' Set email subject
        oMail.Subject = "test email from gmail account"
       
        ' Set email body
        oMail.TextBody = "this is a test email sent from VB.NET project with gmail"

        'Gmail SMTP server address
        Dim oServer As New SmtpServer("smtp.gmail.com")

        ' set 465 port
        oServer.Port = 465

        ' detect SSL/TLS automatically
        oServer.ConnectType = SmtpConnectType.ConnectSSLAuto

        ' Gmail user authentication should use your
        ' Gmail email address as the user name.
        ' For example: your email is "gmailid@gmail.com", then the user should be "gmailid@gmail.com"
        oServer.User = "gmailid"
        oServer.Password = "yourpassword"

        Try

            Console.WriteLine("start to send email over SSL ...")
            oSmtp.SendMail(oServer, oMail)
            Console.WriteLine("email was sent successfully!")

        Catch ep As Exception

            Console.WriteLine("failed to send email with the following error:")
            Console.WriteLine(ep.Message)
        End Try

    End Sub
End Module
RadhaKrishnaKiJayaAuthor Commented:
Thank you for your reply. But it is not working.
CodeCruiserCommented:
Try following code


 Sub SendSimpleMail()
        Try
            Dim Smtp_Server As New SmtpClient("smtp.gmail.com", 465)
            Dim e_mail As New MailMessage()
            Smtp_Server.UseDefaultCredentials = False
            Smtp_Server.Credentials = New Net.NetworkCredential("mygmailID@gmail.com", "mygmailpassword")
            Smtp_Server.EnableSsl = True

            e_mail = New MailMessage()
            e_mail.From = New MailAddress("Rina_mahapatra@yahoo.com")
            e_mail.To.Add("rina_mahapatra@yahoo.com")
            e_mail.Subject = "Email Sending"
            e_mail.IsBodyHtml = False
            e_mail.Body = "Hi"
            Smtp_Server.Send(e_mail)
            MsgBox("Mail Sent")

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

Open in new window

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Ess KayEntrapenuerCommented:
the code i posted requires EASendMail class wich you can find on the link
RadhaKrishnaKiJayaAuthor Commented:
Hi,

Thank you for your reply.. But it is not working.
Ess KayEntrapenuerCommented:
ok.  try this one

http://webcheatsheet.com/asp/sending_email_asp.php


you will make a function in a page,
the button is going to call the  function
Ess KayEntrapenuerCommented:
make sure it works by itself first.  once you got it sending emails,  you can add the button
RadhaKrishnaKiJayaAuthor Commented:
Thank you for your reply. But it is not working for me. I got some different code which is working for me.

Public Shared Function SendSimpleMail(TicketNo As Integer, empName As String, moduleName As String, priority As String, email As String, Status As String) As Boolean
        SendSimpleMail = False
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New  _
            Net.NetworkCredential("userID", "****")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            SmtpServer.EnableSsl = "true"
            mail = New MailMessage()
            mail.From = New MailAddress("admin@gmail.com")
            mail.To.Add(email)
            Dim msg As String = ""
            If Status = "OP" Then
                mail.Subject = "New ticket created, Automated message do not respond."
                msg = "New ticket created by : " & empName & ", Problem in : " & moduleName & ", priority : " & priority
            ElseIf Status = "CO" Then
                mail.Subject = "Your ticket is completed, Automated message do not respond."
                msg = "Ticket #: " & TicketNo & ", module : " & moduleName & " is completed. Please check it and CLOSE if you are satisfied."
            End If

            mail.Body = msg
            Dim userState As Object = mail
            'AddHandler SmtpServer.SendCompleted, AddressOf SmtpClient_OnCompleted
            'SmtpServer.SendAsync(mail,userState)
            SmtpServer.Send(mail)
            SendSimpleMail = True
        Catch ex As Exception
            MsgBox("Problem while sending the mail: SendSimpleMail() : " & ex.Message)
        End Try

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadhaKrishnaKiJayaAuthor Commented:
I found the solution by myself.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.