从.net发送电子邮件有问题

本文关键字:电子邮件 有问题 net | 更新日期: 2023-09-27 18:05:57

我正在用c#, MVC 4, . net 4重写一个Access程序。从我的访问程序,我可以发送300+电子邮件没有问题。下面是从循环中调用的相关代码:

Set objMessage = CreateObject("CDO.Message")
objMessage.From = FromEmail
If DebugMode Then
    objMessage.To = DebugEmail
Else
    objMessage.To = EmailStr
End If
If Not IsNull(BCCEmail) Then
    objMessage.bcc = BCCEmail
End If
objMessage.subject = subject
objMessage.HTMLbody = Message
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServerAddress
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPPort
objMessage.Configuration.Fields.Update
objMessage.Send

在我的MVC应用程序我有各种各样的问题。有时没有收到电子邮件。有时会收到300封邮件中的200封。有时只收到大约20封电子邮件。它所有的情况下,电子邮件发送(根据代码),但从来没有收到所有的电子邮件回到我的电子邮件客户端。以下是我的MVC 4程序的代码:

       using (DatabaseContext dbContext = new DatabaseContext())
        {
            IList<Customer> customers = dbContext.Customers.Where(c => c.sendEmail == true).OrderBy(c => c.boxNumber).ToList();
            Setting setting = dbContext.Settings.First();
            SmtpClient smptClient = new SmtpClient();
            //smptClient.Timeout = 1000 * 200;  // setting the timeout to 200 seconds.
            DateTime batchTimeStamp = DateTime.Now;
            Customer customer;
            for (int i = 0; i < customers.Count; i++ )
            {
                customer = (Customer)customers[i];
                MailMessage emailMessage = new MailMessage();
                setEmailRecipients(customer, ref emailMessage);
                if (!String.IsNullOrEmpty(setting.bccEmail))
                    emailMessage.Bcc.Add(new MailAddress(setting.bccEmail));
                emailMessage.Subject = "Box #" + customer.boxNumber + ".  " + setting.emailSubject;
                emailMessage.Body = setting.emailBody;
                try
                {
                    smptClient.Send(emailMessage);
                }
                catch (SmtpException smtpEx)
                {
                    Debug.WriteLine(smtpEx, "EmailError");
                    Thread.Sleep(1000 * 100);   // wait for 100 seconds
                    i--; // backup the counter so the same email is resent
                    continue;
                }
                customer.sendEmail = false;  // Clear the customer's email sent flag
                EmailHistory history = new EmailHistory();
                history.sentTo = customer.email;
                history.boxNumber = customer.boxNumber;
                history.sentOn = DateTime.Now;
                history.batchTimeStamp = batchTimeStamp;
                dbContext.EmailHistories.Add(history);
                dbContext.SaveChanges();
                //if (i % 5 == 0 && i > 0)
                //    Thread.Sleep(1000 * 20);    // sleep to 20 seconds.
            }
        }
我完全被难住了。有人能告诉我为什么我的c#代码不能工作吗?编辑:我想我找到问题了。我移动了调用来实例化循环中的"using"子句中的SmtpClient对象。因此,SmtpClient被实例化,然后为每个电子邮件销毁。我认为像这样一遍又一遍地实例化SmtpClient是一个禁忌,但它是有效的。有人知道为什么吗?

从.net发送电子邮件有问题

我不确定这是真正的原因,但这里有一个潜在的无限循环:

catch (SmtpException smtpEx)
{
    Debug.WriteLine(smtpEx, "EmailError");
    Thread.Sleep(1000 * 100);   // wait for 100 seconds
    i--; // backup the counter so the same email is resent
    continue;
}

如果有一个异常是由错误的输入引起的(比如错误的电子邮件地址?),并且这些输入不会从一个循环到另一个循环改变,那么这个将无限循环。

是你的网页。配置文件设置是否正确?请参阅ScottGu的博客,以获得一个很好的工作示例。

  <system.net>
    <mailSettings>
      <smtp from="test@foo.com">
        <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

从设计的角度来看,您可能希望将业务逻辑(包括电子邮件和数据库保存)移动到服务中(例如使用Web API),以便它在与网站分开的进程中运行。这种层的分离还将提高代码的可扩展性和可测试性。