【发布时间】:2013-12-16 09:34:42
【问题描述】:
当我在本地运行应用程序时,这工作正常 - 基本上是在 IIS Express(以前称为开发服务器)上。但是如果我发布到 IIS,它就不起作用。在两台不同的机器上尝试了两种不同的 IIS。我不知道在 IIS 上运行时如何获取任何日志信息。所有关于检查是否有每分钟发送电子邮件的新记录。
全球.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 方法:
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>
【问题讨论】:
-
start>run>eventvwr>application events -
什么是 MailSender 以及您如何发送电子邮件?我们需要查看一些实现细节和配置(如果有的话)?
-
通过 SMTP。 EmailSender 方法内部:MailMessage message = new MailMessage();SmtpClient client = new SmtpClient();client.Send(message);
-
根据我的经验,在本地服务器而不是在生产服务器上发送电子邮件通常是因为生产服务器的 SMTP 服务配置为需要身份验证,您的代码可能需要确保它正确地通过 SMTP 服务器进行身份验证
-
@Jude:除非你告诉我们到底是什么问题(从异常日志中收集),否则这里的人很难提供帮助。
标签: asp.net email iis iis-express aspnet-development-server