nsXPIDLCString folderUriWithNamespace;
LocateMessageFolder(nsIMsgIdentity   *userIdentity,                     nsMsgDeliverMode aFolderType,                    const char       *aFolderURI,                    nsIMsgFolder     **msgFolder){  nsresult                  rv = NS_OK;  RETURN_SIMULATED_ERROR(SIMULATED_SEND_ERROR_5, NS_ERROR_FAILURE)    if (!msgFolder) return NS_ERROR_NULL_POINTER;  *msgFolder = nsnull;  if (!aFolderURI || (PL_strlen(aFolderURI) == 0)) {    return NS_ERROR_INVALID_ARG;  }    // as long as it doesn't start with anyfolder://  if (PL_strncasecmp(ANY_SERVER, aFolderURI, PL_strlen(aFolderURI)) != 0) {    nsCOMPtr<nsIRDFService> rdf(do_GetService(kRDFServiceCID, &rv));    if (NS_FAILED(rv)) return rv;    // get the corresponding RDF resource    // RDF will create the folder resource if it doesn't already exist    nsCOMPtr<nsIRDFResource> resource;    rv = rdf->GetResource(aFolderURI, getter_AddRefs(resource));    if (NS_FAILED(rv)) return rv;    nsCOMPtr <nsIMsgFolder> folderResource;    folderResource = do_QueryInterface(resource, &rv);    if (NS_SUCCEEDED(rv) && folderResource)     {        // don't check validity of folder - caller will handle creating it      nsCOMPtr<nsIMsgIncomingServer> server; //make sure that folder hierarchy is built so that legitimate parent-child relationship is established      folderResource->GetServer(getter_AddRefs(server));      if (server)      {        nsCOMPtr<nsIMsgFolder> rootMsgFolder;        server->GetRootMsgFolder(getter_AddRefs(rootMsgFolder));        if (rootMsgFolder)        {          nsCOMPtr<nsIImapIncomingServer> imapServer = do_QueryInterface(server);          // Make sure an specific IMAP folder has correct personal namespace          // See bugzilla bug 90494 (http://bugzilla.mozilla.org/show_bug.cgi?id=90494)          if (imapServer)          {            nsXPIDLCString folderUriWithNamespace;            imapServer->GetUriWithNamespacePrefixIfNecessary(kPersonalNamespace, aFolderURI, getter_Copies(folderUriWithNamespace));            if (!folderUriWithNamespace.IsEmpty())              rv = rootMsgFolder->GetChildWithURI(folderUriWithNamespace, PR_TRUE, PR_FALSE, msgFolder);            else              rv = rootMsgFolder->GetChildWithURI(aFolderURI, PR_TRUE, PR_FALSE, msgFolder);          }          else            rv = rootMsgFolder->GetChildWithURI(aFolderURI, PR_TRUE, imapServer == nsnull /*caseInsensitive*/, msgFolder);          /* we didn't find the folder so we will have to create new one.             CreateIfMissing does that provided we pass in a dummy folder */          if (!*msgFolder)          {            *msgFolder = folderResource;              NS_ADDREF(*msgFolder);          }          return rv;        }        else          return NS_MSG_ERROR_FOLDER_MISSING;      }      else	      return NS_MSG_ERROR_FOLDER_MISSING;    }    else     {	    return NS_ERROR_FAILURE;    }  }  else   {    PRUint32                  cnt = 0;    PRUint32                  i;    if (!userIdentity)      return NS_ERROR_INVALID_ARG;    // get the account manager    nsCOMPtr<nsIMsgAccountManager> accountManager =              do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);    if (NS_FAILED(rv)) return rv;    // if anyfolder will do, go look for one.    nsCOMPtr<nsISupportsArray> retval;     accountManager->GetServersForIdentity(userIdentity, getter_AddRefs(retval));     if (!retval) return NS_ERROR_FAILURE;        // Ok, we have to look through the servers and try to find the server that    // has a valid folder of the type that interests us...    rv = retval->Count(&cnt);    if (NS_FAILED(rv)) return rv;        for (i=0; i<cnt; i++) {      // Now that we have the server...we need to get the named message folder      nsCOMPtr<nsIMsgIncomingServer> inServer;       nsCOMPtr<nsISupports>ptr;      ptr = getter_AddRefs(retval->ElementAt(i));            inServer = do_QueryInterface(ptr, &rv);      if(NS_FAILED(rv) || (!inServer))        continue;            //      // If aFolderURI is passed in, then the user has chosen a specific      // mail folder to save the message, but if it is null, just find the      // first one and make that work. The folder is specified as a URI, like      // the following:      //      //		  mailbox://nobody@Local Folders/Sent      //                  imap://rhp@nsmail-2/Drafts      //                  newsgroup://news.mozilla.org/netscape.test      //      char *serverURI = nsnull;      rv = inServer->GetServerURI(&serverURI);      if ( NS_FAILED(rv) || (!serverURI) || !(*serverURI) )        continue;            nsCOMPtr <nsIFolder> folder;      rv = inServer->GetRootFolder(getter_AddRefs(folder));      if (NS_FAILED(rv) || (!folder))        continue;            nsCOMPtr<nsIMsgFolder> rootFolder;      rootFolder = do_QueryInterface(folder, &rv);            if(NS_FAILED(rv) || (!rootFolder))        continue;            PRUint32 numFolders = 0;            // use the defaults by getting the folder by flags      if (aFolderType == nsIMsgSend::nsMsgQueueForLater)       // QueueForLater (Outbox)        {          rv = rootFolder->GetFoldersWithFlag(MSG_FOLDER_FLAG_QUEUE, 1, &numFolders, msgFolder);        }      else if (aFolderType == nsIMsgSend::nsMsgSaveAsDraft)    // SaveAsDraft (Drafts)        {          rv = rootFolder->GetFoldersWithFlag(MSG_FOLDER_FLAG_DRAFTS, 1, &numFolders, msgFolder);        }      else if (aFolderType == nsIMsgSend::nsMsgSaveAsTemplate) // SaveAsTemplate (Templates)        {          rv = rootFolder->GetFoldersWithFlag(MSG_FOLDER_FLAG_TEMPLATES, 1, &numFolders, msgFolder);        }      else // SaveInSentFolder (Sent) -  nsMsgDeliverNow or nsMsgSendUnsent        {          rv = rootFolder->GetFoldersWithFlag(MSG_FOLDER_FLAG_SENTMAIL, 1, &numFolders, msgFolder);        }      if (NS_SUCCEEDED(rv) && *msgFolder) {	return NS_OK;      }    }  }    return NS_ERROR_FAILURE;}