Overview
Features
Download
Documentation
Community
Add-Ons & Services

PROB RECREATING BINARY FILE ATTACHMENTS

Please post support and help requests here.

PROB RECREATING BINARY FILE ATTACHMENTS

Postby selva » 21 Feb 2008, 15:00

Hi
I managed to create emails to send via the SMTP component with the following code:
Code: Select all

   MailMessage message;
   message.setSender"sender@abcd.com");
   message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, "receiver@abcd.com"));
   message.setSubject("My Subject");
   message.set("X-Classification", "Confidential");
   message.set("X-Encrypt", "On");

   string content;
   content += "My message text";
   message.addContent(new StringPartSource(content));

   unsigned char * pAttachment  = NULL;
   CString sFilename("c:\some_binary_file.rar");
   ifstream fin;
   unsigned long ulSize;
      
   fin.open(sFilename, ios_base::binary);
   if (fin == NULL){
      MessageBox("File open failed!");
   }
   fin.seekg(0,ios_base::end);
   ulSize = fin.tellg();
   fin.seekg(0,ios_base::beg);

   if (pAttachment){
      delete [] pAttachment;
   }
   pAttachment = new unsigned char[ulSize];
   if (pAttachment == NULL){
      fin.close();
      MessageBox("Memory allocation error");
   }
   fin.read((char *) pAttachment, ulSize);
   fin.close();

   string attachFile(reinterpret_cast(pAttachment), ulSize);
   message.addAttachment("some_binary_file.rar", new StringPartSource(attachFile));
//alternative      message.addAttachment("some_binary_file.rar", new StringPartSource(attachFile, "application/octet-stream", "some_binary_file.rar"));

   SMTPClientSession session(mailhostStr);
   session.login();
   session.sendMessage(message);
   session.close();


For receiving the mail, following is my code:

Code: Select all

// RecvMsg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Poco/Net/POP3ClientSession.h"
#include "Poco/Net/MailMessage.h"
#include "Poco/Net/MailRecipient.h"
#include "Poco/Net/MailStream.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/Net/NetException.h"
#include
#include
#include "Poco/Exception.h"
#include "Poco/Net/PartHandler.h"
#include
#include
#include

using Poco::Exception;
using Poco::Net::POP3ClientSession;

using namespace Poco::Net;
using Poco::Net::MessageHeader;

using namespace std;


void writeToFile(string filename, const string data, int dataSize)
{
   cout<<"writing to file: "<   cout<<"file size: "<   ofstream fout(filename.c_str(), ios_base::binary);
   fout.write((char *) &data[0], dataSize);
   fout.close();
}


namespace
{
   class StringPartHandler: public PartHandler
   {
   public:
      StringPartHandler()
      {
      }
      
      void handlePart(const MessageHeader& header, std::istream& stream)
      {
         _disp.push_back(header["Content-Disposition"]);
         _type.push_back(header["Content-Type"]);
         
         if (header["Content-Disposition"].compare(" attachment")){
            _filename.push_back(getAttachfilename(header["Content-Type"]));
         }else {
            _filename.push_back("");
         }
         std::string data;
         int ch = stream.get();
         while (ch > 0)
         {
            data += (char) ch;
            ch = stream.get();
         }
         _data.push_back(data);
      }
      
      const std::vector& data() const
      {
         return _data;
      }

      const std::vector& disp() const
      {
         return _disp;
      }

      const std::vector& type() const
      {
         return _type;
      }
      
      const std::vector& attachFileName() const
      {
         return _filename;      }
      
   private:
      std::vector _data;
      std::vector _disp;
      std::vector _type;
      std::vector _filename;
      string getAttachfilename(string contentDisposition){
         string tmp;
         int pos = contentDisposition.find("name=");
         if (pos<0){
           tmp = "";
         }else{
            //cout<<"<<<>>>= "<            tmp = contentDisposition.substr(pos+5, 255);
         }
         return tmp;
      }

   };
}


int _tmain(int argc, _TCHAR* argv[])
{
   cout<<"starting smtp"<   try
   {
      string sMailHost = "mail.abcd.com";
      POP3ClientSession *session = new POP3ClientSession(sMailHost);
      session->login("user1", "P@ssw0rd");
      std::cout<<"login successful"<
      cout<<"Msg count: "<messageCount()<      POP3ClientSession::MessageInfoVec *msgInfoVec = new POP3ClientSession::MessageInfoVec();
      //POP3ClientSession::MessageInfo msgInfo;
      POP3ClientSession::MessageInfoVec::iterator theIterator;
      POP3ClientSession::MessageInfo msgInfo;
      MailMessage msg;
      
      session->listMessages(*msgInfoVec);
      cout<<"starting iteration
"<      StringPartHandler *msgParts = NULL;
      for (theIterator = msgInfoVec->begin();theIterator != msgInfoVec->end();theIterator++){
         msgInfo = *theIterator;
         cout<<"ID: "<         
         msgParts = new StringPartHandler();
         session->retrieveMessage(msgInfo.id, msg, *msgParts);
         int iNumAttachments = (*msgParts).data().size();

         cout<<"Date: "<         cout<<"From: "<         cout<<"Subject: "<         cout<<"msgParts.data().size(): "<<(*msgParts).data().size()<         for (int idx=0;idx         //cout<<"msgParts.data()[0]: "<<(*msgParts).data()[idx]<            cout<<"msgParts.type()["<            cout<<"msgParts.disp()["<            cout<<"msgParts.attachFileName()["<            string filename = (*msgParts).attachFileName()[idx];
            if (filename.compare("")!=0){
               cout<<"var filename = "<               writeToFile("c:\maildownload\"+filename, (*msgParts).data()[idx], (*msgParts).data()[idx].);

            } else {
            }
            
            
         }
         cout<<"----------------------"<         msgParts = NULL;
      }

      
      session->close();
      cout<<"going home"<   }
   catch (Exception& exc)
   {
      std::cerr << exc.displayText() << std::endl;
      return 1;
   }
   return 0;
}


Pardon me for my rather bad code as I am relatively new to C++ programming. I can only get better though(:cool:)

Tks.


Selva
selva
 
Posts: 2
Joined: 17 Feb 2008, 17:00

Re: PROB RECREATING BINARY FILE ATTACHMENTS

Postby selva » 21 Feb 2008, 15:05

In my haste, I forgot to mention my problem. I can send the emails ok. And I am able to receive and view the messages sent via Outlook or any other mail client fine. However, the program that I wrote to receive the mail and to write the file to disk fails. The attached file is always only 3 or 5 bytes long each time I use the Recvmsg application. This happens only for binary files. When I send text files like Readme.txt, the sendmsg and recvmsg apps I wrote work fine. Appreciate some help with this as I am at my wits end.

Thank You.


Selva
selva
 
Posts: 2
Joined: 17 Feb 2008, 17:00

Re: PROB RECREATING BINARY FILE ATTACHMENTS

Postby peter » 21 Feb 2008, 22:30

Can you please repost that code to the sourceforge mailing list?
The Wiki is really bad for reading code...

Peter

> Hi
> I managed to create emails to send via the SMTP component with the following code:
>
Code: Select all

>    MailMessage message;
>    message.setSender"sender@abcd.com");
>    message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, "receiver@abcd.com"));
>    message.setSubject("My Subject");
>    message.set("X-Classification", "Confidential");
>    message.set("X-Encrypt", "On");
>
>    string content;
>    content += "My message text";
>    message.addContent(new StringPartSource(content));
>
>    unsigned char * pAttachment  = NULL;
>    CString sFilename("c:\some_binary_file.rar");
>    ifstream fin;
>    unsigned long ulSize;
>       
>    fin.open(sFilename, ios_base::binary);
>    if (fin == NULL){
>       MessageBox("File open failed!");
>    }
>    fin.seekg(0,ios_base::end);
>    ulSize = fin.tellg();
>    fin.seekg(0,ios_base::beg);
>
>    if (pAttachment){
>       delete [] pAttachment;
>    }
>    pAttachment = new unsigned char[ulSize];
>    if (pAttachment == NULL){
>       fin.close();
>       MessageBox("Memory allocation error");
>    }
>    fin.read((char *) pAttachment, ulSize);
>    fin.close();
>
>    string attachFile(reinterpret_cast(pAttachment), ulSize);
>    message.addAttachment("some_binary_file.rar", new StringPartSource(attachFile));
> //alternative      message.addAttachment("some_binary_file.rar", new StringPartSource(attachFile, "application/octet-stream", "some_binary_file.rar"));
>
>    SMTPClientSession session(mailhostStr);
>    session.login();
>    session.sendMessage(message);
>    session.close();
>

>
> For receiving the mail, following is my code:
>
> [code]
> // RecvMsg.cpp : Defines the entry point for the console application.
> //
>
> #include "stdafx.h"
> #include "Poco/Net/POP3ClientSession.h"
> #include "Poco/Net/MailMessage.h"
> #include "Poco/Net/MailRecipient.h"
> #include "Poco/Net/MailStream.h"
> #include "Poco/Net/SocketAddress.h"
> #include "Poco/Net/SocketStream.h"
> #include "Poco/Net/NetException.h"
> #include
> #include
> #include "Poco/Exception.h"
> #include "Poco/Net/PartHandler.h"
> #include
> #include
> #include
>
> using Poco::Exception;
> using Poco::Net::POP3ClientSession;
>
> using namespace Poco::Net;
> using Poco::Net::MessageHeader;
>
> using namespace std;
>
>
> void writeToFile(string filename, const string data, int dataSize)
> {
> cout public:
> StringPartHandler()
> {
> }
>
> void handlePart(const MessageHeader& header, std::istream& stream)
> {
> _disp.push_back(header["Content-Disposition"]);
> _type.push_back(header["Content-Type"]);
>
> if (header["Content-Disposition"].compare(" attachment")){
> _filename.push_back(getAttachfilename(header["Content-Type"]));
> }else {
> _filename.push_back("");
> }
> std::string data;
> int ch = stream.get();
> while (ch > 0)
> {
> data += (char) ch;
> ch = stream.get();
> }
> _data.push_back(data);
> }
>
> const std::vector& data() const
> {
> return _data;
> }
>
> const std::vector& disp() const
> {
> return _disp;
> }
>
> const std::vector& type() const
> {
> return _type;
> }
>
> const std::vector& attachFileName() const
> {
> return _filename; }
>
> private:
> std::vector _data;
> std::vector _disp;
> std::vector _type;
> std::vector _filename;
> string getAttachfilename(string contentDisposition){
> string tmp;
> int pos = contentDisposition.find("name=");
> if (pos tmp = "";
> }else{
> //cout };
> }
>
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> cout POP3ClientSession *session = new POP3ClientSession(sMailHost);
> session->login("user1", "P@ssw0rd");
> std::cout session->listMessages(*msgInfoVec);
> coutend();theIterator++){
> msgInfo = *theIterator;
> cout
peter
 
Posts: 67
Joined: 11 Jul 2006, 16:26
Location: Austria


Return to Support

Who is online

Users browsing this forum: Schmendrick and 2 guests