Creating an SMTP Connection

T1TAN23

Member
Joined
Jun 15, 2006
Messages
9
Location
Austin, TX
Programming Experience
3-5
Is there some built in way to connect and send email via an SMTP Connection in VB.NET without having to install some third party dll?

I thought CDONTS was a possiblity but I can't seem to find any examples anywhere on how to do it.

Can some one please point me in the right direction?

~Trent~
 
Believe it or not... it's built right into .NET:

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myAddress As New System.Net.Mail.MailAddress("MyEmail@dontspamme.com", "From Me")
        Dim myRecip As New System.Net.Mail.MailAddress("you@somedomain.com", "Your Name")
        Dim myMail As New System.Net.Mail.MailMessage
        myMail.Sender = myAddress
        myMail.From = myAddress
        myMail.To.Add(myRecip)
        myMail.Subject = "This is a test"
        myMail.Body = "This is a test email sent using the System.Net.Mail namespace. I wonder how long it will take to arrive."
        myMail.IsBodyHtml = False

        Dim mySMTP As New System.Net.Mail.SmtpClient
        mySMTP.Host = "smtp server goes here"
        mySMTP.Port = 25
        mySMTP.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
        mySMTP.Credentials = New System.Net.NetworkCredential("outgoing user name", "password here")
        mySMTP.Send(myMail)

    End Sub

This is for 2005 ( FW2.0) .... for 2003 (FW 1.0/1.1) I think it's in the System.Web namespace....

-tg
 
Thanks for the pointer.

I am VS 2003 with .NET 1.1 so I was able to find and import the System.web.dll as a reference to my project to get to MailMessage.

I was able to send a test email from my app with out any problems, but my question now is...

How do I test the connection to the SMTP Server without actually sending an email. Is there some type of error code I can look for or is there something that I am missing so that once I set the SmtpMail.SmtpServer, is there some kind of connection state I can look for?
 
There is no "connection" .... you simply send it. If it was successfull, the mail was sent. If there was an error, you should be able to trap for it and see what went wrong.

-tg
 
I have seen in other third party .NET email apps that most of them have a connection state or at least a way to simply test the connection. I have no plans on spending any money to get this done, so I guess my solution will be to alert the user that I was either able to send them a test email or it failed and show them why.

I have made some changes on the SMTP server and now I am getting the dreaded "Could not access 'CDO.Message'" error. I have looped through and viewed all of the innerexceptions and i have dug all around trying to figure out my problem. This site has helped quite a bit http://www.systemwebmail.com/faq/4.2.3.aspx
at least showed me what i need to be looking for, but I have made all the suggested changes and I am still getting the error. I setup the SMTP on my localhost and everything works just fine so I have eliminiated the possibility of there being something wrong with the code.

Anyway, thanks for all your help TechGnome it is much appreciated.
 
there is one other thing that could be a possibility that wasn't mentioned in the site you listed: the SMTP port has been remaped to another port (defaULT IS 25). I've seen this done at some places to prevent unauthorized use of their SMTP (the theory being that you would have to be in the know to get the right port number).

-tg
 
Back
Top