Net.Mail下的方法发送邮件遇到一个非常奇怪的问题
以下是代码,我把下面这写代码放在一个winform的按钮事件中,当点击这个按钮后必须要把这个窗体关掉邮件才能发送成功,要不然就一直发送不出去,我一开始考虑是不是要释放什么资源,但是我把MailMessage的对象Dispose掉也不行,我甚至把SmtpClient和MailMessage的对象置空也不行。还有这个不管怎么说还发送出去了,但是我把这个类库编译成DLL放在web的程序中去调用这个DLL就算把web程序关掉了,邮件也发送不出去,不知道是怎么回事情。SmtpClient sc = new SmtpClient("smtp.163.com.cn");
sc.UseDefaultCredentials = true;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
MailAddress from = new MailAddress("koouser@sina.com", "jack", System.Text.Encoding.UTF8);
MailAddress to = new MailAddress("koo@163.com");
MailMessage mm = new MailMessage(from, to);
mm.Body = "我是正文";
mm.BodyEncoding = System.Text.Encoding.UTF8;
mm.Subject = "我是主题";
mm.SubjectEncoding = System.Text.Encoding.UTF8;
sc.Send(mm); --------------------编程问答-------------------- 顶一下 --------------------编程问答-------------------- 帮你顶顶出来高手 --------------------编程问答-------------------- 我也想知道为什么? --------------------编程问答-------------------- 前几天也碰到此问题,发了一个贴子,不过没有得到合理的解决办法,还好最终自己解决了这个问题.手工显式中断SMTP连接即可(SmtpClient的关闭连接方法藏得太深了).
SmtpClient sc = new SmtpClient("smtp.163.com.cn");
//必须设置MaxIdleTime!!
sc.ServicePoint.MaxIdleTime = 1;
sc.UseDefaultCredentials = true;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
MailAddress from = new MailAddress("koouser@sina.com", "jack", System.Text.Encoding.UTF8);
MailAddress to = new MailAddress("koo@163.com");
MailMessage mm = new MailMessage(from, to);
mm.Body = "我是正文";
mm.BodyEncoding = System.Text.Encoding.UTF8;
mm.Subject = "我是主题";
mm.SubjectEncoding = System.Text.Encoding.UTF8;
sc.Send(mm);
//显式关闭,服务器才会真正关闭网络连接,然后才发送信件.
sc.ServicePoint.CloseConnectionGroup(sc.ServicePoint.ConnectionName);
不管怎么样,按照上面方法,这个问题算是解决了.
但是千万不要认为这样就万无一失了,因为有可能在关闭连接后,导致CPU占用率非常高,而且持久不降(我也不知道是什么原因,这个问题已经发到微软新闻组,还没得到解答). --------------------编程问答-------------------- http://blog.csdn.net/hy_lihuan/archive/2007/12/25/1967206.aspx
不知道你有没有用 --------------------编程问答-------------------- 用了4楼的方法邮件是可以发送出去了,但CPU是占用太高了,都到80%左右了,我这个发送邮件还有可能是经常使用的功能,所以不能太消耗系统资源的。 --------------------编程问答-------------------- 还有一个问题,以上代码在WEB中根本就发送不出去,是怎么回事情呀? --------------------编程问答--------------------
using System.Net.Mail;
using System.Configuration;
/// <summary>
/// 发送EMAIL
/// </summary>
/// <param name="sRecipientEmail">收件人地址</param>
/// <param name="sSubject">主题</param>
/// <param name="sMessage">内容</param>
/// <returns>发送是否成功</returns>
public static bool SendMail(string sRecipientEmail, string sSubject, string sMessage)
{
//邮件对象
MailMessage emailMessage;
//smtp客户端对象
SmtpClient client;
// 初始化邮件对象
String sSenderEmail = ConfigurationManager.AppSettings["EmailAddress"];
emailMessage = new MailMessage(sSenderEmail, sRecipientEmail, sSubject, sMessage);
emailMessage.IsBodyHtml = true;
emailMessage.SubjectEncoding = System.Text.Encoding.Default;
emailMessage.BodyEncoding = System.Text.Encoding.Default;
//加入
emailMessage.Headers.Add("X-Priority", "3");
emailMessage.Headers.Add("X-MSMail-Priority", "Normal");
emailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
emailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");
//邮件发送客户端
client = new SmtpClient();
//邮件服务器及帐户信息
client.Host = ConfigurationManager.AppSettings["EmailServer"];
//client.Host = "smtp.163.com";
//client.Port = 465;
//client.EnableSsl = true;
System.Net.NetworkCredential Credential = new System.Net.NetworkCredential();
//你的邮箱的用户名 密码(从web.config读取)
Credential.UserName = ConfigurationManager.AppSettings["EmailUserName"];
Credential.Password = ConfigurationManager.AppSettings["EmailPassword"];
//TODO: 需要更新该密码到web.config
client.Credentials = Credential;
try
{
client.Send(emailMessage);
}
catch
{
return false;
}
return true;
}
补充:.NET技术 , ASP.NET