asp.net, c#, WCF

Webboy2008
Webboy2008 used Ask the Experts™
on
I have the following codes copied from App_Code folder.
How can I convert into WCF? I would like to create WCF, and make the EmailGenerator
sharing for all the websites  that I have.

Thanks

/// <summary>
/// Summary description for EmailGenerator
/// </summary>
public class EmailGenerator
{
    private string mFromEmail;
    private string mToEmail;
    private string mSubject;
    private string mBody;
    private string mFooter;
    public string FromEmail
    {
        get { return mFromEmail; }
        set { mFromEmail = value; }
    }
    public string ToEmail
    {
        get { return mToEmail; }
        set { mToEmail = value; }
    }
    public string Subject
    {
        get { return mSubject; }
        set { mSubject = value; }
    }
    public string Body
    {
        get { return mBody; }
        set { mBody = value; }
    }
    public string Footer
    {
        get { return mFooter; }
        set { mFooter = value; }
    }
    public string SendeMail()
    {
        try
        {
            MailMessage MessageInfo = new MailMessage(mFromEmail, mToEmail, mSubject, mBody);
            MessageInfo.IsBodyHtml = true;
            SmtpClient EmailClient = new SmtpClient("mail.xxx.com");
            System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(xxx.com", "xxxx");
            EmailClient.UseDefaultCredentials = false;
            EmailClient.Credentials = SMTPUserInfo;
            EmailClient.Send(MessageInfo);
            return "Sent";
        }
        catch
        {
            return "Unable to send email";
        }

    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Rose BabuSenior Team Manager

Commented:
Hi,

create a WCF Service Application in VS2010 and i named the service as Service1.

create OperationContract with the Function Name in IService1.cs as below
//IService1.cs
using System.ServiceModel;
[ServiceContract]
public interface IService1
{
	[ServiceContract]
	public interface IService1
	{
		[OperationContract]
		string SendeMail(string mFromEmail, string mToEmail, string mSubject, string mBody, string mFooter);
	} 
}

Open in new window

create mail functionality in Service1.svc.cs as below.
//Service1.svc.cs
using System.Net.Mail;
using System;

public class Service1 : IService1
{        
	public string SendeMail(string mFromEmail, string mToEmail, string mSubject, string mBody, string mFooter)
	{
		string returnMSG = "";
		try
		{
			MailMessage MessageInfo = new MailMessage(mFromEmail, mToEmail, mSubject, mBody);
			MessageInfo.IsBodyHtml = true;
			SmtpClient EmailClient = new SmtpClient("mail.xxx.com");
			System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("xxx.com", "xxxx");
			EmailClient.UseDefaultCredentials = false;
			EmailClient.Credentials = SMTPUserInfo;
			EmailClient.Send(MessageInfo);
			returnMSG = "Sent";
		}
		catch(Exception e)
		{
			returnMSG = "Unable to send email. ";
		}

		return returnMSG;
	}
}

Open in new window

Update the credentials and test the service.

Try this.

Author

Commented:
srosebabu? How to call WCF Services on aspx? And how to  test it?
Senior Team Manager
Commented:
Here is a sample link to consume a eff service

 http://www.codeproject.com/Articles/42643/Creating-and-Consuming-Your-First-WCF-Service

have a look on this and try to call your service. I give simple code later to the method created in your service...
--


RoseBabu
Rose BabuSenior Team Manager

Commented:
Hi,

1. Host your WCF service in IIS.
2. Get the service URL as http://localhost:8080/Service1.svc
3. Add this as a web reference
4. Import namespace or Using namespace. in my case it is EE_Email_WCF is a reference name i gave.
5. Then create an object for the service
6. Consume the service method by using the service object with the input values
7. Parse the response.

Here is a sample code for you.
Imports WebApplication.EE_Email_WCF

Public Sub WCFTEST()

	Dim wcf_ws As New Service1

	Dim isSendEmail = wcf_ws.SendeMail("from@email.com", "to@email.com", "Email Subject", "EmailBody", "Email Footer")

	' ReturnMsg from the SendeMail function
	' Sent Or Unable to send email. 
	If isSendEmail = "Sent" Then
		' Email Sent Successfully

	Else
		' Email Sent Failed

	End If

End Sub

Open in new window

Try this.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial