SMTP sendasync works in Windows 7 but fails in XP
I'm struggling with an error message I'm receiving when sending individual eamils from our in-house C# application via smtp. It works fine in Windows 7 but we receive the following error message on our XP machines. We are using our Google mail account and Google's smtp.gmail.com mail server to deliver. The firewalls are turned off and McAfee is disabled on the failing machines. the error message back to the client application is:
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开发者_如何学C)
at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)
--- End of inner exception stack trace ---
Our C# code is multithreaded so we can updated the user interface with the results of the mail send. I have a mailer class containing these two methods to setup and send the message.
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();
}
}
These three following methods are in the 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();
}
in the form I declare the following for the updating delegate
public delegate void UpdateTextCallback(string text);
public UpdateTextCallback UpdateDelegate;
I have tried using just an IP address for the smtp server, tried disabling ssl in the message, making sure all firewalls are disabled, all virus software is disabled. I installed smtp for IIS on the local XP machine but since I'm not using it as the server, that didn't make sense nor did it resolve the issue.
As I indicated, the code works in Win7 but not XP. Any thoughts?
Our solution was just to upgrade the Windows XP machines to Windows 7. There must be an OS level change in the way smtp messages are sent between these two versions of Windows in regards to permissions. It works flawlessly in Win7.
精彩评论