第二次呼叫时 SMTP 邮件不发送
以下是调用 sendmail
方法的代码。问题是只有第一次调用 sendmail
才会将邮件发送给收件人。第二次调用 sendmail
时,它完美执行,但从未发送任何邮件。如果我将应用程序置于调试模式,然后逐步执行,两封邮件都会被发送。看起来程序的执行速度太快了,出了问题。因此,我在发送函数下面保留了延迟,所以它开始对我来说工作正常,但我不认为这是一个完美的解决方案。任何人都知道这里发生了什么。
if (!String.IsNullOrEmpty(SendMailAdmin))
{
SendMail(SendMailFrom, SendMailAdmin, Subject, AdminMessageText + "<br>" + MessageText);
}
if (!String.IsNullOrEmpty(SendMailOwner))
{
SendMail(SendMailFrom, SendMailOwner, Subject, OwnerMessageText + "<br>" + MessageText);
}
public void SendMail(String MessageFrom, String MessageTo, String MessageSubject, String MessageBody)
{
MailMessage Message = new MailMessage();
Message.Priority = MailPriority.High;
Message.From = new MailAddress(MessageFrom);
Message.To.Add(MessageTo);
Message.Subject = MessageSubject;
Message.IsBodyHtml = true;
Message.Body = MessageBody;
try
{
SmtpClient client = new SmtpClient(SMTPServer, Convert.ToInt32(SMTPPort));
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("{myusername}", "{mypassword}");
client.Send(Message);
System.Threading.Thread.Sleep(3000);
}
catch
{
throw;
}
}
Here is the code which calls the sendmail
method. The problem is that only the first call of sendmail
sends the mail to the receiver. The second time when sendmail
is called , it gets executed perfectly but never delivers any mail. If I put the application in debug mode and then execute it step by step both the mails get delivered. It seemed like the execution speed of the program is so fast that something goes wrong. Therefore I kept a delay below send function, so it started working fine for me, But I don't think it is a perfect solution. Anybody has any clue what is going on here.
if (!String.IsNullOrEmpty(SendMailAdmin))
{
SendMail(SendMailFrom, SendMailAdmin, Subject, AdminMessageText + "<br>" + MessageText);
}
if (!String.IsNullOrEmpty(SendMailOwner))
{
SendMail(SendMailFrom, SendMailOwner, Subject, OwnerMessageText + "<br>" + MessageText);
}
public void SendMail(String MessageFrom, String MessageTo, String MessageSubject, String MessageBody)
{
MailMessage Message = new MailMessage();
Message.Priority = MailPriority.High;
Message.From = new MailAddress(MessageFrom);
Message.To.Add(MessageTo);
Message.Subject = MessageSubject;
Message.IsBodyHtml = true;
Message.Body = MessageBody;
try
{
SmtpClient client = new SmtpClient(SMTPServer, Convert.ToInt32(SMTPPort));
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("{myusername}", "{mypassword}");
client.Send(Message);
System.Threading.Thread.Sleep(3000);
}
catch
{
throw;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我个人认为此时唯一对您有用的是延迟代码。@Shadow 是对的,这就是服务器的配置方式
I personally think the only thing that'll work for you at this point is the delay code .@Shadow is right , this is how servers are configured