Sending emails via c# code

  • Thread starter Thread starter Joe_Black
  • Start date Start date
J

Joe_Black

Hi all,

I have googled this and found some answers but none that give me exactly
what I'm after, maybe someone here can help.

I have an app that will be distributed to users for their laptop and
home PC's, I have no idea what operating system they are using although
it will be at least Win 98, I also have no idea what mail client they
will be using or which ISP their mail account is with. I would like to
send an email from the app using their default mail account, I had a
look at the System.Web.Mail classes, specifically MailMessage class, but
this requires a mail server, how would I get this information from the
system? also it requires a mail from field which I would like to get
from the system without asking the user to enter it, again how is this
achieved?

Is there another way to do what I want to do, I know that there are the
Indy components but I'd rather avoid having to distibute more Dll's,
unless the Indy route is the best.

The only thing I can guarantee is that the target machine that this app
is installed on will have a mail account.

Many thanks

Joe
 
I would build the SMTP server property as configurable in your application.

Try and standardise the from address if possible - maybe use the machine
name or currently login name.
 
Adam said:
I would build the SMTP server property as configurable in your application.

Hi Adam,

Hmmm, I'd like to think the users would have the abilty to do enter the
smtp server details :-), however our target audience wont really be that
computer literate. Also the only time the email part of the app will be
needed is when they register their software, so ideally I'd like it to
be a simple case of click and go for them.
Try and standardise the from address if possible - maybe use the
machine name or currently login name.

Does this mean that the from address does not have to be a valid email
address? i.e.

Their actual email is
(e-mail address removed)

but the from address in the MailMessage object could be set to
(e-mail address removed)

Many thanks
Joe
 
I can't see how you could cope with all the different configurations without
programmatically checking properties of an Outlook profile for example. Even
this relies on the user using Outlook.

You could make the app ask for the server after first installing and then
never bother the user again. Then you just need to let the user know the
details required, i.e. their SMTP server. (If you know their IP, you could
look this up in advance to help them)

The 'from' address is your choice to a certain extent. Admittedly I've only
used a custom address when I've been in control of the SMTP server itself
and can enable relaying etc.
 
Adam said:
I can't see how you could cope with all the different configurations without
programmatically checking properties of an Outlook profile for example. Even
this relies on the user using Outlook.

You could make the app ask for the server after first installing and then
never bother the user again. Then you just need to let the user know the
details required, i.e. their SMTP server. (If you know their IP, you could
look this up in advance to help them)

The 'from' address is your choice to a certain extent. Admittedly I've only
used a custom address when I've been in control of the SMTP server itself
and can enable relaying etc.

I see the problem, what about this then:

Can I find the default mail account, and tell it to create an email with
my email address, subject and body in it. This would negate the need to
know any settings for SMTP etc. but would rely instead on an external
app, like outlook express for instance. This would also mean if the user
had a hotmail account that does not use SMTP my app could still send the
email, although the user themselveswould have to click "Send".

Thanks

Joe
 
I would've said it was far easier to use MailMessage than to start checking
for mail profiles. Outlook these days is quite locked down security wise so
I'm not sure how automatic you could make this. I would almost definitely go
for prompting for the smtp server.

Depending on the context on which you're developing this app, could you not
use your own SMTP server and therefore hardcode this address in your app?
This way a) you wouldn't need to worry about the user knowing any mail
information, and b) you could enable relaying.
 
The only thing I can guarantee is that the target machine that this app is
installed on will have a mail account.

Can you also guarantee that the target machine will have Internet accesss?
If so, why not abandon the email approach altogether and write a webservice
which the target machines will connect to?
 
Adam said:
I would've said it was far easier to use MailMessage than to start checking
for mail profiles. Outlook these days is quite locked down security wise so
I'm not sure how automatic you could make this. I would almost definitely go
for prompting for the smtp server.

Depending on the context on which you're developing this app, could you not
use your own SMTP server and therefore hardcode this address in your app?
This way a) you wouldn't need to worry about the user knowing any mail
information, and b) you could enable relaying.

So, from the last paragrah above, does this mean i could simply use our
own SMTP server, and send mails via that even though the users PC has no
settings or knowledge of the existence of our smtp server?

If this is so then it would definitely seem the best alternative

What if our SMTP server needs an user name and password, if I hard coded
it into my application could someone then hack the app and find our SMTP
server info? I have little experience in the SMTP/POP3 programming world
so these questions may be stupid...

Many thanks

Joe
 
Mark said:
Can you also guarantee that the target machine will have Internet accesss?
If so, why not abandon the email approach altogether and write a webservice
which the target machines will connect to?

Yes, they will have to have an internet connection on their machine as
part of the apps functionality is to connect to our hardware via an ADSL
link.

I'm not sure about the web service thing though, I have no real
experience with them so I'm not sure exactly what your suggesting? I
must admit though I wouldn't mind having a go with the web services.

Basically all I want to do is get information about plugin modules that
the user has installed on their machine, so that they can register these
modules to use them. I need to get the registration information from the
users machine to an email address on our mail server so that the people
generating the key codes to activate the modules can send an email back
to the user with the activation key.

I can't automate this via a web server app as I have been specifically
requested to perform the registration by email or phone.

Many thanks

Joe
 
You need to open up access to your SMTP server and essentially make it
public on the internet.

You could counteract the security issue by limiting its use to the IP
addresses of your clients, although this obviously has an admin overhead. If
you could do this though, it wouldn't matter if the SMTP server was
discovered by hacking your app. Chances are your server will be discovered
anyway.
 
Adam said:
You need to open up access to your SMTP server and essentially make it
public on the internet.

You could counteract the security issue by limiting its use to the IP
addresses of your clients, although this obviously has an admin overhead. If
you could do this though, it wouldn't matter if the SMTP server was
discovered by hacking your app. Chances are your server will be discovered
anyway.

I'm Scared of that option.

I think I may just do as you suggested earlier and ask them for the SMTP
server information at initial start up. If they don't know it off hand
they will have to contact our customer support department who can guide
them through finding it.

Thanks a lot for your help Adam.

Joe
 
Don't use their e-mail client. Use yours.

Bring them to a web page where they enter the info that they need. That
info is e-mailed from your server to the address you have. The format is
fine.

If you need to validate, then have them enter their e-mail address on the
page. Then send them an e-mail with a link. The link will contain a URL
code referring to their particular registration record. They click the
link, and you know that they received the e-mail, so the e-mail address can
be reasonably assumed to belong to them.

Alternative: set up a web service on your web server, and have the app send
data to it.

Don't use System.Web.Mail in client code. That is a server-side option.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Yes, they will have to have an internet connection on their machine as
part of the apps functionality is to connect to our hardware via an ADSL
link.

Then you're home and dry!
I'm not sure about the web service thing though, I have no real experience
with them so I'm not sure exactly what your suggesting? I must admit
though I wouldn't mind having a go with the web services.

OK - let's take a step back here...

1) If you originally intended to use email, presumably you had some way of
automatically generating the body of the mail message to contain the
information you require to do your registration...

2) Simply create a web service which accepts this data as a string parameter
(or whatever), and then process it exactly as you would if it had been
emailed to you.
 
Back
Top