-1

This works fine when I run the application locally- basically on IIS Express (formerly known as Development Server). But if I publish to IIS, it does not work. Tried two different IIS on two different machine. I don't know how to get any log information when it runs on IIS. All about checking if there is a new record every minute to send email.

Global.asax:

protected void Application_Start(object sender, EventArgs e)
{
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 60000;       
    timer.AutoReset = true;
    timer.Elapsed += new ElapsedEventHandler(NewCandidates);
    timer.Enabled = true; 
}

public void NewCandidates(object source, System.Timers.ElapsedEventArgs e)
{
    SendEmail.MailSender mail = new MailSender();
    mail.EmailSender();
}

EmailSender method:

public void EmailSender()
    {
        Entities entity = new Entities();
        DateTime moment = DateTime.Now.AddMinutes(-1);

        var ilan = from ilanRow in entity.IsIlan
                   where (ilanRow.Date >= moment) && (ilanRow.Yayinla==true)
                    from iOnay in entity.IlanOnay
                    where iOnay.IlanId == ilanRow.IsIlanId
                    from sirket in entity.KurumFirma
                    where sirket.KurumFirmaId==ilanRow.KurumFirmaId
                    select new { ilanRow, iOnay, sirket };

        List<IlanList> liste = new List<IlanList>();

        foreach (var itemilan in ilan)
        {
            liste.Add(new IlanList
            {
                KurumId     = itemilan.ilanRow.KurumFirmaId,
                IlanId      = itemilan.ilanRow.IsIlanId,
                SirketAdi   = itemilan.sirket.Adi,
                IlanAdi     = itemilan.ilanRow.Adi,
                Il          = itemilan.ilanRow.Il,
                IsAlan      = itemilan.ilanRow.IsAlan,
                Date        = itemilan.ilanRow.Date,
                Sonuc       = itemilan.iOnay.sonuc
            });
        }

        List<IlanOnayList> listeOnay = new List<IlanOnayList>();

        MailMessage message = new MailMessage();
        message.From = new MailAddress("info@company.com");
        message.To.Add(new MailAddress("cuneytkukrer@googlemail.com"));
        message.Subject = "New Ones";
        string body = "<table style=\"border:1px solid gray; padding:5px;\"><th>Şirket Adı</th><th>İlan Adı</th><th>İl</th><th>İş Alanı</th><th>Tarih</th>";
        foreach (var item in liste)
        {
            body = body +
                "<tr><td style=\"border:1px solid gray; padding:5px;\">" + item.SirketAdi +
                "<td style=\"border:1px solid gray; padding:5px;\">" + item.IlanAdi +
                "</td><td style=\"border:1px solid gray; padding:5px;\">" + item.Il +
                "</td><td style=\"border:1px solid gray; padding:5px;\">" + item.IsAlan +
                "</td><td style=\"border:1px solid gray; padding:5px;\">" + item.Tarih +
                "</td></tr>";
        }
        body = body + "</table><br />";
        message.Body = body;
        message.IsBodyHtml = true;
        SmtpClient client = new SmtpClient();
        foreach (var item in liste)
        {
            if (liste.Count > 0 && item.Sonuc != "var")
            {
                client.Send(message);
                var onay = from i in entity.IlanOnay where i.IlanId == item.IlanId select i;
                foreach (var itemonay in onay)
                {
                    itemonay.sonuc = "var";
                    entity.SaveChanges();
                }
            }
        }
    }

Web.Config:

<system.net>
<mailSettings>
  <smtp from="info@company.com">
    <network host="auth.myhosting.com" port="587" userName="info@company.com" password="123456" defaultCredentials="false" />
  </smtp>
</mailSettings>

Jude
  • 2,353
  • 10
  • 46
  • 70
  • `start` > `run` > `eventvwr` > `application events` – Abhitalks Dec 16 '13 at 09:37
  • 1
    What's MailSender and how are you sending emails? We need to see some implementation details and configuration if any? – Leo Dec 16 '13 at 09:38
  • Through SMTP. Inside the EmailSender method: MailMessage message = new MailMessage();SmtpClient client = new SmtpClient();client.Send(message); – Jude Dec 16 '13 at 09:45
  • @Jude There is an Edit button under your question, please use that to update your question when asked to provide code – RobV Dec 16 '13 at 10:36
  • 1
    In my experience email sending on local server and not on production is usually because production servers have their SMTP services configured to require authentication, your code likely needs to ensure that it is properly authenticating with the SMTP server – RobV Dec 16 '13 at 10:37
  • 1
    @Jude: Unless you tell us what exactly is the problem (gleaned from exception log), it would be very difficult for people here to help. – Abhitalks Dec 16 '13 at 10:52
  • @RobV I have changed the port number to 25, still not working. What I am wondering if the NewCandidate event handler is invoked or not. – Jude Dec 16 '13 at 12:57

1 Answers1

1

Apparently, you need to browse the site once, when it is published to IIS. It's working!

Jude
  • 2,353
  • 10
  • 46
  • 70