Cannot send email using HttpWebRequest

Mar 12 2011 1:53 PM

Hi Everyone, 

I have been struggling with an issue of sending an email using HttpWebRequest. I have almost done a complete sweep of internet and I am doing exactly what needs to done for sending an email using httpwebrequest. But the problem is I cannot see the email arriving at the recepient. Could this be a configuration issue with Exchange 2003? or am I doing something wrong? 

The order in which I am doing this is first using PUT to put the email into drafts folder, then doing PROPPATCH and then moving it to ##DAVMailSubmissionURI##. I can see the email in the sent items folder of the sender. 


Code:
        strMailboxURI = strMailboxURI + "/" + strSubject + ".eml"; 

        //Create the email in the folder 
        strBody = "From: " + strFrom + "\r\n" + 
            "To: " + strTo + "\r\n" + 
              "Subject: " + strSubject + "\r\n" + 
              "MIME-Version: 1.0" + "\r\n" + 
              "Content-Type: text/xml;" + "\r\n" + 
              "\r\n" + 
              strText; 
        PUTRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strMailboxURI); 
        PUTRequest.Credentials = MyCredentialCache; 
        PUTRequest.Headers.Set("Translate", "f"); 
        PUTRequest.Method = "PUT"; 
        bytes = Encoding.UTF8.GetBytes((string)strBody); 
        PUTRequest.ContentLength = bytes.Length; 
        PUTRequestStream = PUTRequest.GetRequestStream(); 
        PUTRequestStream.Write(bytes, 0, bytes.Length); 
        PUTRequestStream.Close(); 
        PUTResponse = (System.Net.HttpWebResponse)PUTRequest.GetResponse(); 


        //Do the PROPPATCH 
        //---------------- 
        Guid id = new Guid(); 
        id = Guid.NewGuid(); 
        String DDHeader = "1:2:3:" + id + ":4"; 
        String DDBody = String.Empty; 
        UploadXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + UploadXML; 
        UploadXML = System.Security.SecurityElement.Escape(UploadXML); 
        string strxml = "<?xml version=\"1.0\"?>" + 
                    "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:e=\"http://schemas.microsoft.com/exchange/\">" + 
                    "<d:set>" + 
                    "<d:prop>" + 
                    "<DDHeader xmlns:e=\"\">" + DDHeader + "</DDHeader>" + 
                    "<DDBody xmlns:e=\"\">" + UploadXML + "</DDBody>" + 
                    "<e:outlookmessageclass>" + 
                    "CustomClass</e:outlookmessageclass>" + 
                    "</d:prop>" + 
                    "</d:set>" + 
                    "</d:propertyupdate>"; 

        PROPPATCHRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strMailboxURI); 
        PROPPATCHRequest.Credentials = MyCredentialCache; 
        PROPPATCHRequest.Headers.Set("Translate", "f"); 
        PROPPATCHRequest.ContentType = "text/xml"; 
        PROPPATCHRequest.ContentLength = strxml.Length; 
        PROPPATCHRequest.Method = "PROPPATCH"; 
        byte[] PROPPATCHbytes = Encoding.UTF8.GetBytes(strxml); 
        PROPPATCHRequest.ContentLength = PROPPATCHbytes.Length; 
        PROPPATCHRequestStream = PROPPATCHRequest.GetRequestStream(); 
        PROPPATCHRequestStream.Write(PROPPATCHbytes, 0, PROPPATCHbytes.Length); 
        PROPPATCHRequestStream.Close(); 
        PROPPATCHResponse = (System.Net.HttpWebResponse)PROPPATCHRequest.GetResponse(); 


        //Move File 
        String destination = "http://myserver/exchange/" + strLoginName + "/##DAVMailSubmissionURI##/"; 
        System.Net.HttpWebRequest MOVERequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strMailboxURI); 
        MOVERequest.Credentials = MyCredentialCache; 
        MOVERequest.Method = "MOVE"; 
        MOVERequest.Headers.Add("Translate", "f"); 
        MOVERequest.Headers.Add("Destination", destination); 
        System.Net.HttpWebResponse MOVEResponse = (System.Net.HttpWebResponse)MOVERequest.GetResponse(); 


Many Thanks, 

Chaitanya


Answers (2)