我正在努力处理我通过smtp从我们的内部C#应用程序发送个别eamils时收到的错误消息。它在Windows 7中工作正常但我们在XP机器上收到以下错误消息。我们正在使用我们的Google邮件帐户和Google的smtp.gmail.com邮件服务器来提供服务。防火墙已关闭,并且在发生故障的计算机上禁用了McAfee。返回客户端应用程序的错误消息是:
Error occurred when sending mail to xxxxxx@xxxxxxx.com System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. ---> System.Net.Sockets.SocketException: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)
at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result)
at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)
--- End of inner exception stack trace ---
我们的C#代码是多线程的,因此我们可以使用邮件发送的结果更新用户界面。我有一个包含这两种方法的邮件程序类来设置和发送邮件。
public void Send()
{
m_frmAccess.Invoke(m_frmAccess.UpdateDelegate,
new Object[] { "" });
SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("xxxxxxxxx@xxxxxxx.com", "xxxxxxxxx"),
EnableSsl = true
};
try
{
StringBuilder sb = new StringBuilder();
sb.Append("Message Body");
LinkedResource logo = new LinkedResource("DA-NewBrand-Stack-100px-for.jpg");
logo.ContentId = "companylogo";
htmlView.LinkedResources.Add(logo);
m_Errormessage = "";
MailAddress to = new MailAddress(m_To);
MailAddress from = new MailAddress(m_From, "Company");
MailAddress cc = new MailAddress("xxxx@xxxxxxx.com", "Name");
MailMessage message = new MailMessage(from, to);
message.CC.Add(cc);
message.AlternateViews.Add(htmlView);
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = m_Subject;
client.Timeout = 10000;
message.SubjectEncoding = System.Text.Encoding.UTF8;
object userState = message;
client.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);
client.SendAsync(message, userState);
}
catch (Exception ex)
{
m_Errormessage = "";
m_Errormessage = ex.Message + Environment.NewLine + ex.StackTrace.ToString() + Environment.NewLine + ex.InnerException + Environment.NewLine;
m_frmAccess.Invoke(m_frmAccess.UpdateDelegate,
new Object[] { m_Errormessage });
}
}
public void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)
{
//Get the Original MailMessage object
m_result = "";
MailMessage mail = (MailMessage)e.UserState;
//write out the subject
string subject = mail.Subject;
try
{
if (e.Cancelled)
{
m_result = "Send canceled for message to " + mail.To.ToString();
}
if (e.Error != null)
{
m_result = "Error occurred when sending mail to " + mail.To.ToString() + " " + e.Error.ToString();
}
else
{
m_result = "Message to " + mail.To.ToString() + " was sent.";
}
m_frmAccess.Invoke(m_frmAccess.UpdateDelegate,
new Object[] { m_result });
}
catch (Exception ex)
{
m_result = ex.Message + ex.InnerException.ToString();
}
}
以下三种方法都在winform中。
public void UpdateText(string message)
{
txtMessage.Text = message;
}
private void ThreadMethod()
{
clsSmtpEmailer smtpMailer = new clsSmtpEmailer(this);
CollectDataToEmail(smtpMailer);
smtpMailer.Send();
}
private void btnSendEmail_Click(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(ThreadMethod));
myThread.Start();
}
在表单中我为更新委托声明了以下内容
public delegate void UpdateTextCallback(string text);
public UpdateTextCallback UpdateDelegate;
我曾尝试仅为smtp服务器使用IP地址,尝试在消息中禁用ssl,确保所有防火墙都被禁用,所有病毒软件都被禁用。我在本地XP机器上为IIS安装了smtp,但由于我没有将它用作服务器,因此没有意义,也没有解决问题。
正如我所说,代码适用于Win7但不适用于XP。有什么想法吗?
答案 0 :(得分:0)
我们的解决方案只是将Windows XP计算机升级到Windows 7.对于权限,必须在这两个版本的Windows之间发送smtp消息的方式发生操作系统级别更改。它在Win7中完美运行。