0

I have a sql server table in which im inserting Mail ID, subject,And Body of the mail. And also i displayed the mail details in a gridview in another page. And there is checkbox for each row. Here I want to send these mails to corresponding Email IDs when user checked corresponding checkbox. The actuall problem is that i want to create a web service to send mail. Please help me to create a web service. I tried in some ways. My last Code for to create web service is given below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Mail;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MailService : System.Web.Services.WebService
{
    public MailService()
    {
        //InitializeComponent(); 
    }
    [WebMethod]
    public bool SendMail(string toAddress, string subject, string body)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("proc_MailData", con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.Fill(ds);
        try
        {
            MailMessage msg = new MailMessage();
            msg.From = "john@averla.in";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {                
                string subjct = ds.Tables[0].Rows[i]["MSubject"].ToString();
                string mail = ds.Tables[0].Rows[i]["MailID"].ToString();
                string bdy = ds.Tables[0].Rows[i]["Body"].ToString();
                msg.To = mail;
                msg.Body = bdy;
                msg.Subject = subjct;
            }
            SmtpMail.SmtpServer = "maildemo.averla.in";
            SmtpMail.Send(msg);
            return true;
        }
        catch (Exception exp)
        {
            return false;
        }
    }
}
Dimt
  • 2,278
  • 2
  • 20
  • 26
Averla Team
  • 407
  • 1
  • 6
  • 16
  • 1
    if you are trying to send emails to multiple users you need to call SmtpMail.Send inside loop. – Mairaj Ahmad Aug 27 '14 at 11:27
  • @MairajAhmad SmtpMail i used and called, but not worked.Please refer it – Averla Team Aug 27 '14 at 11:29
  • What error are you getting ? – Mairaj Ahmad Aug 27 '14 at 11:31
  • i'm getting error like this "It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS." – Averla Team Aug 27 '14 at 11:35
  • have a look at this http://stackoverflow.com/questions/20107593/asp-net-it-is-an-error-to-use-a-section-registered-as-allowdefinition-machine – Mairaj Ahmad Aug 27 '14 at 11:38
  • @MairajAhmad can you please help me by editing code..please – Averla Team Aug 27 '14 at 11:43
  • 2
    This error has nothing to do with code. – Mairaj Ahmad Aug 27 '14 at 12:08
  • @MairajAhmad error removed but not worked the web services. i think some error in this web service page code..please can you make it working.?? – Averla Team Aug 27 '14 at 12:30
  • OK what is error now ? – Mairaj Ahmad Aug 27 '14 at 12:33
  • @MairajAhmad no error now.. while adding web service refference to my application nothing occur – Averla Team Aug 27 '14 at 12:36
  • use smtp client class instead and specify the credentials and you should put the code inside the loop, hope it will help you – Monah Aug 27 '14 at 12:36
  • @HadiHassan how.? can you please edit my code and please upload it ... – Averla Team Aug 27 '14 at 12:40
  • to add the web service reference, right click on the project==> add ==> service reference then paste the url in the address text box then click Go, then the service should appear in the services list, then double click the service, then the operations should be shown, name the namespace, then click ok, then you should be able to access it. – Monah Aug 27 '14 at 12:42

1 Answers1

1

1- add using statement

using System.Net.Mail;

2- to execute the smtp client

SmtpClient client = new SmtpClient();
client.Credentials=new NetworkCredential(username,password);
client.Send(message);

3- after you compile your web service and publish it, you should be able to navigate to the url and it will show you the operations like the image navigating the url of the web service

then in your application, you can add the reference like i stated in the comments before

here some links that might help you

SmtpClient class

How to: Add a Reference to a Web Service

hope it will help you

regards

Monah
  • 6,714
  • 6
  • 22
  • 52