if (NS_FAILED(rv))
if (NS_FAILED(rv) || !mailSession)
int main(int argc, char *argv[]) {   nsIMsgCompFields *pMsgCompFields;  nsIMsgSend *pMsgSend;  nsresult rv = NS_OK;  nsIAppShellService* appShell = nsnull;  nsComponentManager::RegisterComponent(kNetServiceCID, NULL, NULL, NETLIB_DLL, PR_FALSE, PR_FALSE);	nsComponentManager::RegisterComponent(kEventQueueServiceCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);	nsComponentManager::RegisterComponent(kEventQueueCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);	nsComponentManager::RegisterComponent(kPrefCID, nsnull, nsnull, PREF_DLL, PR_TRUE, PR_TRUE);	nsComponentManager::RegisterComponent(kFileLocatorCID,  NULL, NS_FILELOCATOR_PROGID, APPSHELL_DLL, PR_TRUE, PR_TRUE);	nsComponentManager::RegisterComponent(kMimeURLUtilsCID,  NULL, NULL, MIME_DLL, PR_FALSE, PR_FALSE);	nsComponentManager::RegisterComponent(kNetSupportDialogCID,  NULL, NULL, APPSHELL_DLL, PR_FALSE, PR_FALSE);	nsComponentManager::RegisterComponent(kAppShellServiceCID,  NULL, NULL, APPSHELL_DLL, PR_FALSE, PR_FALSE);  SetupRegistry();  /*   * Create the Application Shell instance...   */  nsCOMPtr<nsIWebShellWindow> newWindow;  rv = nsServiceManager::GetService(kAppShellServiceCID,                                    kIAppShellServiceIID,                                   (nsISupports**)&appShell);	if (NS_SUCCEEDED(rv))     appShell->CreateTopLevelWindow(nsnull, nsnull, PR_TRUE, getter_AddRefs(newWindow),                nsnull, nsnull, 200, 200);  // Create the Event Queue for this thread...	NS_WITH_SERVICE(nsIEventQueueService, pEventQService, kEventQueueServiceCID, &rv); 	if (NS_FAILED(rv))   {    printf("Failed to get event queue\n");    return rv;  }  rv = pEventQService->CreateThreadEventQueue();	if (NS_FAILED(rv))   {    printf("Failed to create event queue\n");    return rv;  }	// make sure prefs get initialized and loaded..	// mscott - this is just a bad bad bad hack right now until prefs	// has the ability to take nsnull as a parameter. Once that happens,	// prefs will do the work of figuring out which prefs file to load...	NS_WITH_SERVICE(nsIPref, prefs, kPrefCID, &rv);   if (NS_FAILED(rv) || (prefs == nsnull)) {    exit(rv);  }  if (NS_FAILED(prefs->StartUp()))  {    printf("Failed to start up prefs!\n");    exit(rv);  }  if (NS_FAILED(prefs->ReadUserPrefs()))  {    printf("Failed on reading user prefs!\n");    exit(rv);  }  NS_WITH_SERVICE(nsIMsgMailSession, mailSession, kCMsgMailSessionCID, &rv);  if (NS_FAILED(rv))   {    printf("Failure on Mail Session Init!\n");    return rv;  }    nsIMsgIdentity *identity = GetHackIdentity();  OnIdentityCheck();  rv = nsComponentManager::CreateInstance(kMsgCompFieldsCID, NULL,                                            nsIMsgCompFields::GetIID(), (void **) &pMsgCompFields);     if (rv == NS_OK && pMsgCompFields) {     printf("We succesfully obtained a nsIMsgCompFields interface....\n");    printf("Releasing the interface now...\n");    pMsgCompFields->Release();   }     rv = nsComponentManager::CreateInstance(kMsgSendCID, NULL, kIMsgSendIID, (void **) &pMsgSend);   if (rv == NS_OK && pMsgSend)   {     printf("We succesfully obtained a nsIMsgSend interface....\n");        rv = nsComponentManager::CreateInstance(kMsgCompFieldsCID, NULL, kIMsgCompFieldsIID,                                              (void **) &pMsgCompFields);     if (rv == NS_OK && pMsgCompFields)    {       PRInt32 rv;      pMsgCompFields->SetFrom(", rhp@netscape.com, ", NULL);      pMsgCompFields->SetTo("rhp@netscape.com", NULL);      pMsgCompFields->SetSubject("[spam] test", NULL);      // pMsgCompFields->SetTheForcePlainText(PR_TRUE, &rv);      pMsgCompFields->SetBody(email, NULL);      pMsgCompFields->SetCharacterSet("us-ascii", NULL);      PRInt32 nBodyLength;      char    *pBody;	    pMsgCompFields->GetBody(&pBody);	    if (pBody)		    nBodyLength = PL_strlen(pBody);	    else		    nBodyLength = 0;      nsMsgAttachedFile *ptr = NULL;      nsMsgAttachmentData *newPtr = NULL;            newPtr = GetRemoteAttachments();            //ptr = GetAttachments();      char *tagBuf = (char *)PR_Malloc(256);      if (tagBuf)        PL_strcpy(tagBuf, "Do that voodo, that you do, soooo weeeelllll!");            pMsgSend->CreateAndSendMessage(pMsgCompFields, 						    PR_FALSE,         // PRBool                            digest_p,                PR_FALSE,         // PRBool                            dont_deliver_p,						    nsMsgDeliverNow,   // nsMsgDeliverMode                  mode,						    TEXT_HTML, //TEXT_PLAIN,       // const char                        *attachment1_type,						    pBody,            // const char                        *attachment1_body,						    nBodyLength,      // PRUint32                          attachment1_body_length,						    newPtr,           // const struct nsMsgAttachmentData   *attachments,						    ptr,              // const struct nsMsgAttachedFile     *preloaded_attachments,						    NULL,             // nsMsgSendPart                     *relatedPart,						    nsnull);          // listener array      PR_FREEIF(ptr);    }      }#ifdef XP_PC  printf("Sitting in an event processing loop ...Hit Cntl-C to exit...");  while (1)  {    MSG msg;    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))    {      TranslateMessage(&msg);      DispatchMessage(&msg);    }  }#endif  printf("Releasing the interface now...\n");  pMsgSend->Release();   pMsgCompFields->Release();   return 0; }