Friday, May 6, 2011

Email handling in C#

I need to know the object name for handling emails in C#/.NET Framework.

From stackoverflow
  • System.Net.Mail is the namespace to look in. Start with SmtpClient or MailMessage.

  • You need the namespace System.Net.Mail.

    Here is an example from the ScottGu's blog.

    MailMessage message = new MailMessage();
    message.From = new MailAddress("sender@foo.bar.com"); 
    
    message.To.Add(new MailAddress("recipient1@foo.bar.com"));
    message.To.Add(new MailAddress("recipient2@foo.bar.com"));
    message.To.Add(new MailAddress("recipient3@foo.bar.com")); 
    
    message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
    message.Subject = "This is my subject";
    message.Body = "This is the content";
    
    
    SmtpClient client = new SmtpClient();
    client.Send(message);
    
  • In addition to Ekeko's answer if you would like to use an external mail server you must specify the host in the SmtpClient constructor.

    SmtpClient client = new SmtpClient("mail.yourmailserver.com");
    

    And you might also need authentication to be specified if your server requires it.

    client.Credentials = new NetworkCredential("username", "password");
    
  • These answers are assuming that you are asking about SMTP handling. POP3 is not handled natively in the .NET framework. You will have to purchase a third-party library. I'd recommend the Ostrosoft POP3 library.

  • There is a bunch of questions in stack Overflow that describes how you can send.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.