web service code for email sending not working

srinivaskr

Newcomer
Joined
Sep 27, 2009
Messages
7
C#:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Calling the function SendMail
        Response.Write(SendMail("abc@gmail.com", "def@gmail.com", "ghi@yahoo.com", "Test Mail", "Test Mail Body"));
    }

    public string SendMail(string toList, string from, string ccList, string subject, string body)
    {
        MailMessage message = new MailMessage();
        SmtpClient smtpClient = new SmtpClient();
        string msg = string.Empty;
        try
        {
            MailAddress fromAddress = new MailAddress(from);
            message.From = fromAddress;
            message.To.Add(toList);
            if (ccList != null && ccList != string.Empty)
                message.CC.Add(ccList);
            message.Subject = subject;
            message.IsBodyHtml = true;
            message.Body = body;
            smtpClient.Host = "mail.server.com";
            smtpClient.Port = 25;
            smtpClient.UseDefaultCredentials = true;
            smtpClient.Credentials = new System.Net.NetworkCredential("info@server.com", "password");

            smtpClient.Send(message);
            msg = "Successful";
        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }
        return msg;
    }

}

the above is the web service code written to send a email from flex application. but this code is not working. can anyone help me debug the application.
 
Last edited by a moderator:
When you say it doesn't work can you give a little more detail...

Does it report success but the email isn't sent? Does the exception (if raised) give any useful information?
 
the program gave me a parse error in the directive statement
<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>

the error describes that the service cannot be created.
 
If the code you posted above is for a webservice then why is it inheriting from the Page class?

Have you tried creating a new webservice and just trying to incluide the code required to send the email in it?
 
i have opened a new project as file-> new-> website-> asp.net web service and in the window written the code for the web service and while i was compiling got this error. also when i tried to change the name of the page directive it compiled successfully but am not able to publish the same to the localhost uddi registery.
 
If it is a web service it shouldn't be inheriting from the page class, try creating a new webservice and putting your code inside the webservice derived class rather than replacing the existing code with code from a page.
 
i have successfully debugged the program but how do i now register it to the uddi registery. i want to use the same web service in another web site which is already running on the web.
 
Back
Top