Sending Newsletter to Subscribers Using ASP.Net Web Service

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:
  1. using System.Net.Mail;  //if any error go to references right click on it and add reference, choose .net tab and add System.Net   
Then add the following web method into your webservice1.asmx page:
  1. [WebMethod]    
  2. public string sendEmail(string senderFrom, string subject, string body)    
  3. {    
  4.         string connetionString = null;      
  5.         SqlConnection connection;      
  6.         SqlDataAdapter adapter = new SqlDataAdapter();      
  7.         DataSet ds = new DataSet();      
  8.              
  9.         connetionString = "Data Source=servername;Initial Catalog=dbname;uid=xxx;pwd=xxx";      
  10.       
  11.         connection = new SqlConnection(connetionString);      
  12.       
  13.         connection.Open();      
  14.       
  15.         adapter.SelectCommand = new SqlCommand("SELECT emailId  from subscribers", connection);//your sql command here      
  16.       
  17.         adapter.Fill(ds);      
  18.         connection.Close();      
  19.       
  20.       
  21.         for (int i = 0; i < ds.Tables[0].Rows.Count; i++)      
  22.         {      
  23.             string ddds = ds.Tables[0].Rows[1]["emailId"].ToString();      
  24.             MailMessage Msg = new MailMessage();      
  25.             Msg.From = new MailAddress(senderFrom);      
  26.             Msg.To.Add(ddds);      
  27.             Msg.Subject = subject;      
  28.             Msg.Body = body;      
  29.             Msg.IsBodyHtml = true;      
  30.             SmtpClient smtp = new SmtpClient();      
  31.             smtp.Host = "smtp.gmail.com";      
  32.             System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();      
  33.             NetworkCred.UserName = "your email id here";      
  34.             NetworkCred.Password = "your password here";      
  35.             smtp.UseDefaultCredentials = true;      
  36.             smtp.Credentials = NetworkCred;      
  37.             smtp.Port = 587;      
  38.             smtp.EnableSsl = true;      
  39.             smtp.Send(Msg);      
  40.         }      
  41.         return "Newsletter sent to every one";        
  42.      }    
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).
 
  1. namespace SendNewsLetter  
  2. {  
  3.     public partial class default : System.Web.UI.Page  
  4.     {  
  5.         WebService1 obj = new WebService1();  
  6.         string senderFrom, subject, body;  
Now we need to send our information to the web service.
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2.         {  
  3.               
  4.         }  
  5.         protected void sendEmail_Click(object sender, EventArgs e)    
  6.         {    
  7.             senderFrom = "sender address (example: [email protected]";    
  8.             subject = "your subject here";    
  9.             body = "your body content here";    
  10.             string send = obj.sendEmail(senderFrom, subject, body);    
  11.             ltlResult.Visible = true;    
  12.             ltlResult.Text = sc.ToString();    
  13.         }      
This is a simple way to send a newsletter to subscribers using a web service that uses ASP.NET.
 
Full Example
 
WebService1.asmx:
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.Services;    
  6. using AdventureSports.Controls;    
  7. using System.Data;    
  8. using System.Net.Mail;    
  9.     
  10. namespace SendNewsLetter    
  11. {    
  12.     /// <summary>    
  13.     /// Summary description for WebService1    
  14.     /// </summary>    
  15.     [WebService(Namespace = "http://tempuri.org/")]    
  16.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    
  17.     [System.ComponentModel.ToolboxItem(false)]    
  18.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.     
  19.     // [System.Web.Script.Services.ScriptService]    
  20.     public class WebService1 : System.Web.Services.WebService    
  21.     {    
  22. [WebMethod]    
  23. public string sendEmail(string senderFrom, string subject, string body)    
  24. {    
  25.         string connetionString = null;      
  26.         SqlConnection connection;      
  27.         SqlDataAdapter adapter = new SqlDataAdapter();      
  28.         DataSet ds = new DataSet();      
  29.              
  30.         connetionString = "Data Source=servername;Initial Catalog=dbname;uid=xxx;pwd=xxx";      
  31.       
  32.         connection = new SqlConnection(connetionString);      
  33.       
  34.         connection.Open();      
  35.       
  36.         adapter.SelectCommand = new SqlCommand("SELECT emailId  from subscribers", connection);//your sql command here      
  37.       
  38.         adapter.Fill(ds);      
  39.         connection.Close();      
  40.       
  41.       
  42.         for (int i = 0; i < ds.Tables[0].Rows.Count; i++)      
  43.         {      
  44.             string ddds = ds.Tables[0].Rows[1]["emailId"].ToString();      
  45.             MailMessage Msg = new MailMessage();      
  46.             Msg.From = new MailAddress(senderFrom);      
  47.             Msg.To.Add(ddds);      
  48.             Msg.Subject = subject;      
  49.             Msg.Body = body;      
  50.             Msg.IsBodyHtml = true;      
  51.             SmtpClient smtp = new SmtpClient();      
  52.             smtp.Host = "smtp.gmail.com";      
  53.             System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();      
  54.             NetworkCred.UserName = "your email id here";      
  55.             NetworkCred.Password = "your password here";      
  56.             smtp.UseDefaultCredentials = true;      
  57.             smtp.Credentials = NetworkCred;      
  58.             smtp.Port = 587;      
  59.             smtp.EnableSsl = true;      
  60.             smtp.Send(Msg);      
  61.         }      
  62.         return "Newsletter sent to every one";        
  63.      }    
  64.   }    
  65. }    
 default.aspx:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="SendNewsLetter.default"%>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.     <asp:Button ID="btnSendEmail" runat="server" OnClick="sendEmail_Click" Text="Send Email to Subscribers"/>    
  13.     
  14. <asp:Literal ID="ltlResult" runat="server" Visible="false"></asp:Literal>    
  15.     </div>  
  16.     </form>  
  17. </body>  
  18. </html>  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;  
  8.   
  9. namespace SendNewsLetter  
  10. {  
  11.     public partial class default : System.Web.UI.Page  
  12.     {  
  13.         WebService1 obj = new WebService1();  
  14.         string senderFrom, subject, body;  
  15.   
  16.         protected void Page_Load(object sender, EventArgs e)  
  17.         {  
  18.               
  19.         }  
  20.         protected void sendEmail_Click(object sender, EventArgs e)    
  21.         {    
  22.             senderFrom = "sender address (example: [email protected]";    
  23.             subject = "your subject here";    
  24.             body = "your body content here";    
  25.             string send = obj.sendEmail(senderFrom, subject, body);    
  26.             ltlResult.Visible = true;    
  27.             ltlResult.Text = sc.ToString();    
  28.         }      
  29.          
  30.     }  
  31. }   

Up Next
    Ebook Download
    View all
    Learn
    View all