Click here to Skip to main content
15,946,527 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to send an email message from my code via Yahoo.

I have built a class to accomplish this.

The class header declares:

VB
Dim mEMail As New MailMessage()
Dim SmtpServer As New SmtpClient()



The class new sub is:

VB
Public Sub New()
     MyBase.New()
     SmtpServer.Host = "smtp.mail.yahoo.com"
     SmtpServer.Port = 465
     SmtpServer.Credentials = New Net.NetworkCredential("‎<username>", "<mpassword>")


 End Sub



The send command in the class is:

VB
' send the message
Public Function Send() As Boolean
    Send = False

    If mToAddr = "" Then Exit Function
    If Body = "" Then Exit Function

    mEMail.To.Add(ToAddr)
    mEMail.From = New MailAddress("software@bellalunabeverages.com")
    mEMail.Subject = mSubject
    mEMail.Body = mHeader & vbCrLf & mBody & vbCrLf & vbCrLf & mFooter

    If AttachList.Count > 0 Then
        For i As Short = 0 To AttachList.Count - 1 Step 1
            mEMail.Attachments.Add(New Net.Mail.Attachment(AttachList(i).Trim))
        Next i

    End If

    Try
        SmtpServer.Send(mEMail)
        Send = True

    Catch ex As Exception

        'Console.WriteLine(ex.ToString)

        MsgBox("Error occured during email send " & vbCrLf & _
               "ERROR: " & ex.ToString)

        Send = False

    End Try


End Function



The exception that gets thrown is:

ERROR! System.Net.Mail.Smtpexception: Failure sending mail ---> System.IO.IOException: Unable to read data from thye transport connection: an existing connection was forcibly closed by the remote host. ....

What is the solution to this problem?
Posted

Have you added the reference System.Net.Mail??
 
Share this answer
 
Comments
KevinBrady57 9-Feb-13 21:56pm    
Yes. At the class definition Is "Imports System.Net.Mail"
Problem with the current code

Port 465 isn't supported by System.Net.Mail.SmtpClient.
You should go with Port 587 with EnableSsl = false.

So,the code will typically look like below...
VB
Public Sub New()
     MyBase.New()
     SmtpServer.Host = "smtp.mail.yahoo.com"
     SmtpServer.Port = 587
     SmtpServer.Credentials = New Net.NetworkCredential("‎<username>", "<mpassword>")
     SmtpServer.EnableSsl = false
 End Sub


NOTE
-> Send() function will be the same.
-> Make sure that yahoo username("‎<username>") and password("<mpassword>") are valid.


Thanks...
 
Share this answer
 
v2
Comments
KevinBrady57 10-Feb-13 12:11pm    
This works. Thank you very much. I had to change the "From" address to my yahoo email address. Does there have to be a "From" address?
Most welcome.
The from address can be any address not only yahoo.
But the <username> and <password> in function Net.NetworkCredential() should be yahoo username and password. Got it?

Reply me again with your understanding.
And yes there should be one From Address, it is mandatory.
KevinBrady57 10-Feb-13 12:27pm    
I understand. I tried testing it without a from address, and it wouldn't work. It doesn't work if I use some other email address other than my Yahoo one.

Also, do you know if they same thing can be done using Go Daddy (i.e. secureserver)? I tried it last week using code I found on-line, but couldn't get it to work.
But in my case, it worked. I have already tested before answering this.
For example I did like this-
1. username - myID@yahoo.com
2. password - myYahooPassword
3. from address - myID@gmail.com
4. to address - any mail address

And just provide the link of that Go Daddy code. I will try to find out the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900