Solved

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

Posted on 2013-11-14
2
1,366 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:murbro
2 Comments
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
Comment Utility
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:murbro
Comment Utility
Thanks very much
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now