Send Email by WebService

13,161
public bool Send(string toAddress, string subject, string body, bool isHtml, List<string> files)
{
    try
    {
        MailMessage mailMsg = new MailMessage();

        mailMsg.To = toAddress;
        mailMsg.Headers.Add("From", string.Format("{0} <{1}>", senderName, senderAddress));
        mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = server;
        mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = port;
        mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

        if (enableAuth)
        {
            mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
            mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = userName;
            mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = password;
        }

        if (enableSsl)
        {
            mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
        }

        if (isHtml)
        {
            mailMsg.BodyFormat = MailFormat.Html;
        }

        mailMsg.BodyEncoding = Encoding.UTF8;
        mailMsg.Subject = subject;
        mailMsg.Body = body;

        for (int i = 0; i < files.Count; i++)
        {
            mailMsg.Attachments.Add(new MailAttachment(files[i]));
        }
        SmtpMail.SmtpServer = server;
        SmtpMail.Send(mailMsg);

        return true;
    }
    catch (Exception ex)
    {
        this.errorMsg = ex.Message;
        return false;
    }
}

Note that you must use System.Web.Mail for this cod to work.

Share:
13,161

Related videos on Youtube

xorpower
Author by

xorpower

Reached 500 Repo on May 22'11 Reached 600 Repo on Jul 29'11 Reached 700 Repo on Aug 10'11 Reached 800 Repo on Sep 09'11 Reached 900 Repo on Oct 13'11 Reached (&amp; crossed) 1000 Repo during Mar 14-19'12 Reached 1300 Repo on May 8 2013

Updated on June 04, 2022

Comments

  • xorpower
    xorpower almost 2 years

    I have developed on Windows Application. Now i need to send an email (attachment feature included) by Web Service. How can i do that?

    Also i need to notify the email before 'n' days. ('n' days is a feature controlled by user)

    Let me know if any comment.

    Thanks.

    • leppie
      leppie over 13 years
      Have you started attempting to do this yourself? What do you have so far?
    • xorpower
      xorpower over 13 years
      I searched for the same but was not so successful :-(
    • Peter van Kekem
      Peter van Kekem over 13 years
      Why do you need a webservice to achieve this? do you already use one?
    • xorpower
      xorpower over 13 years
      No i dont have web service created.Its needed so that SMTP Credentials are not required to passed statically.
  • xorpower
    xorpower over 13 years
    Hey thanks for the code.. But can i know how to do that in Web Service and access the same in WinForm Application
  • Stefan P.
    Stefan P. over 13 years
    Well first of all you have to develop your Web Service and create 2 web methods. A method to upload your files to a tmp folder on the machine that hosts the web service. 2ed you need to call "Send" function with the list of file already uploaded (with the root of the tmp folder from the server).
  • xorpower
    xorpower over 13 years
    thanks @stefan: Let me try this at my end and will comment if any hurdle..!