error with sending mail vb.net - asp.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)

Related

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.

System.Net.Mail works as localhost but when I add it to my web server it does not

This program works when I "Debug" in VWD 2010 as localhost.
When I insert it into my webserver my Try...Catch get this error:
Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Here is my code:
Imports System.Net.Mail
Dim msg As New System.Net.Mail.MailMessage
Dim fromAdd As MailAddress = New MailAddress("nsamefrom#mail.com")
msg.[To].Add("nameto#email.com")
msg.Subject = "Email Works!!!!!!!!"
msg.From = fromAdd
msg.IsBodyHtml = True
msg.Priority = System.Net.Mail.MailPriority.Normal
msg.BodyEncoding = Encoding.Default
msg.Body = "<h1>Body!!!!!!</h1>"
Dim host = Request.Url.Host
Dim smtpClient As New SmtpClient("smtp.email.com", "587")
smtpClient.EnableSsl = True
smtpClient.UseDefaultCredentials = False
smtpClient.Credentials = New System.Net.NetworkCredential("user", "pass")
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
smtpClient.Send(Message)
replace port no for gmail smpt by 25,
it will worke on hosting server too.
Source : http://forums.asp.net/t/1624378.aspx

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

error sending email vb.net

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 67.69.240.69:25
Dim smtpClient As New SmtpClient()
Dim message As New MailMessage()
Dim fromAddress As New MailAddress("news#ffff.ca")
smtpClient.Host = "smtp10.bellnet.ca"
smtpClient.Credentials = New NetworkCredential("news", "1235!")
smtpClient.Port = "25"
message.From = fromAddress
message.To.Add("khs#ddddd.com")
message.Subject = "eyjeyjey afsdf"
message.Body = "Hello from aaaaa"
message.IsBodyHtml = True
smtpClient.EnableSsl = True
smtpClient.Send(message)
Bell Canada blocks the default SMTP port of 25.
See this link for a partial list.

Resources