SMTP Exception - Message could not be sent
Hello everyone,
I'm creating an application for my own use that allows me to notify my lawnmowing customers when their grass was last cut, how much they owe, etc.
The application uses an SMTP client to send a MailMessage to the customers through my Yahoo! online email account. The code for sending the message is as follows:
Code:
Dim notification As New MailMessage
notification.Subject = "Grass Tracker Notification"
notification.To.Add("[email protected]")
notification.From("[email protected]")
notification.Body(emailbodystringcreatedearlier)
Dim Smtp As New SMTPClient()
Smtp.Host = "smtp.mail.yahoo.com"
Smtp.Port = "995"
Smtp.Credentials = New System.Net.Credentials("username", "password)
Smtp.EnableSsl = True
Smtp.Send(notification)
However, every time I try to send the message I get an error message that says "Smtp Exception" and "Message could not be sent" or something of that sort. I have tried the following to remedy the problem:
- Using different ports and clients found online
- Using a "Try...Catch" method to handle the exception. Every time I attempt to send the message the exception is thrown
Can anyone offer some advice on how to fix this? The exception is rather vague and doesn't offer a whole lot on what needs changed, like an improper port or something.
All advice is appreciated and Thanks,
Josh
Re: SMTP Exception - Message could not be sent
Your code contains a typo, the "password" is missing the closing double quote.
Why not post the complete text of the exception (ex.ToString())? It should provide a clue to why the email hasn't been sent.
Also, are you able to send emails from the specific account using an email client?
.
Re: SMTP Exception - Message could not be sent
honeybee,
Thanks for the help. Here is an abridged version of the error message:
Code:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 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 98.136.185.95:995
This is evidently some kind of trouble connecting to the port; I may have to try some other ports that I find online.
This is also the fixed code:
vb Code:
Dim notification As New MailMessage
notification.Subject = "Grass Tracker Notification"
notification.Body = "Hey There!"
Dim Smtp As New SmtpClient()
Smtp.Host = "smtp.mail.yahoo.com"
Smtp.Port = "995"
Smtp.Credentials = New System.Net.NetworkCredential("username", "password")
Smtp.EnableSsl = True
Smtp.Send(notification)
Catch ex As SmtpException
TextBox1.Text = ex.ToString
End Try
Also, could you describe how to send messages as an EmailClient? I haven't heard of this before and I'd like to give it a try.
Thanks,
Josh
Re: SMTP Exception - Message could not be sent
Are you sure you are connecting to the correct IP address and most importantly the correct SMTP port? Also do you require SSL or any other SMTP authentications enabled?
By an email client I mean setting up this email account ([email protected]) onto your computer in your Outlook Express/Outlook/Thunderbird or whatever email app you are using, and trying to send an email out from this account. If you are not comfortable with the raw code, it might be better to set up this account in your email client and try sending a sample email to another email account. You can check and tweak the SMTP settings in the email client till the email is successfully sent. Once that is done, you can use the same settings in your above code.
.