Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-11-14
2
Medium Priority
?
1,512 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

636 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