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
Related
I am working on a VB application that sends an HTML email. I found this, and am trying to implement it.
'create the mail message
Dim mail As New MailMessage()
'set the addresses
mail.From = New MailAddress(EmailFrom, "Display Name")
mail.To.Add(EmailTo)
'set the content
mail.Subject = "Subject Line"
mail.Body = "Message body"
mail.IsBodyHtml = True
'send the message
Dim smtp As New SmtpClient("127.0.0.1")
smtp.Send(mail)
I don't have any info on the argument passed to SmtpClient() for this code snippet.
I have found videos showing how to connect to google smtp using "smtp.gmail.com" and a specific port. However, This would only be practical if all of the users of my application will have Gmail. I don't want to force them to have one in order to have access to the functionality.
Could any of you experts provide me with some help on dynamically selecting the correct smtp server and port based on their email?
for example, email is stored in a variable called email:
Dim email As String = "me#gmail.com"
This email is coming from MySql database so how do I take variable string and check which ISP they use and then have my code use the correct smtp and port for that ISP?
Also, I've read that now days authentication is required so I will likely prompt user with input box before calling procedure that sends the email.
Here's some code showing that:
Hotmail:
Smtp.live.com
Port: 587
Gmail:
smtp.gmail.com
Port: 587
Yahoo:
smtp.mail.yahoo.com
Port: 465
Aol:
smtp.aol.co.uk
Port: 587
imports system.net.mail
dim Mail as new mailmessage
Try
mail.From = New MailAddress("FROM EMAIL")
mail.To.Add(TO EMAIL)
mail.Subject = "SUBJECT"
mail.Body = "BODY"
Dim s As New SmtpClient("HOST")
s.Port = PORT
s.EnableSsl = True
s.Credentials = New System.Net.NetworkCredential("FROM EMAIL, "FROM PASSWORD")
s.Send(Mail)
MsgBox("E-mail was sucsessfully sent!", MsgBoxStyle.Information, "Information")
Catch ex As Exception
End Try
I'm completely stumped. Any help would be greatly appreciated. I am also open to any ideas that accomplish same feat even if not in accordance with first code snippet. Thanks in advance.
Here is the code I have to send the email:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
Dim SmtpServer As New SmtpClient
SmtpServer.EnableSsl = True
Dim mail As New MailMessage
SmtpServer.Credentials = New Net.NetworkCredential("Frrizzeh#gmail.com", "Password here")
SmtpServer.Port = "587"
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage
mail.From = New MailAddress("Frrizzeh#gmail.com")
mail.To.Add("Frrizzeh#gmail.com")
mail.Subject = TextBox1.Text
mail.Body = TextBox2.Text
SmtpServer.Send(mail)
Catch ex As Exception
MsgBox(ex.Message)
End Try
However when I run the program it just returns this error:
The SMTP server requires a secure connection or the client was not authenticated.
The server server response was: 5.5.1 Authentication required.
Any ideas where I am going wrong?
Try enabling less secure password from: https://www.google.com/settings/security/lesssecureapps so you can login from your app.
Also, try adding the following properties
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpServer.UseDefaultCredentials = False
You might have two step verification enabled and if so you need to generate an app specific password for you to use. Ive answered this and provided code in another question like this before. See link below...
Fail Sending Email Unable To Connect the Remote Server
I had this problem and fixed it by changing my gmail password to a stronger one with upper, lower, symbol, numeric.
It was funny that it worked fine from localhost but failed from my server.
I have a basic vb.net windows form application and is wondering how I would go about sending an email? I have no servers set up or anything like that, I was wondering if I could still send it without setting up all that stuff. This is the code I have now:
Private Sub sendEmail(ByVal golfersTable As DataTable)
'create the mail message
Dim mail As New MailMessage()
Dim SMTP As New SmtpClient()
'set the addresses
mail.From = New MailAddress("someone#gmail.com")
mail.To.Add("someone#gmail.com")
'set the content
mail.Subject = "Golf Quotas"
mail.Body = "Golfer Name" & "-----------------------------------------------" & row.Item("Average Quota")
For Each row As DataRow In golfersTable.Rows
mail.Body = row.Item("Golfer Name") & "---------------" & row.Item("Average Quota")
Next
'set the server
SMTP.EnableSsl = True
SMTP.UseDefaultCredentials = False
SMTP.Port = "465"
'SMTP.Send(mail)
Try
SMTP.Send(mail)
MsgBox("Your Email has been sent sucessfully - Thank You")
Catch ex As Exception
MsgBox("Message Failed To Send" & ex.ToString)
End Try
End Sub
I have gotten nothing to work at all from everything that I have tried.... Idk what I'm doing wrong but it's not working. I tried a nslookup www.gmail.com in cammand prompt but it said that domain not found ?
If you are using a GMail account to send the emails then you can use Google's SMTP server, smtp.gmail.com.
More details here:
http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm
i have the following code,i'm trying to send an email from my windows application but it's not working... any help ? note that i'm using vb.net and i'm not getting any errors.. i'm just not receiving any emails !
Private Sub senemail()
'create the mail message
Dim mail As New MailMessage()
'set the addresses
mail.From = New MailAddress("jocelyne_elkhoury#inmobiles.net")
mail.To.Add("jocelyne_el_khoury#hotmail.co.uk")
'set the content
mail.Subject = "This is an email"
mail.Body = "this is a sample body"
'send the message
Dim smtp As New SmtpClient("127.0.0.1")
smtp.Send(mail)
End Sub
In my experience, sending email via .net (and vb6 before it) is weird. Sometimes it should work and just doesn't, sometimes because of .net bugs and sometimes incompatibility with the smtp server. Here is some code that works on VB 2008, and it should work with 2010.
Using msg As New MailMessage(New MailAddress(fromAddress, fromName), New MailAddress(toAddress))
Try
Dim mailer As New SmtpClient
msg.BodyEncoding = System.Text.Encoding.Default
msg.Subject = subject
msg.Body = body
msg.IsBodyHtml = False
mailer.Host = mailserver
mailer.Credentials = New System.Net.NetworkCredential(username, password) ' may or may not be necessary, depending on the server
mailer.Send(msg)
Catch ex As Exception
Return ex.Message
End Try
End Using ' mailmsg
If this doesn't work, try using localhost instead of 127.0.0.1 for the mailserver.
If that doesn't work, try using your system name (the one that shows up on the network) for the mailserver.
If that doesn't work, try using an external smtp server with your own username and password (just for testing).
profit.
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).