Friday, January 4, 2008

C#: Send email using SmtpClient

using System.Net.Mail;

//mailFrom > "KSeeSharp " - shows up as "KSeeSharp" as sender
//mailTo > Comma delimited email address(es)
//mailServer > Something like smtp.xyz.com

public bool SendEmail(string mailFrom, string mailTo, string subject, string message, string mailServer)
{
bool returnCode = false;

try
{
MailMessage msg = new MailMessage(mailFrom, mailTo, subject, message);
SmtpClient emailClient = new SmtpClient(mailServer);
emailClient.UseDefaultCredentials = true;
emailClient.Send(msg);

returnCode = true;
}
catch (Exception ex)
{
throw ex
}

return returnCode;
}

5 comments:

  1. Works only with framework 2.0 and above...

    ReplyDelete
  2. HI

    I want to setup contact info in my website using smtp client. First I want to test using localhost. Please let me know how to proceed further. How to setup email account for my localhost.

    This is my email address: sarmatvs@yahoo.com.Please respond. Thanks in advance

    ReplyDelete
  3. HI KC
    I have problem sending emails using SMTPclient.I have a application that sends out bulk emails.Problem is I get error after around 20 mails saying regarding Server Response.
    Can u help on this
    Lens

    ReplyDelete
  4. afcourse if they shrapness of a kknife were not to have seen it, thus obvoiusly would have made a plunge right through moistly soft flesh of the barefeet carressed once ago by teh fragrantic softness of petal flowers; would it they have not been?...

    ReplyDelete

Please use your common sense before making a comment, and I truly appreciate your constructive criticisms.