Wednesday, 15 January 2014

Hurray!! Completed 1 Lakh Visitors within 2 years only :)

It is with such a joy that I am writing this post to inform all of the visitors that this blog has exceeded 1 Lakhs visitors.







There are many visitors who are not just visitors, but regular contributors. Thanks to each and every one who contribute regularly to this blog. Also, there are many who regularly comment and bring a smile to my face. 

 However, life as we all know is never a bed of roses right?, there are occasional comments from visitors who have written nasty things as well. It has many times brought tears in my eyes, which definitely is not acceptable.

The past few weeks I haven’t been adding any new posts, or replying to any comments. This was just because of severe time constraints. This is the first weekend, I am sitting with my laptop and writing any posts. There will be several new posts starting this week.


Once again, a million thanks to all the contributors, and to all those who visit and comment, and also to those who visit and don’t comment.

Thank you all for the support and encouragement :lol:


Completed 1 Lakh Visitors  with in 2 year is something like a dream to me. When I joined blogger I had no idea about it and never thought of posting this much posts. blogger also gave me many good friends.

Happy Programming!!
If you have any query mail me to Sujeet.bhujbal@gmail.com     
Regards
Sujeet Bhujbal
------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------

Wednesday, 20 November 2013

Send Email using WCF Service in C#.net



Hi Friends,

In this article I will tell you how to send email using WCF Service. We can send email directly from the client; however, in some cases you can't send external emails directly. In that case we need WCF service for sending email from client.
In this article, I will first explain how to create WCF service then I will try to implement email functionality using WCF Service. In this article I am using Gmail to send email., that why user must have gmail account for sending email.

1.    Create WCF Service Class Library

Create a new Project WCF Class Library Project and Name it EmailServices. 





Step 1: Create service contract as IEmailService and add OperationContract SendEmail for sending email. This method uses gmailuseraddress ,password and email to and cc subject as parameter


[ServiceContract]
    public interface IEmailService
    {
        [OperationContract]
        string SendEmail(string gmailUserAddress, string gmailUserPassword, string[] emailTo,string[] ccTo, string subject, string body, bool isBodyHtml);
             
    }

Step 2: Add EmailService class and Implement IEmailService. 


public class EmailService : IEmailService
    {
        private static string SMTPSERVER = "smtp.gmail.com";
        private static int PORTNO = 587;

        public string SendEmail(string gmailUserName, string gmailUserPassword, string[] emailToAddress, string[] ccemailTo, string subject, string body, bool isBodyHtml)
        {           
            if (gmailUserName == null || gmailUserName.Trim().Length == 0)
            {
                return "User Name Empty";
            }
            if (gmailUserPassword == null || gmailUserPassword.Trim().Length == 0)
            {
                return "Email Password Empty";
            }
            if (emailToAddress == null || emailToAddress.Length == 0)
            {
                return "Email To Address Empty";
            }

            List<string> tempFiles = new List<string>();

            SmtpClient smtpClient = new SmtpClient(SMTPSERVER, PORTNO);
            smtpClient.EnableSsl = true;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential(gmailUserName, gmailUserPassword);
            using (MailMessage message = new MailMessage())
           
            {
                message.From = new MailAddress(gmailUserName);
                message.Subject = subject == null ? "" : subject;
                message.Body = body == null ? "" : body;
                message.IsBodyHtml = isBodyHtml;

                foreach (string email in emailToAddress)
                {                  
                    message.To.Add(email);
                }
                if (ccemailTo != null && ccemailTo.Length > 0)
                {
                    foreach (string emailCc in ccemailTo)
                    {                      
                        message.CC.Add(emailCc);
                    }
                }
                try
                {
                    smtpClient.Send(message);                
                    return "Email Send SuccessFully";
                }
                catch
                {                 
                    return "Email Send failed";
                }
            }



        }
    }


Step 3: Build your Class library.


2.    Add WCF Service Application Project

 
Step 1: Add reference of Classlibrary Project to that WCFServiceHost project.
Step 2: Delete IService1.Cs and codebehind file
Step3 : Right click on that SaleService.svc and select view Markup:


Step 4: Change the Service Name from that markup:
<%@ ServiceHost Language="C#" Debug="true" Service="EmailServices.EmailService" CodeBehind="EmailService.svc.cs" %>

Step 5: And build the solution and WCFServiceHost set as startup project and EmailService.svc set as startup page and run the service


 



3. Testing Email Service

1.       Use WCFSTORM for testing WCF Service . You can download WCFSTORM from http://www.wcfstorm.com/wcf/home.aspx
2.       Open WCF Strom add following endpoint http://localhost:53818/EmailService.svc?wsdl



By using this, we have successfully send email using WCF Service









Please find attached source code Download Here




Happy Programming!!

If you have any query mail me to Sujeet.bhujbal@gmail.com     


Regards

Sujeet Bhujbal

------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------