Email problem with ASP.NET 2.0 (VB)
-
Hi Sorry to bother you guys but this is driving me nuts. I'm trying to retrieve a list of email addresses from a database, and send an email to all those addresses. For testing purposes, the database contains 1 address, mine. Code below (with senstivie bits *** out) Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click If Page.IsValid Then Dim strSQL As String Dim MyConn As Data.SqlClient.SqlConnection MyConn = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) Dim strBody As String Dim Msg As New System.Net.Mail.MailMessage Dim MailObj As New System.Net.Mail.SmtpClient() 'retrieve list of email address,IDs etc strSQL = "Select * From vwNewsLetterList" Dim objDR As Data.SqlClient.SqlDataReader Dim Cmd As New Data.SqlClient.SqlCommand(strSQL, MyConn) MyConn.Open() objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection) 'loop thru data, construct the message and send it While objDR.Read() strBody = txtMessage.Text Msg.IsBodyHtml = True Msg.Subject = "Test email" MailObj.Host = "mail.*****.net" Msg.From = New System.Net.Mail.MailAddress("*@*.net") Msg.To.Add(New System.Net.Mail.MailAddress(objDR("Email"))) Msg.Body = strBody MailObj.Send(Msg) End While MyConn.Close() End If End Sub The code excutes with no errors, but... immediately afterwards I see my virus scanner going, and when I check my mail I get an "Undelivered Mail Returned to Sender" message from my virus checker, with the following info: I'm sorry to have to inform you that the message returned below could not be delivered to one or more destinations. ------------------------------------------------------------------- *******@*****.com: sorry, that domain isn't allowed to be relayed thru this MTA (#5.7.1) ------------------------------------------------------------------- Your e-mail message is being returned to you in the next part of this message. Try to send the message again. It seems as if the code is trying to send the message through my personal email
-
Hi Sorry to bother you guys but this is driving me nuts. I'm trying to retrieve a list of email addresses from a database, and send an email to all those addresses. For testing purposes, the database contains 1 address, mine. Code below (with senstivie bits *** out) Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click If Page.IsValid Then Dim strSQL As String Dim MyConn As Data.SqlClient.SqlConnection MyConn = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) Dim strBody As String Dim Msg As New System.Net.Mail.MailMessage Dim MailObj As New System.Net.Mail.SmtpClient() 'retrieve list of email address,IDs etc strSQL = "Select * From vwNewsLetterList" Dim objDR As Data.SqlClient.SqlDataReader Dim Cmd As New Data.SqlClient.SqlCommand(strSQL, MyConn) MyConn.Open() objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection) 'loop thru data, construct the message and send it While objDR.Read() strBody = txtMessage.Text Msg.IsBodyHtml = True Msg.Subject = "Test email" MailObj.Host = "mail.*****.net" Msg.From = New System.Net.Mail.MailAddress("*@*.net") Msg.To.Add(New System.Net.Mail.MailAddress(objDR("Email"))) Msg.Body = strBody MailObj.Send(Msg) End While MyConn.Close() End If End Sub The code excutes with no errors, but... immediately afterwards I see my virus scanner going, and when I check my mail I get an "Undelivered Mail Returned to Sender" message from my virus checker, with the following info: I'm sorry to have to inform you that the message returned below could not be delivered to one or more destinations. ------------------------------------------------------------------- *******@*****.com: sorry, that domain isn't allowed to be relayed thru this MTA (#5.7.1) ------------------------------------------------------------------- Your e-mail message is being returned to you in the next part of this message. Try to send the message again. It seems as if the code is trying to send the message through my personal email
try this Try Dim smtpserver As Web.Mail.SmtpMail Dim mesg As New Web.Mail.MailMessage smtpserver.SmtpServer=strsmtpserver mesg.From = mailFrom mesg.To = mailto mesg.Subject = subj mesg.Body = messg smtpserver.Send(mesg) i = "Mail Sent" Catch i = "Failed to send Mail" End Try Remember that ur mail id should be a part of the smtp server e.g if ur id is xzy@yahoo.com then smtp server should be of yahoo else u will get the relay error message
-
try this Try Dim smtpserver As Web.Mail.SmtpMail Dim mesg As New Web.Mail.MailMessage smtpserver.SmtpServer=strsmtpserver mesg.From = mailFrom mesg.To = mailto mesg.Subject = subj mesg.Body = messg smtpserver.Send(mesg) i = "Mail Sent" Catch i = "Failed to send Mail" End Try Remember that ur mail id should be a part of the smtp server e.g if ur id is xzy@yahoo.com then smtp server should be of yahoo else u will get the relay error message
OK, it's sorted - and I didn't have to change any of the code. It turns out my webhost wants the SMTP server bit called "localhost". Thats all that was required. Thank you for the reply though, smita_roy.