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;
}
}
}
