1
        MailMessage mail = new MailMessage();

    mail.To.Add("makovetskiyd@yahoo.co.uk");

    mail.From = new MailAddress("makovetskiyd@yahoo.co.uk");

    mail.Subject = "Test Email";

    string Body = "Welcome to CodeDigest.Com!!";

    mail.Body = Body;

    SmtpClient smtp = new SmtpClient();

    smtp.Host = ConfigurationManager.AppSettings["SMTP"];//i get nuller point exception here.. what does the class configuration manager do?

    smtp.Send(mail);
Dmitry Makovetskiyd
  • 6,942
  • 32
  • 100
  • 160

2 Answers2

1

You can use the built-in classes from Microsoft (System.Net.Mail). For example, here is a quick and easy method for sending email:

public static void SendEmail(string messageText, string subjectText,
        string fromAddress, string toAddress, string ccAddress,
        string bccAddress, string hostName, string attachments,
        string userName, string password)
    {
        try
        {
        string[] toAddressList = toAddress.Split(';');
        string[] ccAddressList = ccAddress.Split(';');
        string[] bccAddressList = bccAddress.Split(';');
        string[] attachmentList = attachments.Split(';');

        MailMessage mail = new MailMessage();

        //Loads the To address field
        foreach (string address in toAddressList)
        {
            if (address.Length > 0)
            {
                mail.To.Add(address);
            }
        }

            //Loads the CC address field
            foreach (string address in ccAddressList)
            {
                if (address.Length > 0)
                {
                    mail.CC.Add(address);
                }
            }

            //Loads the BCC address field
            foreach (string address in bccAddressList)
            {
                if (address.Length > 0)
                {
                    mail.Bcc.Add(address);
                }
            }

            //Loads the attachment list
            foreach (string attachment in attachmentList)
            {
                if (attachment.Length > 0)
                {
                    mail.Attachments.Add(new Attachment(attachment));
                }
            }

            mail.From = new MailAddress(fromAddress);
            mail.Subject = subjectText;
            string Body = messageText;
            mail.Body = Body;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = hostName;
            smtp.Credentials = new System.Net.NetworkCredential(userName,password);
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Send(mail);

            mail.Dispose();
            smtp.Dispose();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

In this example, you can send email through Gmail.

IAmTimCorey
  • 16,412
  • 5
  • 39
  • 75
  • @Dmitry Makovetskiyd - Right now, this will work just with Gmail. However, you could add more options so that you could change the port number, etc. from a config file. However, the rest of it is fairly flexible. – IAmTimCorey May 10 '11 at 05:35
0

You can use the System.Net.Mail classes.

Here's a sample of how to do it using gmail: Sending email in .NET through Gmail

Community
  • 1
  • 1
lomaxx
  • 113,627
  • 57
  • 144
  • 179