[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.net VB.net Send Mail with SQL Query data

Posted on 2013-11-14
2
Medium Priority
?
1,540 Views
Last Modified: 2013-11-16
Hi

I have an ASP.net website, where I send an email using the following code.
I have a SQL database that I pull information from into a GridView.
I now want to Email the results of my SQL query using the emailing code below.
How do I do this?

    Public Function SendScheduleMail(ByVal from As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String) As String
        Try

            ' Instantiate a new instance of MailMessage
            Dim mMailMessage As New MailMessage()

            ' Set the sender address of the mail message
            mMailMessage.From = New MailAddress(from)
            ' Set the recepient address of the mail message
            mMailMessage.To.Add(New MailAddress(recepient))

            ' Check if the bcc value is nothing or an empty string
            If Not bcc Is Nothing And bcc <> String.Empty Then
                ' Set the Bcc address of the mail message
                mMailMessage.Bcc.Add(New MailAddress(bcc))
            End If

            ' Check if the cc value is nothing or an empty value
            If Not cc Is Nothing And cc <> String.Empty Then
                ' Set the CC address of the mail message
                mMailMessage.CC.Add(New MailAddress(cc))
            End If

            ' Set the subject of the mail message
            mMailMessage.Subject = subject
            ' Set the body of the mail message
            mMailMessage.Body = body

            ' Set the format of the mail message body as HTML
            mMailMessage.IsBodyHtml = True
            ' Set the priority of the mail message to normal
            mMailMessage.Priority = MailPriority.Normal

            ' Instantiate a new instance of SmtpClient
            Dim mSmtpClient As New SmtpClient()
            ' Send the mail message
            mSmtpClient.Send(mMailMessage)

            SendScheduleMail = "Mail Sent"
        Catch ex As Exception
            SendScheduleMail = ex.Message
        End Try
    End Function
0
Comment
Question by:Murray Brown
2 Comments
 
LVL 29

Accepted Solution

by:
sammySeltzer earned 2000 total points
ID: 39651164
I will suggest performing your query in the email sub like this:


    Protected Sub YourInfo()

        Dim Conn As SqlConnection
        Conn = New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString)
        Conn.Open()
        Dim emailcmd As New SqlCommand("SELECT....from table... ";", Conn)
        Dim dr = emailcmd.ExecuteReader()
        If dr.Read() Then
            email = dr.GetString(0)
            fullname = dr.GetString(1)
            additional info = GetString...
            additional info = GetString...
        End If
        'code for email requests
        Dim objSmtpClient As SmtpClient = New SmtpClient("yourgetway", 25)
        'Setup credentials to login to our sender email address ("email", "password")
        Dim credentials As New NetworkCredential("username", "password")
        objSmtpClient.Credentials = credentials
        Dim objSender As MailAddress = New MailAddress("NoReply@yourdomain.com", "NoReply@yourdomain.com")
        Dim objMail As MailMessage = New MailMessage("NoReply@yourdomain.com", email)
        'objMail.Bcc.Add("someaddress")
        'objMail.To.Add(email)
        'objMail.CC.Add("someaddress")
        objMail.Subject = "whatever"
        objMail.Body = " Dear " & fullname & " <br> something message "
        objMail.IsBodyHtml = True
        objSmtpClient.Send(objMail)
        dr.Close()

    End Sub

Open in new window


Then on pageLoad() event you call the sub:

YourInfo()

Open in new window

0
 

Author Closing Comment

by:Murray Brown
ID: 39654143
Thanks very much
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question