我制作了一个小程序,基本上可以通过 yahoo smtp 服务器发送电子邮件。我的代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress("myid@yahoo.com");
message.To.Add("anotherid@yahoo.com");
message.Subject = "afdasdfasfg";
message.Body = "Hgfk4564267862738I";
message.IsBodyHtml = true;
message.Priority = MailPriority.High;
SmtpClient sC = new SmtpClient("smtp.mail.yahoo.com");
sC.Port = 587;
sC.Credentials = new NetworkCredential("myid", "mypassword");
//sC.EnableSsl = true;
sC.Send(message);
MessageBox .Show ("Mail Send Successfully");
}
catch (Exception ex)
{
MessageBox .Show (ex + "Mail Sending Fail's") ;
}
}
}
}
奇怪的是它在第一周就奏效了。我可以毫无问题地发送消息。然后就在昨天,程序开始卡住并且没有响应(我没有更改代码)。为什么会这样?我怎样才能修复我的程序?
编辑:@Andreas Niedermair 现在我刚刚尝试了该程序并将其放置了整整一分钟然后显示错误:检测到 ContextSwitchDeadlock 消息:CLR 在 60 秒内无法从 COM 上下文 0x21eb78 转换到 COM 上下文 0x21ece8。拥有目标上下文/单元的线程很可能正在执行非泵等待或处理一个非常长时间运行的操作而不发送 Windows 消息。这种情况通常会对性能产生负面影响,甚至可能导致应用程序变得无响应或内存使用量随着时间的推移不断累积。为避免此问题,所有单线程单元 (STA) 线程都应使用泵送等待原语(例如 CoWaitForMultipleHandles)并在长时间运行的操作期间定期泵送消息。
感谢您的帮助!
最佳答案
您的渔获量
有没有达到?
我假设,您没有足够的耐心来达到 Timeout
属性的默认值(100 秒)...
您可以降低该值以获得更早的完成。
只要您不使用异步模式,您的 UI 线程就会被阻塞。另一种方法是使用 SendAsync
方法(具体方法的 msdn 条目中有示例实现)。
编辑:
正如作者提到的一个可能的 fu**ed 端口:是的,它可能是。但你必须阅读 specification paper ,它告诉我们:
- SMTP 服务器:plus.smtp.mail.yahoo.com
- 使用 SSL
- 端口:465
- 使用身份验证
- 帐户名/登录名:您的 Yahoo!邮件 ID(您的电子邮件地址不带“@yahoo.com”,例如“testing80”)
- 电子邮件地址:您的 Yahoo!邮件地址(例如,testing80@yahoo.com)
- 密码:您的 Yahoo!邮箱密码
- [...] 在通过 Yahoo! 的 SMTP 服务器发送电子邮件时,尝试将 SMTP 端口号设置为 587。
但即使你符合规范:你真的应该选择异步模式:)
编辑: 提到的异常与 COM 有关......有点谷歌搜索,我发现 this :
What's probably happening is that you have a COM object in a form, and you're doing work on the UI thread. If your UI gets blocked by the processing for >60 seconds, the COM component can complain.
编辑:
否则:您是否更改了 visual studio 的异常对话框中的任何内容?那么这可能是你的 solution , 或 this one (有一些基本的解释)...
关于c# - 为什么发送电子邮件程序卡住?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4269738/