?
Solved

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

Posted on 2013-11-14
2
Medium Priority
?
1,490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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 …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

764 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