smtp.send failure sending email message from client machine - vb.net

Mail.From = New Net.Mail.MailAddress(test#gmail.com)
Mail.To.Add("exc#gmail.com")
Mail.Subject = "Test"
Mail.Body = "Test"
Try
Dim mySmtp As New Net.Mail.SmtpClient("server")
mySmtp.Send(Mail)
Catch ex As Exception
MsgBox(ex.Message)
End Try

Try disabling the your Anti Virus as they can block SMTP calls.

Related

Sending an email still have an error using VB.Net

I want to send an email when user click btnSubmit. My problem is when I click the btnSubmit it gives me error at Catch ex As Exeption. I'm using Web Form.aspx.vb
Here is my code
Dim mail As MailMessage = New MailMessage
mail.From = New MailAddress("example#gmail.com")
mail.To.Add(New MailAddress("example#gmail.com"))
mail.Subject = "TEST SUBJECT"
mail.Body = "Test Message"
mail.IsBodyHtml = True
Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
client.EnableSsl = True
Try
client.Send(mail)
MsgBox("Sending Email succeeded") 'Return True if there is nothing error while sending email.
Catch ex As Exception
MsgBox("Sending Email failed. Please try again")
End Try

How to test SMTP connection before SMTPClient.Send [VB.NET]

Pretty new to programming here.
The program I'm currently working on needs to send an email with logs.
Working great if I'm using the right server host but when I'm trying with a "false" server host my program sure can't connect, but it immediatly crash, I can't raise any exception, can't tell the user he's doing something wrong, nothing.
So I guess I have to test the connection before SMTPClient.Send but I can't seem to find how...
How can I test a SMTP Server connection in VB.NET ?
That's what I'm using :
Try
Dim SmtpServer As New SmtpClient()
With SmtpServer
.EnableSsl = False
.UseDefaultCredentials = False
.Credentials = New Net.NetworkCredential(MailUser, MailPassword)
.Port = 25
.Host = ServerAdress
End With
Dim mail As New MailMessage()
With mail
.From = New MailAddress(MailSender)
.To.Add(MailReceiver)
.CC.Add(MailCC)
.Subject = MailObject
.Body = MailBody
End With
SmtpServer.Send(mail)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
1- I think you should try something like this:
Using tcp As New TcpClient
Try
tcp.Connect(ip, 25)
' server found
Catch ex As Exception
' server not found
End Try
tcp.Close()
End Using ' tcpclient
2- For test your smtp server this article can be usefull:
https://www.port25.com/how-to-check-an-smtp-connection-with-a-manual-telnet-session-2/
3- and also there is a smtp test class in this question's answers
.Net TcpClient and SmtpClient won't connect to my Smtp server

Failure sending mail Exception while sending mail through SMTP

Dim mail As New Net.Mail.MailMessage()
mail.From = New Net.Mail.MailAddress("abc#xyz.com")
mail.[To].Add("abc#xyz.com")
mail.IsBodyHtml = True
mail.Subject = "A subject line."
mail.Body = "A mail body message."
Dim smtp As New Net.Mail.SmtpClient("smtp.xyz.com", 25)
smtp.Credentials = New System.Net.NetworkCredential("abc#xyz.com","password")
Try
smtp.Send(mail)
MsgBox("Your Email has been sent sucessfully - Thank You")
Catch exc As Exception
MsgBox("Send failure: " & exc.ToString())
End Try
In Windows Application a simple SMTP using Credential, throwing Exception-
Failure sending mail.
Why it is throwing can anybody help me out.

cannot send yahoo email using vb.net

I'm working with a program that can send email supporting yahoo mail and gmail. And it works in gmail(if the sender utilizes gmail) But it won't work if the sender is using yahoo mail.
Here is my code:
mail.From = New MailAddress(TextBox2.Text)
mail.To.Add(New MailAddress(TextBox1.Text))
mail.Subject = TextBox4.Text
mail.Body = TextBox4.Text
mail.IsBodyHtml = True
Dim client2 As SmtpClient = New SmtpClient("smtp.mail.yahoo.com", 25)
Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
client.EnableSsl = True
client.Credentials = New System.Net.NetworkCredential(TextBox2.Text, TextBox3.Text)
Try
client.Send(mail)
Catch ex As Exception
MessageBox.Show("Sending email failed. Please Try again")
Looks like you might be using the wrong port? Try this
Dim client2 As SmtpClient = New SmtpClient("smtp.mail.yahoo.com", 587)
EDIT
OK, that didn't work. Actually, isn't the SMTP address also wrong?
Dim client2 As SmtpClient = New SmtpClient("plus.smtp.mail.yahoo.com", 587)
You could also wrap the whole program in a Try block and catch any SmtpException and write out the special SmtpStatusCode:
Try
' Blah blah '
Catch (SmtpException e)
Console.WriteLine("Error: {0} {1}", e.StatusCode, e.ToString)
End Try
Yahoo uses Port 465 for non-paying users (subscription services).

how to check if a line of code actually succeeded?

I'm making an email sending program and still I don't know how to check if the mail was really sent or not, because sometimes the program will have no error messages but the mail was not actually sent. Is there any other way on how to deal with this except for making use of try catch?
Here is my code:
Try
mail.From = New MailAddress(TextBox2.Text)
mail.To.Add(New MailAddress(TextBox1.Text))
mail.Subject = TextBox4.Text
mail.Body = TextBox4.Text
mail.IsBodyHtml = True
Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
If TextBox2.Text.Contains("#gmail.com") Then
client.EnableSsl = True
client.Credentials = New System.Net.NetworkCredential(TextBox2.Text, TextBox3.Text)
Try
client.Send(mail)
Catch ex As Exception
MessageBox.Show("Sending email failed. Please Try again")
End Try
End If
Catch
MsgBox("Please input the correct value!")
End Try
ProgressBar1.Value = 100
clear()
I would typically use try/catch for this sort of thing.
Instead of catching a Generic Exception you can catch SmtpException and SmtpFailedRecipientsException's.
SmtpException is thrown when a connection could not be made or operation timed out. SmtpFailedRecipientsException is thrown if The message could not be delivered to one or more of the recipients.
Converted MSDN Code
Try
client.Send(message)
Catch ex As SmtpFailedRecipientsException
For i As Integer = 0 To ex.InnerExceptions.Length - 1
Dim status As SmtpStatusCode = ex.InnerExceptions(i).StatusCode
If status = SmtpStatusCode.MailboxBusy OrElse status = SmtpStatusCode.MailboxUnavailable Then
Console.WriteLine("Delivery failed - retrying in 5 seconds.")
System.Threading.Thread.Sleep(5000)
client.Send(message)
Else
Console.WriteLine("Failed to deliver message to {0}", ex.InnerExceptions(i).FailedRecipient)
End If
Next
Catch ex As Exception
Console.WriteLine("Exception caught in RetryIfBusy(): {0}", ex.ToString())
End Try
Another problem you may run into is that the mail is being sent but it is not getting there due to spam filtering. If it works to one email address it should work for all ignoring the TextBox2 check for gmail address.
You can use a bool method which always returns true or false of your sending status.
If you have no error then please check the emailId whether it exists or not.