Pomster
Pomster

Reputation: 15197

How to populate a Database with emails using a Windows Service?

I'm Looking for Code to populate a Microsoft Sql Server Database, with my email inbox and i would like it to be done by a Windows service so the users don't know its happening.

How do i get my windows service to populate my Database?

My Database is called Email_Log, With Tables: Email_ID e_To e_From e_Subject e_Date

My Insert Code will look something like.

INSERT INTO Email_Log
(Email_ID, e_To, e_From, e_Subject, e_Date)
VALUES
(Data, data, data, data, data)

I have used OpenPop before to create webservice to retrieve mails, so i know how to do that, my exp with windows services are ... none and with databases very little.

I'm looking for some example code on how to do this? or some useful links to sights that have done something similar to this. I have looked online and can't find any help in what i'm trying to do. I just need a push start. Following an example is the best way to start.

Upvotes: 1

Views: 1302

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

  • For authoring Windows Services in .NET => here.
  • For accessing a SQL database in .NET => here

So for example here's how a sample INSERT query might look like using plain ADO.NET:

using (var conn = new SqlConnection("your connection string to the database"))
using (var cmd = conn.CreateCommand())
{
    conn.Open();
    cmd.CommandText = 
        @"
        INSERT INTO Email_Log
        (Email_ID, e_To, e_From, e_Subject, e_Date)
        VALUES
        (@id, @to, @from, @subject, @date)
        ";
    cmd.Parameters.AddWithValue("@id", 123);
    cmd.Parameters.AddWithValue("@to", "[email protected]");
    cmd.Parameters.AddWithValue("@from", "[email protected]");
    cmd.Parameters.AddWithValue("@subject", "some subject");
    cmd.Parameters.AddWithValue("@date", DateTime.Now);
    cmd.ExecuteNonQuery();
}

Upvotes: 4

Related Questions