Solved

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

Posted on 2013-11-14
2
1,390 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
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:murbro
ID: 39654143
Thanks very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

11 Experts available now in Live!

Get 1:1 Help Now