How do I send an email?

R

Randel Bjorkquist

First, I'm extremely new to C# and the Microsoft Visual Studio IDE. I am in
the middle of writing a small web application that communicates to a
database server and sends out email confirmations. I've got the database
connection going okay, but I'm having problems trying to find a good way to
send an email. I have found a couple products online, but buying a tool is
not an option. So I need to figure out how to use what I have.

I'm looking for a small coded example. If anyone has one, that would be
great. If not, can someone point me into a good direction.

Thanks,

Randel Bjorkquist
 
B

Brian

Hello Randel,

Here's a sample. You'll need to add a reference to "System.Web" first.


using System;
using System.Web;


public void Send()
{

SmtpMail.SmtpServer = "NameOfYourMailServer" //usually mail.xxxxxx.com

MailMessage msg = new MailMessage();
msg.From = "(e-mail address removed)";
msg.To = "(e-mail address removed)";
msg.Body = "Test Message";
msg.Subject = "Test";
msg.BodyFormat = MailFormat.Text;

SmtpMail.Send(msg);

}
 
R

Randel Bjorkquist

Hey Brian,

Thanks for the extremely quick reply. I've tried your code, but it wont
compile for me. Some of the errors I'm getting are: "The type or namespace
name 'SmtpMail' could not be found..." and "The type or namespace name
'MailMessage' could not be found...". I'm using VS .NET 2003 Professional.
Is that the problem? Are these only availible in the Enterprise version?

I'll keep looking. But again, thanks for the help,

Randel
 
B

Brian

Hello Randel,

You need to make sure you add a reference to "System.Web".

In the "Solution Explorer" window, right-click on the "References" folder
under your project. Choose "Add Reference"
In the window that shows up, scroll down until you see "System.Web". Click
on it and choose "Select". Then click "Okay" at the bottom.

I left out the class struction from the example, here is a full example:

using System;
using System.Web;

namespace sandbox.SendEmailExample
{

class program
{

[STAThred]
static void Main(string[] args)
{

SmtpMail.SmtpServer = "NameOfYourMailServer" //usually
mail.xxxxxx.com

MailMessage msg = new MailMessage();
msg.From = "(e-mail address removed)";
msg.To = "(e-mail address removed)";
msg.Body = "Test Message";
msg.Subject = "Test";
msg.BodyFormat = MailFormat.Text;
SmtpMail.Send(msg);
}

}

}
 
R

Randel Bjorkquist

Hey Brian,

I have the "System.Web" in my "Solution Explorer". When I created my
project, I created a new "ASP.NET Web Application" project and it looks to
me as if it added it.

The top of my *.aspx.cs file looks like this:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

I am correct in my thinking that I should be good to go? Or am I still
missing something?

Randel


Brian said:
Hello Randel,

You need to make sure you add a reference to "System.Web".

In the "Solution Explorer" window, right-click on the "References" folder
under your project. Choose "Add Reference"
In the window that shows up, scroll down until you see "System.Web".
Click on it and choose "Select". Then click "Okay" at the bottom.

I left out the class struction from the example, here is a full example:

using System;
using System.Web;

namespace sandbox.SendEmailExample
{

class program
{

[STAThred]
static void Main(string[] args)
{

SmtpMail.SmtpServer = "NameOfYourMailServer" //usually
mail.xxxxxx.com

MailMessage msg = new MailMessage();
msg.From = "(e-mail address removed)";
msg.To = "(e-mail address removed)";
msg.Body = "Test Message";
msg.Subject = "Test";
msg.BodyFormat = MailFormat.Text;
SmtpMail.Send(msg);
}

}

}

Hey Brian,

Thanks for the extremely quick reply. I've tried your code, but it
wont compile for me. Some of the errors I'm getting are: "The type or
namespace name 'SmtpMail' could not be found..." and "The type or
namespace name 'MailMessage' could not be found...". I'm using VS
.NET 2003 Professional. Is that the problem? Are these only availible
in the Enterprise version?

I'll keep looking. But again, thanks for the help,

Randel
 
R

Randel Bjorkquist

Thanks Chris,

That fixed my problem. The code example that Brain posted compiled just
peachy.

Thanks again to both of you, I really appreciated it.

Randel Bjorkquist
 
P

Paul BJ

Randel said:
First, I'm extremely new to C# and the Microsoft Visual Studio IDE. I am in
the middle of writing a small web application that communicates to a
database server and sends out email confirmations. I've got the database
connection going okay, but I'm having problems trying to find a good way to
send an email. I have found a couple products online, but buying a tool is
not an option. So I need to figure out how to use what I have.

I'm looking for a small coded example. If anyone has one, that would be
great. If not, can someone point me into a good direction.

Thanks,

Randel Bjorkquist
Have a look into SmtpMail in System.Web.Mail in the help files. This is
what I use to send out mail:
SmtpMail.SmtpServer = "smtp.yourISP.com";
SmtpMail.Send(emailmsg);

There is rather more to it than that but.....

If you have access to Outlook, you can also access that through interop
and use that to send mail. See
http://www.microeye.com/resources/res_tech_vsnet.htm
 

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