Form Based Authentication on Exchange 2003, code snippet to send mail in C#



  • Hi All,

    Can some one help me with code snippet to send mail in C# when Form Based  Authentication is enabled in Exchange 2003 server.

    I am using WebDav code. It beautifuly works for NTML but for bombs for Form Based Authentication. 

    I am able to read and mark the mails as read with little changes in the code for Form Based Authentication. But to send mails I am unable to find any help.

     



  • People here will want to see what you've done so far, otherwise it just looks like a disguised homework question.



  • This below function to send a mail, intialy puts the mail in Draft and then sends the mail. It works perfectly fine for NTML but for Form Based Authentication while we are moving the mail from draft it gets hanged and the request gets timed out :( 

    Please look at the code snippet... 

     

    public void SendEmail(string server, string mailbox, string password, string to, string subject, string body, byte[][] streams)
            {
                //check the admin config if send emails to meeting organizers are allowed
                try
                {
                 
                    Console.WriteLine("----- Sending EMail -----");
                    
                    System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
                    string httpProtocol = s_bSecureHttp ? "https://" : "http://";
                    string mailboxUri = httpProtocol + server + "/" + mailbox;
                    string submissionUri = httpProtocol + server + "/" + mailbox + "/##DavMailSubmissionURI##/";
                    string draftsUri = httpProtocol + server + "/" + mailbox + "/drafts/" + Guid.NewGuid().ToString() + ".eml/";

                    if (subject != null && subject.Length == 0)
                    {
                        Console.WriteLine("Error: A blank email was being sent ####################");
                        return;
                    }
                    string message = "To: " + to + "\n" +
                    "Subject: " + subject + "\n" +
                    "Date: " + System.DateTime.Now +
                    "X-Mailer: mailer" + "\n" +
                    "MIME-Version: 1.0" + "\n" +
                    "Content-Type: text/plain;" + "\n" +
                    "Charset = "iso-8859-1"" + "\n" +
                    "Content-Transfer-Encoding: 7bit" + "\n" +
                    "\n" + body;
                    // Credentials for exchange requests.
                    CredentialCache credentials = new CredentialCache();
                    credentials.Add(new Uri(mailboxUri), "NTLM", new NetworkCredential(mailbox, password));

                    // Request to put an email the drafts folder.
                    HttpWebRequest putRequest = (HttpWebRequest)HttpWebRequest.Create(draftsUri);
                    if (s_AuthenticationType == "FBA")
                    {
                        //To Check if user is already logged in, if not logg in with user credentials.
                        if (s_sessionid == null || s_cadata == null)
                            Login_FBA();                
                //s_sessionid and s_cadata  are the two cookies stored after successfull log in.    
                        putRequest.Headers["Cookie"] = "sessionid=" + s_sessionid.Value + ",0x409; cadata=" + s_cadata.Value;
                    }
                    else
                        putRequest.Credentials = credentials;
                                   
                    
                    putRequest.Method = "PUT";
                    putRequest.ContentType = "message/rfc822";
                    byte[] bytes = Encoding.UTF8.GetBytes((string)message);
                    putRequest.ContentLength = bytes.Length;
                    Stream putRequestStream = putRequest.GetRequestStream();
                    putRequestStream.Write(bytes, 0, bytes.Length);
                    putRequestStream.Close();
                    WebResponse putResponse = (HttpWebResponse)putRequest.GetResponse();
                    
                    

                    //Do the PROPPATCH
                    string strxml = "<?xml version='1.0'?>" +
                    "<d:propertyupdate xmlns:d='DAV:'>" +
                    "<d:set>" +
                    "<d:prop>" +
                    "<isCollection xmlns='DAV:'>False</isCollection>" +
                    "</d:prop>" +
                    "</d:set>" +
                    "</d:propertyupdate>";

                    HttpWebRequest PROPPATCHRequest =(System.Net.HttpWebRequest)HttpWebRequest.Create(draftsUri);

                    if (s_AuthenticationType == "FBA")
                        PROPPATCHRequest.Headers["Cookie"] = "sessionid=" + s_sessionid.Value + ",0x409; cadata=" + s_cadata.Value;
                    else
                        PROPPATCHRequest.Credentials = credentials;

                    
                    
                    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;
                    Stream PROPPATCHRequestStream = PROPPATCHRequest.GetRequestStream();
                    PROPPATCHRequestStream.Write(PROPPATCHbytes, 0, PROPPATCHbytes.Length);
                    PROPPATCHRequestStream.Close();
                    WebResponse PROPPATCHResponse =
                    (System.Net.HttpWebResponse)PROPPATCHRequest.GetResponse();
            
            //If strema is empty fill up some junk
                    if (streams != null)
                    {
                        string FileName = "snapshot.jpg";
                        string attachURI = draftsUri + FileName;
                        HttpWebRequest PUTRequest1 = (System.Net.HttpWebRequest)HttpWebRequest.Create(attachURI);

                        if (s_AuthenticationType == "FBA")
                            PUTRequest1.Headers["Cookie"] = "sessionid=" + s_sessionid.Value + ",0x409; cadata=" + s_cadata.Value;
                        else
                            PUTRequest1.Credentials = credentials;

                        
                        PUTRequest1.Method = "PUT";
                        System.IO.FileStream inFile;
                        PUTRequest1.ContentLength = streams[0].Length;
                        Stream PUTRequestStream1 = PUTRequest1.GetRequestStream();
                        PUTRequestStream1.Write(streams[0], 0, streams[0].Length);
                        PUTRequestStream1.Close();
                        WebResponse PUTResponse1 = (System.Net.HttpWebResponse)PUTRequest1.GetResponse();
                    }
                    // Put the message in the Drafts folder of the sender's mailbox.
                    //putResponse.Close();
                    // Request to move the email from the drafts to the mail submission Uri.

                    HttpWebRequest moveRequest = (HttpWebRequest)HttpWebRequest.Create(draftsUri);
                    if (s_AuthenticationType == "FBA")
                    {
                        moveRequest.Headers["Cookie"] = "sessionid=" + s_sessionid.Value + ",0x409; cadata=" + s_cadata.Value;
                    }
                    else
                        moveRequest.Credentials = credentials;
                    
                    moveRequest.Method = "MOVE";
                    moveRequest.Headers.Add("Destination", submissionUri);

                    Console.WriteLine("@@@@@@ :: It bombssssssssssssssssssssssssssss @@@@@@@@@@");
                    WebResponse moveResponse = (HttpWebResponse)moveRequest.GetResponse();
                    Console.WriteLine("@@@@@@ :: doesn't come here :( ");
                    moveResponse.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    PrintException(ex);
                    Console.WriteLine("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                }
            }



  • You may want to dump the following information when doing the HTTP requests:

    • The response header
    • The response content
    • The error code/string of your HTTP library
    Then get the same information from your browser when you do this manually, and compare the two.

    However, I have to ask why don't you just send the email over SMTP like everyone else does? (Add a BCC to yourself so you get a copy)

Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.