如何发送未发送的邮件
在vs2010,asp.net C#上工作。最近我在smtp服务器上工作。使用smtp服务器我需要发送邮件,我已经成功地完成了这项工作。要发送邮件,我使用以下语法。
public string PostEmail(string mailSubject, string mailBody)
{
string deliveryStatusCode = "Ok";
if (!string.IsNullOrEmpty(mailSubject) && !string.IsNullOrEmpty(gConfig.EmailTo))
{
MailMessage msg = new MailMessage();
msg.To.Add(gConfig.EmailTo);
msg.From = new MailAddress(gConfig.EmailTo);
msg.Subject = mailSubject;
msg.Body = mailBody;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(gConfig.SmtpHost, gConfig.SmtpPort);
smtp.Credentials = new System.Net.NetworkCredential(gConfig.SmtpCredentials, gConfig.SmtpPassword);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
smtp.Send(msg);
}
catch (SmtpException smptpEx)
{
deliveryStatusCode = smptpEx.Message;
}
}
else
{
deliveryStatusCode = "Invalid or null value entry.";
}
return deliveryStatusCode;
}
现在我面临一个问题,由于某种原因有些邮件无法发送,那些未发送的邮件需要发送,如何发送那些未发送的邮件,smtp中有没有任何机制来执行这项工作或者有什么方法可以解决这个问题.提前致谢。如果有任何疑问,请询问。
Work on vs2010 ,asp.net C#.Recently I work on smtp server.Using the smtp server I need to send mail, I already successfully done this job . To send mail I use the bellow syntax.
public string PostEmail(string mailSubject, string mailBody)
{
string deliveryStatusCode = "Ok";
if (!string.IsNullOrEmpty(mailSubject) && !string.IsNullOrEmpty(gConfig.EmailTo))
{
MailMessage msg = new MailMessage();
msg.To.Add(gConfig.EmailTo);
msg.From = new MailAddress(gConfig.EmailTo);
msg.Subject = mailSubject;
msg.Body = mailBody;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(gConfig.SmtpHost, gConfig.SmtpPort);
smtp.Credentials = new System.Net.NetworkCredential(gConfig.SmtpCredentials, gConfig.SmtpPassword);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
smtp.Send(msg);
}
catch (SmtpException smptpEx)
{
deliveryStatusCode = smptpEx.Message;
}
}
else
{
deliveryStatusCode = "Invalid or null value entry.";
}
return deliveryStatusCode;
}
.Now I face one problem,for some reason some mail cannot be sent ,those unsent mail need to be sent ,How to send those unsent mail,Is there any mechanism in smtp to perform this job or there is some way to solve this issue.Thanks in advance .If have any query plz ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用拾取目录,如果 smpt 服务器关闭,则不会发送邮件。当它上线时,它将读取目录中的文件并开始发送电子邮件。
You can use a pickup directory, if the smpt server is down the mail is not send. When it comes online, it will read the files in the directory and starts sending the emails.
您的 .NET 代码只需获取一条消息,生成电子邮件,然后将其发送到 SMTP 服务器。由 SMTP 服务器重新发送未发送的邮件。
您可以执行 @peer 所说的操作,只需将生成的文件放入 SMTP 拾取目录中,而不是发送 SMTP 消息,但这需要您的 .NET 代码能够访问 SMTP 拾取目录。
另一种选择可能是存储电子邮件对象(在内存中或在磁盘上序列化),如果
send
抛出错误,请稍后重试。然而,最终,您确实需要首先找出电子邮件未发送的原因。这通常是一个配置问题,可以通过一些调查来缓解。查看网络连接、SMTP 服务器正常运行时间,以及最重要的……身份验证。
正如我在评论中所说,生成的电子邮件在最终到达最终用户收件箱之前经过了很多跳。如果没有更多关于错误的细节,我们只能扔飞镖并希望其中一个落地。
祝你好运
追赶
Your .NET code simply takes a message, generates and email, and sends it off to the SMTP server. It's up to the SMTP server to resend any messages that are unsent.
You can do what @peer said, and instead of sending an SMTP message, simply drop a generated file in the SMTP pickup directory, however this requires your .NET code to have access to the SMTP pickup directory.
Another option might be to store the email object (either in memory, or serialized on disk) and if the
send
throws an error, retry later.Ultimately however, you really need to find out why the email isn't going through in the first place. It's often a configuration issue that can be mitigated with a little investigation. Look into network connectivity, SMTP Server Uptime, and most importantly.. authentication.
As I said in my comment, the generated email makes a great many hops before finally landing in the end users inbox. Without a little more details as to what is erroring, we can only throw darts and hope that one of them lands.
Good luck
Chase