程序中有用到SmtpClient发送邮件,但是邮件服务器管理员告诉我,我发的邮件没有退出指令,导致服务器产生了很多警告信息
而且在批量发送邮件时,还经常存在有些人发出去了,有些人没发出去的情况请问如何在Send后,指示邮件服务器退出?我的代码如下:
System.Threading.Thread thread = new System.Threading.Thread(delegate()
{
SmtpClient client = new SmtpClient();
client.Host = mailServerAddr;
client.Port = mailServerPort;
if(!string.IsNullOrEmpty(mailUser))
client.Credentials = new System.Net.NetworkCredential(mailUser, mailPwd);
else
client.UseDefaultCredentials = true;
client.Send(message);// 这里想用SendAsync,但是还要设置所调用的asp.net页面,所以没用
});
thread.Start();
而且在批量发送邮件时,还经常存在有些人发出去了,有些人没发出去的情况请问如何在Send后,指示邮件服务器退出?我的代码如下:
System.Threading.Thread thread = new System.Threading.Thread(delegate()
{
SmtpClient client = new SmtpClient();
client.Host = mailServerAddr;
client.Port = mailServerPort;
if(!string.IsNullOrEmpty(mailUser))
client.Credentials = new System.Net.NetworkCredential(mailUser, mailPwd);
else
client.UseDefaultCredentials = true;
client.Send(message);// 这里想用SendAsync,但是还要设置所调用的asp.net页面,所以没用
});
thread.Start();
//用Net.Mail
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Net;
using System.Net.Mail;public partial class FindMM: System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{ SendMail(); }
} public bool SendMail()
{
MailMessage myMail = new MailMessage();
myMail.From = new MailAddress("[email protected]");
myMail.To.Add(new MailAddress("[email protected]"));
myMail.Subject = "Test";
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
myMail.Body = "You are successed.";
myMail.BodyEncoding = System.Text.Encoding.UTF8;
myMail.IsBodyHtml = false;
myMail.Priority = MailPriority.High;
myMail.CC.Add(new MailAddress("[email protected]")); //抄送
myMail.Bcc.Add(new MailAddress("[email protected]")); //密抄送
//smtp client
SmtpClient sender = new SmtpClient();
sender.Host = "smtp.gmail.com";
sender.Port = 587;
sender.Credentials = new NetworkCredential("用户名", "密码");
sender.DeliveryMethod = SmtpDeliveryMethod.Network;
sender.EnableSsl = true;
try
{
sender.Send(myMail);
return true;
}
catch (Exception e)
{
LblMsg.Text = e.Message.ToString();
return false;
}
}}
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=146711&wa=wsignin1.0
I strongly recommend to NOT use the workaround of setting MaxIdleTime or ConnectionLimit if you're on an IIS7 server. Setting these properties to 1 on IIS7 leads to a very prolonged period of high CPU. My workaround is to wrap Send() in a Try-Catch, and if an exception occurs, I immediately re-try Send() ing. On the second attempt a new connection to the mail server is opened up and the email goes thru. Btw, the exception I often receive is:"Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host."The exception occurs when .NET doesn't properly close its connection to the mail server, and then when attempting to send a second email about 30 seconds later, .NET tries using the same open connection which the mail server finally closed on its end. .NET throws that error when it discovers its connection to the mail server was closed. If .NET would simply issue a QUIT command (per the spec) and disconnect from the mail server, this exception would not occur.
去官方网站下载了个JMail.net,测试发现邮件发出去后,居然带着Dimac的版权信息,汗
去掉版权信息,然后在内部使用吧然后这个帖子散分