473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

sending email to multiple recipient

1
Hi...

I want to send an email to multiple recipients from my database in sql server. I'm using .vb to do it.. Any suggestions on how i can use a for loop to send my email? Thanks.

Expand|Select|Wrap|Line Numbers
  1. Dim cmdEmails As New SqlCommand("select student_email from student", conn)
  2.         conn.Open()
  3.  
  4.  
  5.         Dim ds As New DataTable
  6.         ds = New DataTable
  7.  
  8.         Dim mailMessage As New MailMessage()
  9.         mailMessage.From = New MailAddress("0609887e@student.tp.edu.sg")
  10.         mailMessage.To.Add(dd_email.SelectedValue)
  11.  
  12.  
  13.         mailMessage.Subject = txt_subj.Text
  14.         mailMessage.Body = txt_content.Text
  15.         'Dim msg As New MailMessage(dd_email.SelectedValue, txt_subj.Text, txt_content.Text)
  16.         Try
  17.             If fileUpload.PostedFile.FileName = "" Then
  18.             Else
  19.                 mailMessage.Attachments.Add(New System.Net.Mail.Attachment(fileUpload.PostedFile.FileName))
  20.             End If
  21.             Dim smtpMail As New SmtpClient("localhost")
  22.            smtpMail.Send(mailMessage)
  23.             lbl_status.Visible = True
  24.             lbl_status.Text = "Message sent."
  25.         Catch ex As Exception
  26.             lbl_status.Visible = True
  27.             lbl_status.Text = "Error"
  28.  
  29.             conn.Close()
  30.         End Try
  31.     End Sub
  32. End Class
Sep 5 '08 #1
1 2477
MickT
28
You'll need to add AxMAPISession and AxMAPIMessages to your form.

The emails are sent via Outlook.

If you don't want to use that, I'm sure that parts of the code will provide your loop.

Private Sub MySyb()

' Email reports to specified recipients
strSubject = "My Subject"
strBody = "My Body"

' Collect the email addresses for each recipient
strSQL = "SELECT Name AS Addressee FROM Recipient ORDER BY Name"

strSub = strSubName + " - Filling recipient dataset"
objDSRecipient = FillDataSet(strConn, strSQLRecipient)

' Send the emails

With objDSRecipient.Tables(0)
' Loop through the records and select reports for current recipient
If .Rows.Count > 0 Then
For X = 0 To .Rows.Count - 1
intRecipientID = CInt(.Rows(X).Item("ID").ToString)
ReDim strAddressArray(1)
strAddressArray(1) = .Rows(X).Item("Addressee").ToString
Next X ' Recipient
NewEmail(strAddressArray, _
strBody, strSubject)
End If
End With
Catch objA As Exception
Cursor.Current = Cursors.Default
MessageBox.Show("Error in sub '" & strSub & "' with " & objA.Source & _
vbCrLf & vbCrLf & objA.Message, "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)

Finally
Cursor = Cursors.Default
objDSRecipient = Nothing
End Try
End Sub


Public Function FillDataSet(ByVal strConn As String, _
ByVal strProc As String) As Data.DataSet
Dim objDS As New Data.DataSet
Dim objDA As SqlClient.SqlDataAdapter
Try
strSubName = "FillDataSet"

strSub = strSubName + " - Initialising variables"
Cursor.Current = Cursors.WaitCursor 'vbHourglass
' Initialise the SqlDataAdapter with the stored procedure/SQL
' and connection string, and then use the SqlDataAdapter to fill
' the Dataset with data.
strSub = strSubName + " - Creating DataAdapter & Filling Dataset"
objDA = New SqlClient.SqlDataAdapter(strProc, strConn)
objDA.Fill(objDS)
FillDataSet = objDS

Catch objA As Exception
MessageBox.Show("Error in sub '" & strSub & "' with " & objA.Source & _
vbCrLf & vbCrLf & objA.Message, "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
Cursor.Current = Cursors.Default ' vbDefault
objDS = Nothing
objDA = Nothing
End Try
End Function



Private Sub NewEmail(ByVal strAddress() As String, _
ByVal strAttachment() As String, _
Optional ByVal strBody As String = "", _
Optional ByVal strSubject As String = "", _
Optional ByVal strAttachName As String = "")

Dim X As Integer
Dim lngFile As Long
Dim strRecord As String

Try
strSubName = Me.GetType.ToString + " - NewEmail"

strSub = strSubName + " - Preparing Email"
' Trigger default email client to send file
mapSession.SignOn()

With mapMessage
.SessionID = mapSession.SessionID
.MsgIndex = -1
.Compose()

strSub = strSubName + " - Adding Recipient(s)"
' Add address(s) of recipient(s)
For X = 1 To UBound(strAddress)
.RecipIndex = X - 1
' .RecipAddress = strAddress(X)
.RecipDisplayName = strAddress(X)
Next X
'Close()

' Add message subject
.MsgSubject = strSubject

strSub = strSubName + " - Dealing With Attachment(s)"
' Deal with attachment(s)
If blnAttachments Then

' Deal with message text - attachments
' Reserve space for attachments
strMsg = Space(UBound(strAttachment)) & vbCrLf
If Len(strBody) > 0 Then
.MsgNoteText = strMsg & strBody
Else
.MsgNoteText = strMsg & ""
End If

If Len(strAttachment(1)) > 0 Then
For X = UBound(strAttachment) To 1 Step -1
.AttachmentIndex = X - 1
.AttachmentPosition = X - 1
If Len(strAttachName) > 0 Then
.AttachmentName = strAttachName
End If
.AttachmentPathName = strAttachment(0) & "\" & strAttachment(X)
'MsgBox(.AttachmentPathName)
' .AttachmentName = strAttachName
Next X
End If
Else

strSub = strSubName + " - Dealing With Message Text"
' Deal with message text - no attachments
If Len(strBody) > 0 Then
.MsgNoteText = strBody
End If
End If

strSub = strSubName + " - Sending Email"
.Send(False) ' False means that there is user interaction; with True there is
End With

Catch objA As Exception
'MsgBox(Err.Number)
If Err.Number = 32001 Then ' User Cancelled
Exit Try
ElseIf Err.Number = 32026 Then ' Cancelled email from Outlook security message
MsgBox("Email message to " + strAddress(1) + " cancelled by user")
Security.WriteUserAudit(20, strUser, strAddress(1))

Exit Try
ElseIf Err.Number = 32011 Then ' Attachment not found
MsgBox("Attachment not found for email message to " + strAddress(1), MsgBoxStyle.Information, "Attachment Not Found")
Exit Try
End If

MessageBox.Show("Error in sub '" & strSub & "' with " & objA.Source & _
vbCrLf & vbCrLf & objA.Message, "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)

Finally

strSub = strSubName + " - Close Session"
mapSession.SignOff()
End Try

End Sub

From MickT - 5th Sept 2008
Sep 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Kingdom | last post by:
I have a SelectBoxes.asp page that is working with multiple selection dropdown boxes to extract data and total the selection prices. Tom & Bob were kind enough to give me a big help getting this...
4
by: acni | last post by:
I have the following peice of code to try and send an email to selected contacts in my form.The problem is this line: StrStore = DLookup("", "qrySelectEmail", "??????") This looks up the email...
3
by: VB Programmer | last post by:
My company has it's own webserver, which is going to host our ASP.NET web application. We want the website to be able to send out emails. 1. What do I need on the server so that it has the...
3
by: VB Programmer | last post by:
I have an ASPX page where I send out emails through my mail server mail.MyDomain.com. When I send emails to MyName@MyDomain.com it sends PERFECTLY. When I try sending an email to any other address...
6
by: Anuradha | last post by:
Dear All How can i send mails using vb.net Thanx all
3
by: JaffaCakes | last post by:
I want to send an email confirmation after a user completes a form on our Internet page. I am testing this with the System.Web.Mail.SmtpMail class. If I do a SmtpMail.Send then the message takes...
3
by: bcanter | last post by:
I am setting up a user request form to help our IT staff get all of the information required to setup a user account, I would like to send the form to a static address each time that it is submitted...
0
by: damimkader | last post by:
Hi, I'm trying to send emails using a Macro based on an Excel Sheet and the Email Client I'm using is Lotus Notes. OS used - Windows Xp. Language - VB Below is the Code I'm using for doing...
7
by: bleachie | last post by:
Hey, I just need some help, my form seems to not send me all of the 'guestNames' and 'guestEmails' forms. i use this function to add more guestnames and guestemail fields based on the number of...
0
by: ramab | last post by:
hi, i'm a bit stuck here in sending emails to multiple users which i need to read from a database. With codes below, i able only to send 2 recipients. any idea how to send to multiple users? thx...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.