Exemplo n.º 1
0
void MailMessageTest::testWriteMultiPart()
{
	MailMessage message;
	MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "*****@*****.**", "John Doe");
	message.addRecipient(r1);
	message.setSubject("Test Message");
	message.setSender("*****@*****.**");
	Timestamp ts(0);
	message.setDate(ts);
	message.addContent(new StringPartSource("Hello World!\r\n", "text/plain"), MailMessage::ENCODING_8BIT);
	StringPartSource* pSPS = new StringPartSource("This is some binary data. Really.", "application/octet-stream", "sample.dat");
	pSPS->headers().set("Content-ID", "abcd1234");
	message.addAttachment("sample", pSPS);

	assert (message.isMultipart());

	std::ostringstream str;
	message.write(str);
	std::string s = str.str();
	std::string rawMsg(
		"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
		"Content-Type: multipart/mixed; boundary=$\r\n"
		"Subject: Test Message\r\n"
		"From: [email protected]\r\n"
		"To: John Doe <*****@*****.**>\r\n"
		"Mime-Version: 1.0\r\n"
		"\r\n"
		"--$\r\n"
		"Content-Type: text/plain\r\n"
		"Content-Transfer-Encoding: 8bit\r\n"
		"Content-Disposition: inline\r\n"
		"\r\n"
		"Hello World!\r\n"
		"\r\n"
		"--$\r\n"
		"Content-ID: abcd1234\r\n"
		"Content-Type: application/octet-stream; name=sample\r\n"
		"Content-Transfer-Encoding: base64\r\n"
		"Content-Disposition: attachment; filename=sample.dat\r\n"
		"\r\n"
		"VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhLiBSZWFsbHku\r\n"
		"--$--\r\n"
	);
	std::string::size_type p1 = s.find('=') + 1;
	std::string::size_type p2 = s.find('\r', p1);
	std::string boundary(s, p1, p2 - p1);
	std::string msg;
	for (std::string::const_iterator it = rawMsg.begin(); it != rawMsg.end(); ++it)
	{
		if (*it == '$')
			msg += boundary;
		else
			msg += *it;
	}

	assert (s == msg);
}
Exemplo n.º 2
0
void SMTPChannel::log(const Message& msg)
{
	try
	{
		MailMessage message;
		message.setSender(_sender);
		message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, _recipient));
		message.setSubject("Log Message from " + _sender);
		std::stringstream content;
		content << "Log Message\r\n"
			<< "===========\r\n\r\n"
			<< "Host: " << Environment::nodeName() << "\r\n"
			<< "Logger: " << msg.getSource() << "\r\n";

		if (_local)
		{
			DateTime dt(msg.getTime());
			content	<< "Timestamp: " << DateTimeFormatter::format(LocalDateTime(dt), DateTimeFormat::RFC822_FORMAT) << "\r\n";
		}
		else
			content	<< "Timestamp: " << DateTimeFormatter::format(msg.getTime(), DateTimeFormat::RFC822_FORMAT) << "\r\n";

		content	<< "Priority: " << NumberFormatter::format(msg.getPriority()) << "\r\n"
			<< "Process ID: " << NumberFormatter::format(msg.getPid()) << "\r\n"
			<< "Thread: " << msg.getThread() << " (ID: " << msg.getTid() << ")\r\n"
			<< "Message text: " << msg.getText() << "\r\n\r\n";

		message.addContent(new StringPartSource(content.str()));
	
		if (!_attachment.empty())
		{
			{
				Poco::FileInputStream fis(_attachment, std::ios::in | std::ios::binary | std::ios::ate);
				if (fis.good())
				{
					int size = fis.tellg();
					char* pMem = new char [size];
					fis.seekg(std::ios::beg);
					fis.read(pMem, size);
					message.addAttachment(_attachment, new StringPartSource(std::string(pMem, size), _type, _attachment));
					delete [] pMem;
				}
			}
			if (_delete) File(_attachment).remove();
		}

		SMTPClientSession session(_mailHost);
		session.login();
		session.sendMessage(message);
		session.close();
	} 
	catch (Exception&) 
	{ 
		if (_throw) throw; 
	}
}
Exemplo n.º 3
0
Arquivo: Mail.cpp Projeto: 12307/poco
int main(int argc, char** argv)
{
	SSLInitializer sslInitializer;

	if (argc < 4)
	{
		Path p(argv[0]);
		std::cerr << "usage: " << p.getBaseName() << " <mailhost> <sender> <recipient> [<username> <password>]" << std::endl;
		std::cerr << "       Send an email greeting from <sender> to <recipient>," << std::endl;
		std::cerr << "       using a secure connection to the SMTP server at <mailhost>." << std::endl;
		return 1;
	}
	
	std::string mailhost(argv[1]);
	std::string sender(argv[2]);
	std::string recipient(argv[3]);
	std::string username(argc >= 5 ? argv[4] : "");
	std::string password(argc >= 6 ? argv[5] : "");
	
	try
	{
		// Note: we must create the passphrase handler prior Context 
		SharedPtr<InvalidCertificateHandler> pCert = new ConsoleCertificateHandler(false); // ask the user via console
		Context::Ptr pContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
		SSLManager::instance().initializeClient(0, pCert, pContext);

		MailMessage message;
		message.setSender(sender);
		message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient));
		message.setSubject("Hello from the POCO C++ Libraries");
		std::string content;
		content += "Hello ";
		content += recipient;
		content += ",\r\n\r\n";
		content += "This is a greeting from the POCO C++ Libraries.\r\n\r\n";
		std::string logo(reinterpret_cast<const char*>(PocoLogo), sizeof(PocoLogo));
		message.addContent(new StringPartSource(content));
		message.addAttachment("logo", new StringPartSource(logo, "image/gif"));
		
		SecureSMTPClientSession session(mailhost);
		session.login();
		session.startTLS(pContext);
		if (!username.empty())
		{
			session.login(SMTPClientSession::AUTH_LOGIN, username, password);
		}
		session.sendMessage(message);
		session.close();
	}
	catch (Exception& exc)
	{
		std::cerr << exc.displayText() << std::endl;
		return 1;
	}
	return 0;
}
Exemplo n.º 4
0
int main(int argc, char** argv)
{
	if (argc != 4)
	{
		Path p(argv[0]);
		std::cerr << "usage: " << p.getBaseName() << " <mailhost> <sender> <recipient>" << std::endl;
		std::cerr << "       Send an email greeting from <sender> to <recipient>," << std::endl;
		std::cerr << "       the SMTP server at <mailhost>." << std::endl;
		return 1;
	}
	
	std::string mailhost(argv[1]);
	std::string sender(argv[2]);
	std::string recipient(argv[3]);
	
	try
	{
		MailMessage message;
		message.setSender(sender);
		message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient));
		message.setSubject("Hello from the POCO C++ Libraries");
		std::string content;
		content += "Hello ";
		content += recipient;
		content += ",\r\n\r\n";
		content += "This is a greeting from the POCO C++ Libraries.\r\n\r\n";
		std::string logo(reinterpret_cast<const char*>(PocoLogo), sizeof(PocoLogo));
		message.addContent(new StringPartSource(content));
		message.addAttachment("logo", new StringPartSource(logo, "image/gif"));
		
		SMTPClientSession session(mailhost);
		session.login();
		session.sendMessage(message);
		session.close();
	}
	catch (Exception& exc)
	{
		std::cerr << exc.displayText() << std::endl;
		return 1;
	}
	return 0;
}
Exemplo n.º 5
0
void Test_MailMessage()
{
    std::string sender("*****@*****.**");
    std::string recipient("*****@*****.**");
    std::string subject("Generated email");
    std::string content = "Hi ";
    content += recipient;
    content += ",\n\n";
    content += "Have a good day!\n\n";
    content += "Regards,\n";
    content += "A-Team";
    std::string attachment("C:\\DSC.jpg");

    MailMessage message;
    message.setSender( sender );
    message.addRecipient( MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient) );
    message.setSubject( subject );
    message.setContent( content );
    message.addAttachment( attachment );

    send(message);
}
Exemplo n.º 6
0
int main(int argc, char** argv)
#endif
{
    size_t msglen=0;
    WCHAR *name[10], *msg=0;
    int i;
    const WCHAR *attach;

#ifdef _WIN32_WCE
    name[0] = stringdup(TEXT("message.xml"));
    name[1] = stringdup(TEXT("text.xml"));
    name[2] = NULL;
    attach = TEXT("/synclog.txt");
#else
    for(i=1; i<argc; i++)
       name[i-1] = utf82wc(argv[i]);
    name[i-1] = NULL;
    attach = TEXT("c:/windows/temp/synclog.txt");
#endif
    LOG.setLevel(LOG_LEVEL_DEBUG); // Force debug level for this test.

    // Test parse/format loop if names were given on cmdline
    for (i=0; name[i]; i++) {
        msg = loadAndConvert(name[i]);
        if( !msg ){
            WCHAR dbg[256];
            wsprintf(dbg, TEXT("Can't open file %s\n"), name[i]);
	        LOG.error("%s", dbg);
            continue;
        }

    	EmailData em;

        if (em.parse(msg))
            fprintf(stderr, "Parse failed on: %S\n", name[i]);

        delete [] msg;

        WCHAR outname[10];
        wsprintf(outname, TEXT("msgout%d.xml"), i);
        if ( convertAndSave( outname, em.format() ) ) {
            fprintf(stderr, "Error in convertAndSave(em)\n");
        }
    }

    // Try to send a new mail with attachment
    EmailData newmail;
    MailMessage n;
    BodyPart body;

    body.setContent(TEXT("Ma che bella la città!"));

    n.setFrom(TEXT("*****@*****.**"));
    n.setTo(TEXT("*****@*****.**"));
    n.setSubject(TEXT("Test"));
    n.setBody(body);

	BodyPart a;
	a.setFilename( TEXT("pippo.txt") );
	a.setContent( attach );
	a.setEncoding( TEXT("base64") );
    n.addAttachment(a);

    newmail.setRead(true);
    newmail.setEmailItem(n);

    if ( convertAndSave( L"attachment.xml", newmail.format() ) ) {
        fprintf(stderr, "Error in convertAndSave(newmail)\n");
    }
    //extern size_t StringBuffer_memcount;
    //fprintf(stderr, "Memcount: %ld\n", StringBuffer_memcount);
    //getchar();
	return 0;
}