vb asp.net contact form - asp.net

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

Related

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

Asp.net email application mail comes twice when it sends

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

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

Specified cast error

I am using VS2012 with ASP.NET 4.5 and MySQL as my database provider.
I am getting a specified cast error now on code that was working fine at one point.
It is from my Register.aspx page for registration verification and the code is adapted from an asp.net website tutorial.
Here is the code, the error is on the Dim newUser as Guid line
Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser
FormsAuthentication.SetAuthCookie(RegisterUser.UserName, False)
Dim newUser As MembershipUser = Membership.GetUser(RegisterUser.UserName)
Dim newUserID As Guid = DirectCast(newUser.ProviderUserKey, Guid)
Dim urlBase As String = Request.Url.GetLeftPart(UriPartial.Authority) & Request.ApplicationPath
Dim verifyUrl As String = "VerifyNewUser.aspx?ID=" & newUserID.ToString()
Dim fullPath As String = urlBase & verifyUrl
Dim appPath As String = Request.PhysicalApplicationPath
Dim sr As New StreamReader(appPath & "EmailTemplates/VerifyUserAccount.txt")
Dim mailMessage As New MailMessage()
mailMessage.IsBodyHtml = True
mailMessage.From = New MailAddress("myacct#gmail.com")
mailMessage.To.Add(New MailAddress(RegisterUser.Email))
mailMessage.CC.Add(New MailAddress("myacct#gmail.com"))
mailMessage.Subject = "New User Registration"
mailMessage.Body = sr.ReadToEnd
sr.Close()
mailMessage.Body = mailMessage.Body.Replace("<%UserName%>", RegisterUser.UserName)
mailMessage.Body = mailMessage.Body.Replace("<%VerificationUrl%>", fullPath)
//Set up the smtp for gmail to send the email
Dim mailClient As New SmtpClient()
With mailClient
.Port = 587 'try 465 if this doesn't work
.EnableSsl = True
.DeliveryMethod = SmtpDeliveryMethod.Network
.UseDefaultCredentials = False
.Credentials = New NetworkCredential(mailMessage.From.ToString(), "password")
.Host = "smtp.gmail.com"
End With
mailClient.Send(mailMessage)
Dim continueUrl As String = RegisterUser.ContinueDestinationPageUrl
If String.IsNullOrEmpty(continueUrl) Then
continueUrl = "~/"
End If
Response.Redirect(continueUrl)
End Sub
When you, or anyone, creates a MembershipProvider you need to specify whether the ProviderUserKey is going to be an integer or a Guid. The default SqlMembershipProvider implements it a a Guid while the default MySqlMembershipProvider implements it as an int.
You could always implement your own provider by inheriting from one of the defaults and implementing your own version of the ProviderUserKey

configuration of asp.net mailmessage

Can someone help me pls. I am ready to deploy my web application. I have a contact form that i want users to send me a message. Ive created the smtp when i click on submit, i get the error message cannot be sent.
The application is still on my local machine maybe thats why. But i just want to know if this code is good for my form:
Imports System.Net.Mail
Partial Class contact
Inherits System.Web.UI.Page
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
If txtComments.Text.Length > 300 Then
args.IsValid = False
Else
args.IsValid = True
End If
End Sub
Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick
SendMail(txtEmail.Text, txtComments.Text)
End Sub
Private Sub SendMail(ByVal from As String, ByVal body As String)
Dim mailServerName As String = "SMTP.relay-hosting.secureserver.net"
Dim message As MailMessage = New MailMessage(from, "collins#collinsegbe.com", "feedback", body)
Dim mailClient As SmtpClient = New SmtpClient
mailClient.Host = mailServerName
mailClient.Send(message)
message.Dispose()
End Sub
End Class
The error is indicated on this line of code: mailClient.Send(message)
I will appreciate help from anyone
protected void Button9_Click(object sender, EventArgs e)
{
{
var fromAddress = new MailAddress("rusty109.stud#gmail.com");
var toAddress = new MailAddress(TextBox13.Text);
const string fromPassword = "commando1";
string subject = TextBox14.Text;
string body = TextBox12.Text;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
}
}
my old code when i wanted to email, you can use the gmail account if you wish, nothing in it... was set up to use this code :)
using System.Net.Mail;
using System.Net;
You need to add the username and password for the SMTP server by setting the SmtpClient's Credential property to an instance of the NetworkCredential class.

Resources