Sending an e-mail in asp.net using vb - asp.net

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.

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)

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.?

I have find many answers on this problem but still no solution to my problem. I am using this code to send Account Activation mail to the user. But i get this error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."
using (MailMessage mm = new MailMessage("sender#gmail.com", txtEmail.Text))
{
mm.Subject = "Account Activation";
string body = "Hello " + txtUsername.Text.Trim() + ",";
body += "<br /><br />Please click the following link to activate your account";
body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
body += "<br /><br />Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("sender#gmail.com", "<password>");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
//var client = new SmtpClient("smtp.gmail.com", 587)
//{
// Credentials = new NetworkCredential("myusername#gmail.com", "mypwd"),
// EnableSsl = true
//};
//client.Send("myusername#gmail.com", "myusername#gmail.com", "test", "testbody");
}
The problem is like water, code executes from top-to-bottom. You're passing the credentials, but at the same time setting the UseDefaultCredentials to true. Which causes this problem. Most of the servers require authentication, such as EnableSsl and credentials (username/password combination) in order to send the emails from their server.
So, the solution would be to remove this following line,
smtp.UseDefaultCredentials = true;
Then the code would execute and the email would be sent.

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>

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

asp.net send mail in vb.net

This is web config
<appSettings>
<add key="SmtpServer" value="gmail.com"/>
<add key="SmtpUtilisateur" value="superman#gmail.com"/>
<add key="SmtpPassword" value="12345678"/>
</appSettings>
This my vb method
Sub SendSimpleMail()
Dim Message As New Mail.MailMessage
Dim utilisateur As String
Dim pass As String
Dim server As String
utilisateur = ConfigurationSettings.AppSettings("StmpUtilisateur")
pass = ConfigurationSettings.AppSettings("SmtpPassword")
server = ConfigurationSettings.AppSettings("SmtpServer")
Message.From = "superman#gmail.com"
Message.To = "superman#gmail.com"
Message.Subject = "test"
Message.Body = "salut je voulais savoir comment tu allais"
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1")
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", utilisateur)
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtppassworld", pass)
SmtpMail.SmtpServer = server
Try
SmtpMail.Send(Message)
Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub
I get an error like the "transport fail in connection to server"
I don't know why this is not work well...
Thank's for helping me!
This in vb.net
First, it is recommended to use System.Net.Mail instead of SmtpMail, since the latter has been declared obsolete by Microsoft.
Second, the Gmail SMTP server requires a secure connection which can be set using SmtpClient.EnableSsl.
Your example could be changed to the following:
Sub SendSimpleMail()
Dim utilisateur As String = ConfigurationSettings.AppSettings("StmpUtilisateur")
Dim pass As String = ConfigurationSettings.AppSettings("SmtpPassword")
Dim server As String = ConfigurationSettings.AppSettings("SmtpServer")
Dim Message As New Mail.MailMessage()
Message.From = "superman#gmail.com"
Message.To = "superman#gmail.com"
Message.Subject = "test"
Message.Body = "salut je voulais savoir comment tu allais"
' You won't need the calls to Message.Fields.Add()
' Replace SmtpMail.SmtpServer = server with the following:
Dim client As New SmtpClient(server)
client.Port = 587
client.EnableSsl = true
client.Credentials = new System.Net.NetworkCredential(utilisateur,pass);
Try
client.Send(Message)
Catch ex As Exception
' ...
End Try
End Sub
If you replace the appsettings in the web.config with the following specific block, the SmtpClient will automatically configure itself accordingly:
<system.net>
<mailSettings>
<smtp from="superman#gmail.com">
<network host="smtp.gmail.com"
password="12345678"
userName="superman#gmail.com"
enableSsl="true"
port=587/>
</smtp>
</mailSettings>
</system.net>
This would reduce your method to:
Sub SendSimpleMail()
Dim Message As New Mail.MailMessage()
Message.To = "superman#gmail.com"
Message.Subject = "test"
Message.Body = "salut je voulais savoir comment tu allais"
Dim client As New SmtpClient()
Try
client.Send(Message)
Catch ex As Exception
' ...
End Try
End Sub
MailMessage msg = new MailMessage {From = new MailAddress(txtGAddress.Text, “Sender’s Name”)};
msg.To.Add(new MailAddress(txtTo.Text));
msg.Subject = txtSubject.Text;
msg.Body = txtMessage.Text;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient
{
Host = “smtp.gmail.com”,
Credentials = new NetworkCredential(txtGAddress.Text, txtGPassword.Text),
Port = 587,
EnableSsl = true
};
Label1.Visible = true;
try
{
smtp.Send(msg);
Label1.Text = “Email sent accessfully.”;
}
catch (Exception exeption)
{
Label1.Text = exeption.Message;
}
You will need to set the port number to be used when sending via gmail using the smtpserverport property
I think this is 587 for Google
Also I believe that gmail will require an SSL connection which you can set using the smtpusessl property

Resources