HQT

追求.NET 技术永无止境

导航

利用 .NET 发送 Hotmail 邮件(XMLHTTP,但测试无法通过)

Posted on 2005-10-19 17:10  HQT  阅读(512)  评论(1编辑  收藏  举报

网上搜索到的,经测试发现 Hotmail 现在不支持(希望是我理解错了?)Outlook,OutlookExpress 等客户端进行收发信件了。
但还是要抱着对技术学习的态度,我还是把转的东西贴一贴。

核心代码如下:

        public void SendMail(string from, string fromName,string to, string subject, string body,string username,string password)
        
{
            
//    We begin by setting up a quote string (used later) as well generating a mail time stamp: 

            
// Quote character.
            string quote = "\u0022";
            
// Generate the time stamp.
            DateTime now = DateTime.Now;
            
string timeStamp = now.ToString("ddd, dd MMM yyyy hh:mm:ss"); 

            
// The HTTPMail protocol follows an SMTP-like communication scheme (See RFC 821). Outlook express sends out mail in MIME format, but for demonstrations purposes we simply send a plain text e-mail: 

            
// Build the post body.
            string postBody = null;
            
// Dump mail headers.
            postBody += "MAIL FROM:<" + from + ">\r\n";
            postBody 
+= "RCPT TO:<" + to + ">\r\n";
            postBody 
+= "\r\n";
            postBody 
+= "From: " + quote + fromName + quote + " <" + from + ">\r\n";
            postBody 
+= "To: <" + to + ">\r\n";
            postBody 
+= "Subject: " + subject +"\r\n";
            postBody 
+= "Date: " + timeStamp + " -0000\n";
            postBody 
+= "\r\n";
            
// Dump mail body.
            postBody += body; 

            
// To send the mail, we need to set the Content-Type request header to message/rfc821, indicating that this request contains a body which follows RFC 821. We POST the request body generated above to the sendmsg URL obtained during connection time: 

            xmlHttp_ 
= new XMLHTTP();

            
// Open the connection.
            xmlHttp_.open("POST", serverUrl, false, username,password);
            
// Send the request.
            xmlHttp_.setRequestHeader("Content-Type""message/rfc821");
            xmlHttp_.send(postBody); 
            
            MessageBox.Show( xmlHttp_.statusText ); 
//xmlHttp_.responseText;
        }

原代码在最后一行是用 xmlHttp_.responseText 实际上读不出数据,而用 statusText, 可以收到 Hotmail 的响应内容:
---------------------------
Access to Hotmail via Outlook and Outlook Express now requires a subscription. Please sign up at http://upgrade.msn.com
---------------------------

当然还有其他变通的办法让你可以使用 hotmail 发信件的。。。。