[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


I need the C# code to send an email

Posted on 2007-09-29
Medium Priority
Last Modified: 2013-12-17
I need the C# code to send an email that has an HTML body and a single attachment.  I don't think I need the complexity of making it multi-threaded, but do need as much efficiency as you can muster since I will be sending many emails.

I plan on running this process from a web service so I can run it from my website.

Any sample C# code would be much appreciated.  I use Visual Studio 2005.

Question by:newbieweb
  • 3
  • 2
LVL 16

Accepted Solution

TSmooth earned 2000 total points
ID: 19985718
This article should help you get started. It's written in VB and for ASP.NET but since you are going to run this as a web service all the code should pretty much be the same and if you have any problems converting it to C# just let me know. Basically, everything you want is in the System.Net.Mail namespace.

Author Comment

ID: 19985724
Thanks, but converting emailing code from VB to C# has too many potential pitfalls, considering I don't konw VB and am weak on C# communications.  I'll wait to see if someone's got a C# version that's tested.

LVL 16

Expert Comment

ID: 19985737
public void SendMail()
            MailMessage mm = new MailMessage(); // create a new mail message
            mm.To.Add("toaddress@blah.com"); // Add an address to the "to" address list
            mm.From.Address = "fromaddress@blah.com"); // Set the from address
            mm.IsBodyHtml = true; // We want this to be an HTML email
            mm.Body = "<b>Bold</b><br /><br /><p>Test Message</p>"; // Set the body of the mail
            mm.Attachments.Add(new Attachment("c:\\myattachment.zip")); // Attach a file
            mm.Subject = "My Subject"; // Set the subject
            // Create a new smtp client instance, specifying our smtp server host
            SmtpClient smtp = new SmtpClient("myhost.smtp.com");  
            smtp.Send(mm); // Send the message


Author Comment

ID: 19985761
Is this:

 mm.Body = "<b>Bold</b><br /><br /><p>Test Message</p>"; // Set the body of the mail

simply the HTML as a string?  How do I convert an HTML file to a string?

Author Comment

ID: 19985781

I just discovered I had another project going with some pretty nice emailing code.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question