Sending Email from a .NET Windows App

G

Guest

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. Isn't there a class that will
detect whatever mail server is available on a computer and use that? 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?

Thanks,
joe
 
A

Andrew Robinson

You need a SMTP server somewhere. Mail needs to be queued. If you program
exits before you mail is sent, the process needs to keep running.

How about setting up a central IIS / SMTP server? It will still work with
SmtpMail.

-A
 
R

Ross Presser

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.
 
G

Guest

To Ross and Andrew who responded to my "Sending Email" question. Let me
refine my question, especially now that I have looked back at the SMTP
example that I mentioned previously.

The example that I am using comes from the MS Book, "101 Visual Basic.NET
Applications". It is Application #75 in the book. When I run it, I get a
message that I don't have an SMTP Mail Server on my computer - which is not
true. Looking back at the discussion in the book, it says to change the
application setting in app.config to point to the server. The app.config
file contains the following information:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SmtpMail.SmtpServer" value="127.0.0.1"/>
</appSettings>
</configuration>

So, now my question is how do I find the 'value' (which I assume is an IP
address) for the SmtpServer? And, how would I do this programmatically so
that my users can install my program on their computer, the program can
identify the location of their Smtp Server, and emails can be sent from the
app?
 
J

John A. Bailo

joe215 said:
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. Isn't there a class that will
detect whatever mail server is available on a computer and use that? 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?

Unlike Linux, where a client runs a sendmail server by default, most XP
machines don't have a running 'mail server'. They typically connect to
a remote mail server by IP or by name. That server can be configured
in the SMPTServer object just as easily as a 'local' email server.

In fact, you can put it in the app.config file and use the same one for
everyone that can relay on that server.
 
G

Guest

Thanks, John. I did finally come to that realization. However, I'm not
knowledgeable about SMTP Servers. So now my question is:

Given an app.config file similar to the one below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SmtpMail.SmtpServer" value="127.0.0.1"/>
</appSettings>
</configuration>

My question is how do I find the IP address for the SmtpServer? And, how
would I do this programmatically so that my users can install my program on
their computer, the program can identify the location of their Smtp Server,
and emails can be sent from the app?

Thanks,
Joe
 
J

John A. Bailo

Well, it's tricky.

You have to let us know is this to be deployed internally on an
corporate network or widespread over the Internet?

If the latter then you are talking about pointing it to their ISP's
smptserver (that they use for Outlook Express or Thunderbird). If that
is the case, you would need to either

(a) run a config/install script that requests that IP
(b) read the registry or profile settings to pick out he smtp address

If it's a corporate network, then you would need to find if they run a
sendmail server or have a IIS/Smtp server that lets people relay on the
server.
 
G

Greg Burns

joe215 said:
The example that I am using comes from the MS Book, "101 Visual Basic.NET
Applications". It is Application #75 in the book. When I run it, I get a
message that I don't have an SMTP Mail Server on my computer - which is
not
true. Looking back at the discussion in the book, it says to change the
application setting in app.config to point to the server. The app.config
file contains the following information:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SmtpMail.SmtpServer" value="127.0.0.1"/>
</appSettings>
</configuration>

You do realize that 127.0.0.1 is the loopback address for your local
computer. Not sure why your example cannot find your local SMTP server if
you truely have it installed and working. Sometimes you'll see localhost
used in place of 127.0.0.1. Make sure you 127.0.0.1 allowed for relaying.
Control Panel->Admin Tools->IIS->Right-click default SMTP
server->Properties->Access Tab->Relay button->add 127.0.0.1.
So, now my question is how do I find the 'value' (which I assume is an IP
address) for the SmtpServer? And, how would I do this programmatically so
that my users can install my program on their computer, the program can
identify the location of their Smtp Server, and emails can be sent from
the
app?

I would imagine you are going to have to have a way for the users of your
app to specifiy their ISP's smtp server address inside your app (either an
IP address or its domain name (example: smtp.comcast.net ). Just like you
have to do when setting up an email account in Outlook Express.

Greg
 
G

Guest

This is to be a Windows app that can be used on any computer system whether
it is on a corporate network or a home computer.

So, where do I look in the registry to obtain the smtp address?

And, how do I programmatically obtain the necessary information about smtp
on a computer in a corporate network?

Thanks.
 
A

Andrew Robinson

I don't know that there is a programmatic method of finding the local SMTP
server or that it is listed anywhere in the registry. You might be able to
check for the existance of a local SMTP server but I don't think you will
find a listing in the registry pointing to a remote server. SMTP is a
service that you will have to configure on either your local machine or a
server and then point your application at that SMTP server.

Things are complicated by the fact that a significant number of ISPs don't
allow you to relay SMTP traffic from within their networks using your own
SMTP server. This is done to combat spam.

-Andrew
 
R

Ross Presser

So, where do I look in the registry to obtain the smtp address?

It won't be in the registry, unless there is a email client installed on
the machine configured to use it -- which there very well may be; most
people who use computers also use email. In which case it depends on the
email client: Outlook Express, Outlook 2000, Outlook 2003, Eudora, Netscape
..... there's no set place.
And, how do I programmatically obtain the necessary information about smtp
on a computer in a corporate network?

As I said in my previous message, you *could* scan the local LAN to see if
any machine has an open port 25. This can be considered unfriendly
behavior, though, and you should probably ask the user permission before
beginning a scan.

Another slightly sneaky way to do it would be to test all hostnames that
are in the local machine's DNS cache. If the user has been using email
lately, chances are his mail server will appear there. Shell out to the
commandline

IPCONFIG /DISPLAYDNS

and capture the results to a file. Go through the result file and extract
the host names from lines which contain the words "Record Name".

If you turn up empty, prompt the user to check his email or send a test
email, and try again.
 
J

John A. Bailo

He could also take some educated guesses.

Get the domain name of the host and put the word "mail" in front of it,
and use

mail.mydomainname.com

That is typically the name of the smtp/sendmail server on port 25.

It wouldn't always work of course.

An alternative is to run your own sendmail server or find one that
allows relaying and then you would just need to test to see if they can
access it (that is, port 25 is not blocked, as you mention).

If either of those two options are not possible, then the application
should throw a dialog saying "enter smpt/sendmail server".
 
J

John A. Bailo

BTW -- I'm sure you're a good guy and all...but just to be sure, please
assure us that you're not writing a spambot.
 
G

Guest

No, no! This is legit. The program is a data logger and will send an alarm
email message to a building supervisor if the data logging stops for some
reason.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top