Click here to Skip to main content
Licence 
First Posted 13 Mar 2007
Views 18,746
Downloads 338
Bookmarked 16 times

Sending mail from C# 2.0

By | 13 Mar 2007 | Article
See how to send mail from C#

Introduction

Recently I involved in .NET project, where last developer that worked in project has used a non native class to send mail. Well, I have problems with used solution. The used component doesn't work well on windows 2k3. This problem was solved using SMTP object form .NET 2.0. It's very simple to use.

Using the code

There are one basic class myMail. This class has a method to setting SMTP and sends mail.

static class myMail
    {
        public static string sendMail(string to, string cc, string bcc, string subj, string msg)
        {
            SmtpClient smtp = new SmtpClient();
            MailMessage message = new MailMessage();
            string[] myCC;
            string [] myBCC;
           
            myCC = cc.Split (';');
            myBCC = bcc.Split (';');
            try
            {
                foreach (string ccCopy in myCC)
                {
                    message.CC.Add(new MailAddress(ccCopy));
                }
                foreach (string bccHide in myBCC)
                {
                    message.Bcc.Add(new MailAddress(bccHide));
                }
                message.To.Add(new MailAddress(to));
                message.Subject = subj;
                message.Body = msg;
                smtp.Host = "teste";  // hostname smtpserver
                smtp.Port = 25;       // smtp server port
                message.From = new MailAddress("<a href="mailto:paulo.passos@xxxxxxxxxx.com.br">paulo.passos@xxxxxxxxxx.com.br</a>");
                smtp.Send(message);
            }
            catch (SmtpException E)
            {
                return "Mail send failed with message: " + E.Message;
            }
            return "Mail was send";
        }
    }
    // sending mail from project
    //
    private void btnSend_Click(object sender, EventArgs e)
    {
        MessageBox.Show (myMail.sendMail(txtTO.Text , txtCC.Text , txtBCC.Text , txtSubject.Text , rtMsg.Text));
    }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ppassos

Web Developer

Brazil Brazil

Member

Paulo Passos is a student at PUC Minas São Gabriel, Minas Gerais, Brazil. He has developing since 1989 using Clipper, Pascal, Delphi, Basic, VB, C#, eVC++, eVB and Java. Actually he's a developer manager at idevelopers - TI Solutions at Belo Horizonte/MG and teaching at Utramig.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSend from an other smtp like outlook do. Pinmemberelfraga7:34 24 Apr '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 13 Mar 2007
Article Copyright 2007 by ppassos
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid