Search This Blog

Monday, November 15, 2010

How to send a mail message in C#

Here you can find a piece of code to send mail using c#.
Please not that you need to know your Smtp Host server name to be able to sent out any mail.
In addtion I added a couple of lines to get the current user name.
You can map the current user name to an email address so that you can send the mail out from the current user mail address

ex: mailAddress = abeti then use mrblue.abeti@companyname.com as mail address

using System.Net.Mail;
using System.Security.Principal;

        private void button1_Click(object sender, EventArgs e)

        {
            string mailAddress;
            mailAddress = WindowsIdentity.GetCurrent().Name;
            Console.Write(mailAddress);
            MailMessage message = new MailMessage();
            message.From = new MailAddress("mrblue.abeti@companyname.com");
            message.To.Add(new MailAddress("mrred.pini@companyname.com"));
            message.CC.Add(new MailAddress("mryellow.ciliegi@companyname.com"));
            message.Subject = "Message From .Net";
            message.Body = "This is the content";
            string pdfPath = "C:\\test\\";
            string pdfFileName = "prova.pdf";
            Attachment pdfFile = new Attachment(pdfPath + pdfFileName);
            SmtpClient client = new SmtpClient();
            client.Host="MAILSERVER";
            //client.Send(message);
        }

No comments:

Post a Comment