SMTP sendasync 在 Win7 下可以工作,但在 XP 下不行

鉴客 发布于 2011/08/04 09:46
阅读 600
收藏 0
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();
        }

    }

错误信息:

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 ---

期间我尝试不使用SSL、关闭防火墙、关闭病毒软件,都没效果。

 

加载中
返回顶部
顶部