1

Hello I try to create auto emails send by service file but there is some problem.

this is BrithdayEmailService.cs file.

namespace BirthdayEmailSevice
{
partial class BirthdayEmailService : ServiceBase
{
    private Timer scheduleTimer = null;
    private DateTime lastRun;
    private bool flag;
    public BirthdayEmailService()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("EmailSource"))
        {
            System.Diagnostics.EventLog.CreateEventSource("EmailSource", "EmailLog");
        }
        eventLogEmail.Source = "EmailSource";
        eventLogEmail.Log = "EmailLog";

        scheduleTimer = new Timer();
        scheduleTimer.Interval = 1 * 5 * 60 * 1000;
        scheduleTimer.Elapsed += new ElapsedEventHandler(scheduleTimer_Elapsed);
    }
    protected override void OnStart(string[] args)
    {
        flag = true;
        lastRun = DateTime.Now;
        scheduleTimer.Start();
        eventLogEmail.WriteEntry("Started");
    }
    protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (flag == true)
        {
            ServiceEmailMethod();
            lastRun = DateTime.Now;
            flag = false;
        }
        else if (flag == false)
        {
            if (lastRun.Date < DateTime.Now.Date)
            {
                ServiceEmailMethod();
            }
        }
    }
 private void ServiceEmailMethod()
    {
        eventLogEmail.WriteEntry("In Sending Email Method");

        EmailComponent.GetEmailIdsFromDB getEmails = new EmailComponent.GetEmailIdsFromDB();
        getEmails.connectionString = ConfigurationManager.ConnectionStrings["CustomerDBConnectionString"].ConnectionString;
        getEmails.storedProcName = "GetBirthdayBuddiesEmails";
        System.Data.DataSet ds = getEmails.GetEmailIds();
        EmailComponent.Email email = new EmailComponent.Email();
        email.fromEmail = "example@gmail.com";
        email.fromName = "example Name";
        email.subject = "Birthday Wishes";
        email.smtpServer = "smtp.gmail.com";
        email.smtpCredentials = new System.Net.NetworkCredential("example1@gmail.com", "example1 password");
        foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
        {
            email.messageBody = "<h4>Hello " + dr["CustomerName"].ToString() + "</h4><br/><h3>We Wish you a very Happy" +
                                "Birthday  to You!!! Have a bash...</h3><br/><h4>Thank you.</h4>";

            bool result = email.SendEmailAsync(dr["CustomerEmail"].ToString(), dr["CustomerName"].ToString());

            if (result == true)
            {
                eventLogEmail.WriteEntry("Message Sent SUCCESS to - " + dr["CustomerEmail"].ToString() +
                                         " - " + dr["CustomerName"].ToString());
            }
            else
            {
                eventLogEmail.WriteEntry("Message Sent FAILED to - " + dr["CustomerEmail"].ToString() +
                                         " - " + dr["CustomerName"].ToString());
            }

        }
    }
}

On scheduleTimer = new Timer(); got error

(This member is defined more than one)

Error:

Ambiguity between 'BirthdayEmailService.BirthdayEmailService.scheduleTimer' and 'BirthdayEmailService.BirthdayEmailService.scheduleTimer'

Also got error on BirthdayEmailService.Designer.cs this is file of design tool of service error is :

  private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.eventLogEmail = new System.Diagnostics.EventLog();
        **this.scheduleTimer = new System.Windows.Forms.Timer(this.components);**
        ((System.ComponentModel.ISupportInitialize)(this.eventLogEmail)).BeginInit();
        // 
        // BirthdayEmailService
        // 
        this.ServiceName = "BirthdayEmailService";
        ((System.ComponentModel.ISupportInitialize)(this.eventLogEmail)).EndInit();

    } 

those part is bold there is getting error as same as.

(This member is defined more than one)

Error:

Ambiguity between 'BirthdayEmailService.BirthdayEmailService.scheduleTimer' and 'BirthdayEmailService.BirthdayEmailService.scheduleTimer'

1 Answers1

1

It could be because the name of the class and the namespace are the same one. See this post.

Community
  • 1
  • 1
Ignacio
  • 806
  • 1
  • 10
  • 29
  • I using this using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Timers; using System.Configuration; using EmailComponent; – Niket Patil Jul 16 '15 at 10:31
  • What I was trying to say is that you namespace is **BirthdayEmailSevice** and the name of the class is also **BirthdayEmailSevice**. The ambiguite could be because of this reason, so try to change one of the names. – Ignacio Jul 16 '15 at 10:35
  • But my namespace and class name is different this is class name BirthdayEmailService and namespace is BirthdayEmailSevice. – Niket Patil Jul 16 '15 at 11:16