rss
twitter
  •  

ColdFusion & .NET: Send an Email

| Posted in .NET, ColdFusion |

2

Today's post in the ColdFusion & .NET series covers a fairly simple task: sending an email.  This is very easy to do in both languages.  The examples below show you how to send a text based email in each language.

In ColdFusion you use the CFMAIL tag to send off an email.  In the example below I have included the server attributes as part of the tag.  If you do not supply those when using the tag, ColdFusion will use the information that is setup in the ColdFusion Administrator to connect to the SMTP server.

 ColdFusion |  copy code |? 
1
<cfmail to="sometoaddress@email.com" from="Some Name <someaddress@email.com>" subject="Your Test Email">
2
    Hello test email.
3
    </cfmail>

Now the code in C#:

 C# |  copy code |? 
1
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
2
    message.To.Add("sometoaddress@email.com");
3
    message.Subject = "Your Test Email";
4
    message.From = new System.Net.Mail.MailAddress("somefromaddress@email.com");
5
    message.Body = e.ToString();
6
    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mail.someserver.com");
7
    smtp.Credentials = new NetworkCredential("username", "password");
8
    smtp.Send(message);

In C# you must also include the following library:

 C# |  copy code |? 
1
using System.Net.Mail;

http://www.thedevshack.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/dzone_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/blogmarks_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/newsvine_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/magnolia_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/google_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/sphinn_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/mixx_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/twitter_48.png http://www.thedevshack.com/wp-content/plugins/sociofluid/images/meneame_48.png

Comments (2)

Will that send HTML email? How easy is it in .NET to add an attachment?

James,
I just posted a new entry that answers both of your questions. It shows you how to send the email with HTML and also with an attachment.
http://blog.magnetion.com/2009/02/net-sending-a-html-email-with-an-attachment.html
Mike

Post a comment