How do I send an email? | | |
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 | | | | re: How do I send an email?
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 = "you@yourhost.com";
msg.To = "someone@somehost.com";
msg.Body = "Test Message";
msg.Subject = "Test";
msg.BodyFormat = MailFormat.Text;
SmtpMail.Send(msg);
}
[color=blue]
> 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
>[/color] | | | | re: How do I send an email?
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
"Brian" <fake@email.com> wrote in message
news:3558632472490298750000@news.microsoft.com...[color=blue]
> 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 = "you@yourhost.com";
> msg.To = "someone@somehost.com";
> msg.Body = "Test Message";
> msg.Subject = "Test";
> msg.BodyFormat = MailFormat.Text;
>
> SmtpMail.Send(msg);
>
> }
>
>[color=green]
>> 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
>>[/color]
>
>
>[/color] | | | | re: How do I send an email?
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 = "you@yourhost.com";
msg.To = "someone@somehost.com";
msg.Body = "Test Message";
msg.Subject = "Test";
msg.BodyFormat = MailFormat.Text;
SmtpMail.Send(msg);
}
}
}
[color=blue]
> 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
>
> "Brian" <fake@email.com> wrote in message
> news:3558632472490298750000@news.microsoft.com...
>[color=green]
>> 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 = "you@yourhost.com";
>> msg.To = "someone@somehost.com";
>> msg.Body = "Test Message";
>> msg.Subject = "Test";
>> msg.BodyFormat = MailFormat.Text;
>> SmtpMail.Send(msg);
>>
>> }
>>[color=darkred]
>>> 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
>>>[/color][/color][/color] | | | | re: How do I send an email?
Randel Bjorkquist wrote:[color=blue]
> wont compile for me. Some of the errors I'm getting are: "The type
> or namespace name 'SmtpMail' could not be found..."[/color]
It's a good example, but the poster left off the essential namespace. Add
"using System.Web.Mail" to your code.
By the way, you can find the docs for it here: http://msdn.microsoft.com/library/en...temwebmail.asp
--
Chris Priede (priede@panix.com) | | | | re: How do I send an email?
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" <fake@email.com> wrote in message
news:3579632472502429531250@news.microsoft.com...[color=blue]
> 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 = "you@yourhost.com";
> msg.To = "someone@somehost.com";
> msg.Body = "Test Message";
> msg.Subject = "Test";
> msg.BodyFormat = MailFormat.Text;
> SmtpMail.Send(msg);
> }
>
> }
>
> }
>
>[color=green]
>> 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
>>
>> "Brian" <fake@email.com> wrote in message
>> news:3558632472490298750000@news.microsoft.com...
>>[color=darkred]
>>> 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 = "you@yourhost.com";
>>> msg.To = "someone@somehost.com";
>>> msg.Body = "Test Message";
>>> msg.Subject = "Test";
>>> msg.BodyFormat = MailFormat.Text;
>>> SmtpMail.Send(msg);
>>>
>>> }
>>>
>>>> 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
>>>>[/color][/color]
>
>
>[/color] | | | | re: How do I send an email?
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
"Chris Priede" <priede@panix.com> wrote in message
news:eZE$aDJMFHA.3644@TK2MSFTNGP10.phx.gbl...[color=blue]
> Randel Bjorkquist wrote:[color=green]
>> wont compile for me. Some of the errors I'm getting are: "The type
>> or namespace name 'SmtpMail' could not be found..."[/color]
>
> It's a good example, but the poster left off the essential namespace. Add
> "using System.Web.Mail" to your code.
>
> By the way, you can find the docs for it here:
>
> http://msdn.microsoft.com/library/en...temwebmail.asp
>
>
> --
> Chris Priede (priede@panix.com)
>[/color] | | | | re: How do I send an email?
Randel Bjorkquist wrote:
[color=blue]
>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
>
>
>
>[/color]
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 |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,387 network members.
|