468,772 Members | 1,843 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,772 developers. It's quick & easy.

Sending Email from a .NET Windows App

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

Nov 10 '05 #1
13 2774
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

"joe215" <jo****@discussions.microsoft.com> wrote in message
news:C6**********************************@microsof t.com...
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

Nov 10 '05 #2
On Thu, 10 Nov 2005 09:45:02 -0800, joe215 wrote:
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/compone...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.
Nov 10 '05 #3
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?

"Ross Presser" wrote:
On Thu, 10 Nov 2005 09:45:02 -0800, joe215 wrote:
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/compone...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.

Nov 10 '05 #4
joe215 wrote:
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.
Nov 10 '05 #5
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

"John A. Bailo" wrote:
joe215 wrote:
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.

Nov 10 '05 #6

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.

joe215 wrote:
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

"John A. Bailo" wrote:

joe215 wrote:
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.

Nov 10 '05 #7

"joe215" <jo****@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.com...
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

Nov 10 '05 #8
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.

"John A. Bailo" wrote:

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.

joe215 wrote:
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

"John A. Bailo" wrote:

joe215 wrote:

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.

Nov 10 '05 #9
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
"joe215" <jo****@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
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.

"John A. Bailo" wrote:

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.

joe215 wrote:
> 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
>
> "John A. Bailo" wrote:
>
>
>>joe215 wrote:
>>
>>>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.
>>

Nov 11 '05 #10
On Thu, 10 Nov 2005 15:10:07 -0800, joe215 wrote:
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.
Nov 11 '05 #11

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

Ross Presser wrote:
On Thu, 10 Nov 2005 15:10:07 -0800, joe215 wrote:

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.

Nov 11 '05 #12

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.

joe215 wrote:
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

Nov 11 '05 #13
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

"John A. Bailo" wrote:

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.

joe215 wrote:
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

Nov 11 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by David | last post: by
2 posts views Thread by Mr. x | last post: by
1 post views Thread by festivalman | last post: by
13 posts views Thread by joe215 | last post: by
3 posts views Thread by Ant | last post: by
31 posts views Thread by happyse27 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.