Hi darshanbihani,
You can attach file from disk folder to the email by passing the Byte Array from Windows Service to Web Service method and add the Byte Array as attachment to the MailMessage object.
You can refer below steps for calling a Web Service from Windows Service after creating a Web Service project and send the email with attachment.
1. Open your Windows Service project in Visual Studio.
2. Right click on the project in the Solution Explorer.
3. Select Add then, click on Service Reference.
4. Enter the URL of your web service and click Go.
5. Click on OK.
6. Now you can create an instance of the Service class and call the Web Methods from Windows Service.
Refer below example.
Web Service
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Web.Services;
using System.Net.Configuration;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string SendEmail(byte[] attachment)
{
// Get email settings from Database.
DataSet ds = this.GetData();
if (ds.Tables[0].Rows.Count > 0)
{
string from = ds.Tables[0].Rows[0]["FromEmail"].ToString().Trim();
using (MailMessage mm = new MailMessage(from, from))
{
mm.Subject = "Dc Sale Report";
mm.Body = "Dc Sale Report Excel Attachment";
//Add Byte array as Attachment.
mm.Attachments.Add(new Attachment(new MemoryStream(attachment), "DCSale_Report.xlsx"));
mm.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = ds.Tables[0].Rows[0]["EmailHost"].ToString().Trim();
smtp.EnableSsl = true;
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = ds.Tables[0].Rows[0]["EmailUsername"].ToString().Trim();
credentials.Password = ds.Tables[0].Rows[0]["EmailPassword"].ToString().Trim();
smtp.UseDefaultCredentials = false;
smtp.Credentials = credentials;
smtp.Port = int.Parse(ds.Tables[0].Rows[0]["EmailPort"].ToString());
smtp.Send(mm);
}
}
}
return "Method executed successfully";
}
private DataSet GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Sales";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds, "Sales");
return ds;
}
}
}
}
}
Calling the Web Service from Windows Service
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
HelloService.WebService service = new HelloService.WebService();
byte[] bytes = System.IO.File.ReadAllBytes(@"D:\\Files\\DCSale_Report.xlsx");
// Calling Web Method with attachment from Disk path.
service.SendEmail(bytes);
}
protected override void OnStop()
{
}
}
For installing and running the Windows Service, please refer the article Windows Service Hello World example in C# and VB.Net.
Screenshot
The Recieved Email