I have a table called Emails with three colums, mailID identity seed PK, , and fullName.
This table has several names and email addresses.
So far, when I click "Send Emails", only the last email gets sent.
Any ideas what's wrong with the script below?
<asp:Button ID="btnSend" runat="server" Text="Send" BackColor="#999966" OnClick="btnsend_Click" />
Imports System
Imports System.Data.SqlClient
Imports System.Net
Imports System.Net.Mail
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected toEmail, EmailSubj, EmailMsg, name As String
Public Pass, FromEmailid, HostAdd As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnsend_Click(ByVal sender As Object, ByVal e As EventArgs)
Using conn As New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using cmd As New SqlCommand()
cmd.CommandText = "SELECT DISTINCT e.FullName, STUFF((SELECT ';' + Email FROM Emails WHERE FullName = e.FullName AND sent = 'No' FOR XML PATH('')),1,1,'') As Email From Emails e"
'cmd.Parameters.AddWithValue("@fullname", name)
cmd.Connection = conn
conn.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
name = sdr("FullName").ToString()
toEmail = sdr("Email").ToString()
End While
End Using
conn.Close()
End Using
End Using
EmailSubj = Convert.ToString(txtsub.Text)
EmailMsg = Convert.ToString(txtsub.Text)
Email_Without_Attachment(toEmail, EmailSubj, EmailMsg)
'Response.Write(toEmail)
'Response.End()
End Sub
Sub Email_Without_Attachment(ByVal ToEmail As String, ByVal Subj As String, ByVal Message As String)
HostAdd = "smtp.gmail.com"
FromEmailid = "gjdoe@mail.com"
Pass = "mypassword"
Dim mailMessage As MailMessage = New MailMessage()
mailMessage.From = New MailAddress(FromEmailid)
mailMessage.Subject = Subj
mailMessage.Body = Message
mailMessage.IsBodyHtml = True
Dim Multi As String() = ToEmail.Split(","c)
For Each Multiemailid As String In Multi
mailMessage.[To].Add(New MailAddress(Multiemailid))
Next
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = HostAdd
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New NetworkCredential()
NetworkCred.UserName = mailMessage.From.Address
NetworkCred.Password = Pass
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mailMessage)
End Sub
End Class