I want my users to send emails from a Windows app that I am developing in
Visual Basic.NET 2003. I found a good example of sending email to a SMTP
server using the SmtpMail class. However, using this, it seems, that the
user must install IIS on their computer.
Not true. Your example must be assuming that, since installing IIS by
default sets up an SMTP relay server. However, any SMTP server that your
program can reach will suffice.
Isn't there a class that will
detect whatever mail server is available on a computer and use that?
Private Function DetectSMTPServer( _
Optional ByVal host As String = "127.0.0.1") _
As Boolean
Dim tcp As New System.Net.Sockets.TcpClient
Dim ServerExists As Boolean = False
Try
tcp.Connect(host, 25)
ServerExists = True
Catch ex As Exception
ServerExists = False
Finally
tcp.Close()
End Try
Return ServerExists
End Function
This function returns true if there's an SMTP server answering at the
specified location. If you don't specify, it assumes the local machine.
How do
I create this functionality without having the user add any other programs or
change any previously defined configurations - just use their computer's
email system?
If you really want to use "their computer's email system", you might look
into MAPI. Here's some code I found that uses Outlook to send an email:
Dim mOutLookApp As Outlook.Application
Dim mNameSpace As Outlook.NameSpace
Dim mItem As Outlook.MailItem
Set mOutLookApp = New Outlook.Application
Set mNameSpace = mOutLookApp.GetNamespace("MAPI")
mNameSpace.Logon ,, False, True
Set mItem = mOutLookApp.CreateItem(olMailItem)
mItem.To = strEmailAddress
mItem.Subject = "This is for you"
mItem.HTMLBody = strMessage
mItem.Sensitivity = olConfidential
mItem.Importance = olImportanceHigh
mItem.Send
Or you could attempt to use the destination mail host for the address
you're sending to, by using DNS to look up the MX record for the
destination domain. No built-in classes to do this simply, but a few
people have written them. See
http://www.google.com/search?q=vb.net+MX+lookup
The following free COM object claims to do the MX lookup internally, giving
a very small footprint. But it's COM:
http://www.visualbuilder.com/component/viewcomponent.asp?id=360&devid=1
You could go crazy and scan the local LAN for an open port 25.
Or you could just ask the user if they know their mail server's name.
That's probably safe. Maybe use that as a default option, if other methods
fail.