Asp.net email application mail comes twice when it sends - asp.net

I made this simple mailing application but problem is recipients gets email twice.
This is my code on button click
Protected Sub submit_feedback_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit_feedback.Click
Dim feedback As String = "<b>Client Name :</b><br/>" + ParseOutput.Text + "<br/><br/> <b>Clients Experience :</b><br/>" + brief_details.Text + "<br/><br/> <b>Improvement Needed :</b><br/>" + brief_details2.Text + "<br/><br/> <b>Rating Given :</b><br/>" + rate1.SelectedValue.ToString + "<br/><br/> <b>How do you know about us :</b><br/>" + choice2.SelectedValue.ToString
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient()
mail.To.Add("surajprince20#gmail.com")
mail.From = New MailAddress("something#gmail.com", "SuRaj_ Email Test")
mail.Subject = "Feedback - Reply From Client "
mail.Body = feedback
mail.IsBodyHtml = True
SmtpServer.Port = 25
SmtpServer.Credentials = New System.Net.NetworkCredential("something#gmail.com", "1234")
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
SmtpServer.Send(mail)
Try
SmtpServer.Send(mail)
Response.Write("Successfull")
Catch ex As SmtpException
Response.Write(ex)
End Try
End Sub

It is clear from your code that you are sending the mail twice, have a look into your code:
SmtpServer.Send(mail) '<----- first sending attempt
Try
SmtpServer.Send(mail)'<----- second sending attempt
Response.Write("Successfull")
Catch ex As SmtpException
Response.Write(ex)
End Try
so you need to remove the SmtpServer.Send(mail) which is placed outside the TRY
ie.,
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
Try ' <------ eleminate one line
SmtpServer.Send(mail)
Response.Write("Successfull")
Catch ex As SmtpException
Response.Write(ex)
End Try

SmtpServer.Send(mail)
Try
SmtpServer.Send(mail)
you are acutally sending the mail twice

Protected Sub submit_feedback_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit_feedback.Click
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New System.Net.NetworkCredential("something#gmail.com", "1234")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("something#gmail.com", "SuRaj_ Email Test")
mail.To.Add("surajprince20#gmail.com")
mail.Subject = "Feedback - Reply From Client "
mail.Body = feedback
SmtpServer.Send(mail)
MsgBox("mail send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
END SUB

Related

Send email failed conversion from string "" to type 'Boolean' is not valid

Imports System.Net.Mail
Partial Class ch5_proj4_ch5_proj4
Inherits System.Web.UI.Page
Protected Sub EmailBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles EmailBtn.Click
' insert code here
Dim MySmtpClient As New SmtpClient()
Dim MM As New MailMessage()
Try
MySmtpClient.Host = "stmp.gmail.com"
MySmtpClient.Port = 587
MySmtpClient.EnableSsl = True
MySmtpClient.UseDefaultCredentials = True
MySmtpClient.Credentials = New System.Net.NetworkCredential()
'mail message
MM.To.Add(New MailAddress(TextBox2.Text, TextBox1.Text))
Dim fromAddress As New MailAddress(TextBox4.Text, TextBox3.Text)
MM.From = fromAddress
MM.Subject = TextBox4.Text
MM.IsBodyHtml = RadioButtonList1.SelectedValue
MM.Body = txtMessage.Text
MySmtpClient.Send(MM)
Label5.Text = "Email successfully sent."
Catch exc As Exception
Label5.Text = "Send email failed" + exc.Message
End Try
MM = Nothing
MySmtpClient = Nothing
End Sub
End Class
What am I doing wrong? Every time I do it I have an error
"Send email failed Conversion from string "" to type 'Boolean' is not valid."
Please help.
Try to change this MM.IsBodyHtml = RadioButtonList1.SelectedValue to this MM.IsBodyHtml = If(RadioButtonList1.SelectedValue = "True",True,False)

canceling email send with asp.net custom validator

I have a form that sends an email that includes 4 fields. One of the fields is validated by a custom validator against the database and if the value is found the email should send. If it is not found the email should cancel and there should be an error message. Everything is working except the email sends regardless of the validation. How can I keep the email from sending?
Imports System.Net.Mail
Imports System.Data.OleDb
Imports System.Data.SqlClient
Partial Class inforequest
Inherits System.Web.UI.Page
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim mm As New MailMessage("sender#email.com", "receiver#email.com")
mm.Subject = txtSubject.Text
mm.Body = "Name: " & txtName.Text & "<br /><br />Email: " & txtEmail.Text & "<br />" & txtBody.Text & "<br /> Agent Code:" & AgentCode.Text
mm.IsBodyHtml = True
Dim smtp As New SmtpClient()
smtp.Host = "mailserver"
smtp.EnableSsl = False
Dim NetworkCred As New System.Net.NetworkCredential()
smtp.UseDefaultCredentials = False
NetworkCred.UserName = "username"
NetworkCred.Password = "password"
smtp.EnableSsl = False
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
lblMessage.Text = "Email Sent SucessFully."
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim dbconn As String
Dim qstring = Request.QueryString("ID")
Dim addressDR As System.Data.SqlClient.SqlDataReader
Dim sqlcommand As String = "SELECT * FROM listings WHERE ID=#qstring"
dbconn = ConfigurationManager.ConnectionStrings("houses").ToString
Dim connection As New System.Data.SqlClient.SqlConnection(dbconn)
connection.Open()
Dim addresscmd As New System.Data.SqlClient.SqlCommand(sqlcommand, connection)
addresscmd.Parameters.AddWithValue("#qstring", qstring)
addressDR = addresscmd.ExecuteReader()
If addressDR.HasRows Then
addressDR.Read()
Me.txtBody.Text = "I would like to request a showing of the home located at: " & addressDR("address") & " MLS#: " & addressDR("mlsnum")
addressDR.Close()
End If
connection.Close()
End Sub
Protected Sub CodeValidate_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CodeValidate.ServerValidate
Dim AgentCode = Request.Form("AgentCode")
Dim sql As String = "SELECT agentcode FROM Codes WHERE agentcode = #AgentCode"
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Codes").ConnectionString)
Using cmd As New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("#AgentCode", AgentCode)
conn.Open()
Using rdr As SqlDataReader = cmd.ExecuteReader()
If (rdr.Read()) Then
'MsgBox("reader reading")
'If AgentCode = rdr("agentcode").ToString() Then
args.IsValid = True
'MsgBox("valid!")
Else
args.IsValid = False
'MsgBox("not valid")
End If
End Using
conn.Close()
End Using
End Using
End Sub
End Class
You need to call Page.Validate() and also check Page.IsValid

vb asp.net contact form

Have created a simple contact form for use on client's website and can get the email (i'm using my gmail account temporarily) but when it is sent it shows it is from me no matter what I put in the email field. Please help! Probably something completely ignorant but please help! I want the email to be derived from the txtEmail.text field. Is there something I need to add to my web.config or on GoDaddy side of things? Thanks. Below is my code. I am aware I need to include try catch and clear the fields, but that will come after I get this to work!! And yes I did include my real credentials.
Imports System.Net.Mail.MailAddress
Imports System.Net.Mail.MailMessage
Imports System.Net.NetworkCredential
Imports System.Net.Mail
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim Msg As New MailMessage()
' Sender e-mail address.
Msg.From = New MailAddress(txtEmail.Text)
' Recipient e-mail address.
Msg.To.Add("lchevy5#gmail.com")
Msg.Subject = txtSubject.Text
Msg.Body = "Sent From:" & txtName.Text + Environment.NewLine + "Email:" & txtEmail.Text + Environment.NewLine + txtMessage.Text
' your remote SMTP server IP.
Dim smtp As New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.Port = 25
smtp.Credentials = New System.Net.NetworkCredential("user", "pass")
smtp.EnableSsl = True
smtp.Send(Msg)
'Msg = null;
lbltexts.Visible = True
End Sub
End Class
Your code looks correct, but you're sending it through Gmail. Their SMTP server is likely rewriting the FROM field so that it matches the email address you're using for authentication.
You might want to see this question.
Alternatively, you can send your mail through an SMTP server that doesn't do this.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
Button2.Click
Dim MyMessage As New MailMessage
Try
MyMessage.From = New MailAddress(TextBox2.Text)
MyMessage.To.Add("Example#gmail.com")
MyMessage.Subject = TextBox3.Text + body
MyMessage.Body = TextBox4.Text
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.Port = 587
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential("Example#gmail.com", "Password")
SMTP.Send(MyMessage)
MessageBox.Show("Success")
Catch ex As Exception
MessageBox.Show("failed To Send")
End Try
End Sub
Try this Code for Send Email For Your Specified Mail Id

The parameter 'from' cannot be an empty string. Parameter name: from

Ok, so basically I have a comments form on my website. People but comments in the comment box and it emails the comments to my email. The email part works fine and goes through. However, the error comes when I try to clear the form fields. It gives me an error saying:
Error: The parameter 'from' cannot be an empty string.
Parameter name: from
So anyone have any thoughts? like I said the email part works fine its just clearing the fields is giving me trouble
here is the code:
Imports System.Web.Mail
Imports System.Text
Partial Class Pages_ContactUs : Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim body As String = "From: " + YourName.Text + " " + Environment.NewLine + "Email: " + YourEmail.Text + Environment.NewLine + Environment.NewLine + "Message: " + Environment.NewLine + Comments.Text
Dim MM As New System.Net.Mail.SmtpClient
MM.EnableSsl = True
MM.Host = "smtp.gmail.com"
Dim cred As New System.Net.NetworkCredential("myEmail#gmail.com", "myPassword")
MM.Credentials = cred
MM.Send(YourEmail.Text, "myEmail#gmail.com", Subject.Text, body)
ClearFields()
lblEmail.ForeColor = Drawing.Color.Green
lblEmail.Text = "Your message has been sent successfuly"
lblEmail.Visible = True
End Sub
Protected Sub ClearFields()
YourName.Text = ""
YourEmail.Text = ""
Comments.Text = ""
Subject.Text = ""
End Sub
End Class
You must supply it, just make it yours. Basic pattern:
Using SmtpServer As New SmtpClient()
Using mail As New System.Web.Mail.MailMessage()
Try
SmtpServer.UseDefaultCredentials = True
SmtpServer.EnableSsl = True
SmtpServer.Credentials = New Net.NetworkCredential(un, pw)
SmtpServer.Port = 25
SmtpServer.Host = "smtp.address"
mail.From = New MailAddress(sender)
mail.To.Add(some address)
mail.Subject = subject
mail.Body = body
mail.Sender = New MailAddress(sender)
SmtpServer.Send(mail)
Catch
End Try
End Using
End Using

Getting an error when sending email through Gmail SMTP

Basically I have attempted to send an email when a button is pressed.
With the following code, I get an error that says something about 'The SMTP server requires a secure connection or the client was not authenticated'.
What is causing this error?
Imports System.Net.Mail
Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New _
Net.NetworkCredential("MYEMAIL#gmail.com", "MYPASSWORD")
SmtpServer.EnableSsl = True
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpServer.UseDefaultCredentials = False
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("MYEMAIL")
mail.To.Add("SENDINGADRESS")
mail.Subject = "Test Mail"
mail.Body = "This is for testing SMTP mail from GMAIL"
SmtpServer.Send(mail)
MsgBox("mail send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
You're very close, you need to set the following properties as well.
SmtpServer.EnableSsl = True
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpServer.UseDefaultCredentials = False

Resources