Getting an error when sending email through Gmail SMTP - asp.net

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

Related

error with sending mail vb.net

i'm having a problem with sending mail. my code is so easy as below.
when this code starts it gives the error:
Request for the permission of type 'System.Net.Mail.SmtpPermission,
System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' failed.
when i searched for this error, they recommend changing web.config trust full setting or port from 587 to 25 and other stuff, but none work. what else could it be?
Sub MailGonder()
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Host = "mail.excelinefendisi.com"
SmtpServer.Credentials = New Net.NetworkCredential("info#excelinefendisi.com", "mypassword")
SmtpServer.Port = 587
mail.From = New MailAddress("info#excelinefendisi.com")
mail.To.Add("volkan.yurtseven#hotmail.com")
mail.Subject = "Test Mail"
mail.Body = "This is for testing SMTP"
SmtpServer.EnableSsl = True
SmtpServer.Send(mail)
End Sub
Use this code, work perfect for me :
Dim iphost As IPHostEntry = Dns.GetHostByName(Dns.GetHostName())
Dim Emailmessage As New MailMessage()
Emailmessage.From = New MailAddress("from_Email#hotmail.com")
Emailmessage.To.Add("To_Email#hotmail.com")
Emailmessage.Subject = "tTest Mail"
Emailmessage.Body = ("This is for testing SMTP")
Dim smtp As New SmtpClient("smtp.live.com")
smtp.Port = 587
smtp.EnableSsl = True
smtp.Credentials = New System.Net.NetworkCredential("your_email#hotmail.com.com", "Password")
smtp.Send(Emailmessage)

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

From Email Name in ASP.NET

I am trying to send email from X#gmail.com to Y#yahoo.com using System.Net.Mail,
Below is my code
Dim message As MailMessage = New MailMessage()
message.From = New MailAddress(Me.txtFromEmail.Text, Me.txtFromName.Text)
message.Sender = New MailAddress(Me.txtFromEmail.Text, Me.txtFromName.Text)
message.ReplyToList.Add(New MailAddress(Me.txtFromEmail.Text))
message.To.Add(New MailAddress(Me.txtTo.Text))
message.Subject = Me.txtSubject.Text & " " & Now.ToString
message.Body = Me.txtMessage.Text
If Me.txtCC.Text <> "" Then message.CC.Add(Me.txtCC.Text)
If Me.txtBCC.Text <> "" Then message.Bcc.Add(txtBCC.Text)
Dim mclient As SmtpClient = New SmtpClient()
mclient.Host = "smtp.gmail.com"
mclient.Port = 587
mclient.EnableSsl = True
mclient.Credentials = New System.Net.NetworkCredential("email#myapp.com", "somepassword")
mclient.Send(message)
Above code sends email without any error....
Issues: At the recipient inbox it listed as Sender Name <email#myapp.com> instead of Sender Name <x#gmail.com>
How can I set it as Sender Name <x#gmail.com>

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

Sending an e-mail in asp.net using vb

error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
I tried all things that i can do for this error ,but unable to solve this. Please help!!
My .net code for sending e-mail is as follows:
Try
Dim oMsg As MailMessage = New MailMessage()
Dim sendr, receivr As MailAddress
sendr = New MailAddress("username#gmail.com")
receivr = New MailAddress(TextBox4.Text)
oMsg.From = sendr
oMsg.To.Add(receivr)
oMsg.Subject = "Asp.net testing"
oMsg.IsBodyHtml = True
oMsg.Body = "Hi everyOne"
'oMsg.BodyFormat = MailFormat.Html
'oMsg.Body = "<HTML><BODY><B>Dear " & TextBoxName.Text & "</B></br></br>Congratulations! You have been successfully registered.</br></br>"
'oMsg.Body = oMsg.Body & "Your Username is: " & TextBox1.Text & "</br>Your Password is: " & TextBox2.Text & " </br>Regards,</br>team SMVDU.</BODY></HTML>"
Dim client As SmtpClient = New SmtpClient()
Dim cred As System.Net.NetworkCredential = New System.Net.NetworkCredential("username#gmail.com", "*******", "smtp.gmail.com")
client.Credentials = cred
client.Host = "smtp.gmail.com"
client.Port = 587
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.EnableSsl = True
client.UseDefaultCredentials = False
client.Send(oMsg)
Response.Write("<script>alert('E-mail sent')</script>")
Catch ex As Exception
Response.Write("" & ex.Message)
End Try
I am using windows 7 home basic. And all things that can be given is correct i.e email, password from my side.

Resources