Introduction
This article will help you to create a simple web service for sending a newsletter to subscribers in ASP.NET.
Step 1
Open your Visual Studio then select Add New Project then choose C# then add a webservice (WebService1.asmx) and add a default.aspx page.
Step 2
Open your Webservice1.asmx page and use the following namespace:
Then add the following web method into your webservice1.asmx page:
- [WebMethod]
- public string sendEmail(string senderFrom, string subject, string body)
- {
- string connetionString = null;
- SqlConnection connection;
- SqlDataAdapter adapter = new SqlDataAdapter();
- DataSet ds = new DataSet();
-
- connetionString = "Data Source=servername;Initial Catalog=dbname;uid=xxx;pwd=xxx";
-
- connection = new SqlConnection(connetionString);
-
- connection.Open();
-
- adapter.SelectCommand = new SqlCommand("SELECT emailId from subscribers", connection);
-
- adapter.Fill(ds);
- connection.Close();
-
-
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- string ddds = ds.Tables[0].Rows[1]["emailId"].ToString();
- MailMessage Msg = new MailMessage();
- Msg.From = new MailAddress(senderFrom);
- Msg.To.Add(ddds);
- Msg.Subject = subject;
- Msg.Body = body;
- Msg.IsBodyHtml = true;
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "smtp.gmail.com";
- System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
- NetworkCred.UserName = "your email id here";
- NetworkCred.Password = "your password here";
- smtp.UseDefaultCredentials = true;
- smtp.Credentials = NetworkCred;
- smtp.Port = 587;
- smtp.EnableSsl = true;
- smtp.Send(Msg);
- }
- return "Newsletter sent to every one";
- }
Step 3
Now we need to create a web reference for our web service (webService1.asmx) page.
In Visual Studio 2010 go to references then right-click on it then Add Service Reference then click Advanced and click Add web reference then type your URL, for example:
http://localhost:61834/Webservice1.asmx
then click go and change your web reference name (example: NewsLetter) to whatever you want and click Add Reference.
Now our web service is ready to use.
Step 4
Open your default .aspx page and call your web service page (WebService1.asmx).
- namespace SendNewsLetter
- {
- public partial class default : System.Web.UI.Page
- {
- WebService1 obj = new WebService1();
- string senderFrom, subject, body;
Now we need to send our information to the web service.
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- protected void sendEmail_Click(object sender, EventArgs e)
- {
- senderFrom = "sender address (example: [email protected]";
- subject = "your subject here";
- body = "your body content here";
- string send = obj.sendEmail(senderFrom, subject, body);
- ltlResult.Visible = true;
- ltlResult.Text = sc.ToString();
- }
This is a simple way to send a newsletter to subscribers using a web service that uses ASP.NET.
Full Example
WebService1.asmx:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using AdventureSports.Controls;
- using System.Data;
- using System.Net.Mail;
-
- namespace SendNewsLetter
- {
-
-
-
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [System.ComponentModel.ToolboxItem(false)]
-
-
- public class WebService1 : System.Web.Services.WebService
- {
- [WebMethod]
- public string sendEmail(string senderFrom, string subject, string body)
- {
- string connetionString = null;
- SqlConnection connection;
- SqlDataAdapter adapter = new SqlDataAdapter();
- DataSet ds = new DataSet();
-
- connetionString = "Data Source=servername;Initial Catalog=dbname;uid=xxx;pwd=xxx";
-
- connection = new SqlConnection(connetionString);
-
- connection.Open();
-
- adapter.SelectCommand = new SqlCommand("SELECT emailId from subscribers", connection);
-
- adapter.Fill(ds);
- connection.Close();
-
-
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- string ddds = ds.Tables[0].Rows[1]["emailId"].ToString();
- MailMessage Msg = new MailMessage();
- Msg.From = new MailAddress(senderFrom);
- Msg.To.Add(ddds);
- Msg.Subject = subject;
- Msg.Body = body;
- Msg.IsBodyHtml = true;
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "smtp.gmail.com";
- System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
- NetworkCred.UserName = "your email id here";
- NetworkCred.Password = "your password here";
- smtp.UseDefaultCredentials = true;
- smtp.Credentials = NetworkCred;
- smtp.Port = 587;
- smtp.EnableSsl = true;
- smtp.Send(Msg);
- }
- return "Newsletter sent to every one";
- }
- }
- }
default.aspx:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="SendNewsLetter.default"%>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="btnSendEmail" runat="server" OnClick="sendEmail_Click" Text="Send Email to Subscribers"/>
-
- <asp:Literal ID="ltlResult" runat="server" Visible="false"></asp:Literal>
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.IO;
-
- namespace SendNewsLetter
- {
- public partial class default : System.Web.UI.Page
- {
- WebService1 obj = new WebService1();
- string senderFrom, subject, body;
-
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- protected void sendEmail_Click(object sender, EventArgs e)
- {
- senderFrom = "sender address (example: [email protected]";
- subject = "your subject here";
- body = "your body content here";
- string send = obj.sendEmail(senderFrom, subject, body);
- ltlResult.Visible = true;
- ltlResult.Text = sc.ToString();
- }
-
- }
- }