Click here to Skip to main content
15,879,095 members
Articles / Web Development / IIS

Reading a text file in ASP.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (15 votes)
18 Mar 2009CPOL 763.2K   4.1K   54  
How to read a text file on a server using VBScript in ASP
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library.  Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//

#include "tests/testUtils.hpp"
#include "vmime/htmlTextPart.hpp"


VMIME_TEST_SUITE_BEGIN(htmlTextPartTest)

	VMIME_TEST_LIST_BEGIN
		VMIME_TEST(testParseText)
		VMIME_TEST(testParseEmbeddedObjectsCID)
		VMIME_TEST(testParseEmbeddedObjectsLocation)
	VMIME_TEST_LIST_END


	static const vmime::string extractContent
		(vmime::ref <const vmime::contentHandler> cth)
	{
		std::ostringstream oss;
		vmime::utility::outputStreamAdapter osa(oss);

		cth->extract(osa);

		return oss.str();
	}


	void testParseText()
	{
		const vmime::string msgString = ""
"MIME-Version: 1.0\r\n"
"Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n"
"\r\n"
"--LEVEL1\r\n"
"Content-Type: text/plain; charset=\"x-ch1\"\r\n"
"\r\n"
"Plain text part\r\n"
"--LEVEL1\r\n"
"Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n"
"\r\n"
"--LEVEL2\r\n"
"Content-Type: text/html; charset=\"x-ch2\"\r\n"
"\r\n"
"<img src=\"cid:image@test\"/>\r\n"
"--LEVEL2\r\n"
"Content-Type: image/png; name=\"image.png\"\r\n"
"Content-Disposition: inline; filename=\"image.png\"\r\n"
"Content-ID: <image@test>\r\n"
"\r\n"
"Image\r\n"
"--LEVEL2--\r\n"
"\r\n"
"--LEVEL1--\r\n"
"";

		vmime::ref <vmime::message> msg = vmime::create <vmime::message>();
		msg->parse(msgString);

		// Sanity checks
		VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount());
		VASSERT_EQ("part-count2", 2, msg->getBody()->getPartAt(1)->getBody()->getPartCount());

		vmime::htmlTextPart htmlPart;
		htmlPart.parse(msg, msg->getBody()->getPartAt(1),
			msg->getBody()->getPartAt(1)->getBody()->getPartAt(0));

		VASSERT_EQ("plain", "Plain text part", extractContent(htmlPart.getPlainText()));
		VASSERT_EQ("html", "<img src=\"cid:image@test\"/>", extractContent(htmlPart.getText()));

		// Should return the charset of the HTML part
		VASSERT_EQ("charset", "x-ch2", htmlPart.getCharset().generate());
	}

	/** Test parsing of embedded objects by CID (Content-Id).
	  */
	void testParseEmbeddedObjectsCID()
	{
		const vmime::string msgString = ""
"MIME-Version: 1.0\r\n"
"Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n"
"\r\n"
"--LEVEL1\r\n"
"Content-Type: text/plain; charset=\"x-ch1\"\r\n"
"\r\n"
"Plain text part\r\n"
"--LEVEL1\r\n"
"Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n"
"\r\n"
"--LEVEL2\r\n"  // one embedded object before...
"Content-Type: image/png; name=\"image1.png\"\r\n"
"Content-Disposition: inline; filename=\"image1.png\"\r\n"
"Content-ID: <image1@test>\r\n"
"\r\n"
"Image1\r\n"
"--LEVEL2\r\n"  // ...the actual text part...
"Content-Type: text/html; charset=\"x-ch2\"\r\n"
"\r\n"
"<img src=\"cid:image1@test\"/>\r\n"
"<img src=\"CID:image2@test\"/>\r\n"
"--LEVEL2\r\n"  // ...and one after
"Content-Type: image/jpeg; name=\"image2.jpg\"\r\n"
"Content-Disposition: inline; filename=\"image2.jpg\"\r\n"
"Content-ID: <image2@test>\r\n"
"\r\n"
"Image2\r\n"
"--LEVEL2--\r\n"
"\r\n"
"--LEVEL1--\r\n"
"";

		vmime::ref <vmime::message> msg = vmime::create <vmime::message>();
		msg->parse(msgString);

		// Sanity checks
		VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount());
		VASSERT_EQ("part-count2", 3, msg->getBody()->getPartAt(1)->getBody()->getPartCount());

		vmime::htmlTextPart htmlPart;
		htmlPart.parse(msg, msg->getBody()->getPartAt(1),
			msg->getBody()->getPartAt(1)->getBody()->getPartAt(1));

		// Two embedded objects should be found.
		// BUGFIX: "CID:" prefix is not case-sensitive.
		VASSERT_EQ("count", 2, htmlPart.getObjectCount());

		// Ensure the right objects have been found.
		VASSERT_EQ("has-obj1", true, htmlPart.hasObject("image1@test"));
		VASSERT_EQ("has-obj2", true, htmlPart.hasObject("image2@test"));

		// hasObject() should also work with prefixes
		VASSERT_EQ("has-obj1-pre", true, htmlPart.hasObject("CID:image1@test"));
		VASSERT_EQ("has-obj2-pre", true, htmlPart.hasObject("cid:image2@test"));

		// Check data in objects
		vmime::ref <const vmime::htmlTextPart::embeddedObject> obj;

		obj = htmlPart.findObject("image1@test");

		VASSERT_EQ("ref-type1", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_ID, obj->getReferenceType());
		VASSERT_EQ("id-obj1", "image1@test", obj->getId());
		VASSERT_EQ("data-obj1", "Image1", extractContent(obj->getData()));
		VASSERT_EQ("type-obj1", "image/png", obj->getType().generate());

		obj = htmlPart.findObject("image2@test");

		VASSERT_EQ("ref-type2", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_ID, obj->getReferenceType());
		VASSERT_EQ("id-obj2", "image2@test", obj->getId());
		VASSERT_EQ("data-obj2", "Image2", extractContent(obj->getData()));
		VASSERT_EQ("type-obj2", "image/jpeg", obj->getType().generate());
	}

	/** Test parsing of embedded objects by location.
	  */
	void testParseEmbeddedObjectsLocation()
	{
		const vmime::string msgString = ""
"MIME-Version: 1.0\r\n"
"Content-Type: multipart/alternative; boundary=\"LEVEL1\"\r\n"
"\r\n"
"--LEVEL1\r\n"
"Content-Type: text/plain; charset=\"x-ch1\"\r\n"
"\r\n"
"Plain text part\r\n"
"--LEVEL1\r\n"
"Content-Type: multipart/related; boundary=\"LEVEL2\"\r\n"
"\r\n"
"--LEVEL2\r\n"
"Content-Type: image/png; name=\"image1.png\"\r\n"
"Content-Location: http://www.vmime.org/test/image1.png\r\n"
"Content-Disposition: inline; filename=\"image1.png\"\r\n"
"Content-Id: <image1@test>\r\n"
"\r\n"
"Image1\r\n"
"--LEVEL2\r\n"
"Content-Type: text/html; charset=\"x-ch2\"\r\n"
"\r\n"
"<img src=\"http://www.vmime.org/test/image1.png\"/>\r\n"
"--LEVEL2--\r\n"
"\r\n"
"--LEVEL1--\r\n"
"";

		vmime::ref <vmime::message> msg = vmime::create <vmime::message>();
		msg->parse(msgString);

		// Sanity checks
		VASSERT_EQ("part-count1", 2, msg->getBody()->getPartCount());
		VASSERT_EQ("part-count2", 2, msg->getBody()->getPartAt(1)->getBody()->getPartCount());

		vmime::htmlTextPart htmlPart;
		htmlPart.parse(msg, msg->getBody()->getPartAt(1),
			msg->getBody()->getPartAt(1)->getBody()->getPartAt(1));

		// Only one embedded object
		VASSERT_EQ("count", 1, htmlPart.getObjectCount());

		// Should work only with Content-Location as the Content-Id is
		// not referenced in the HTML contents
		VASSERT_EQ("has-obj-loc", true, htmlPart.hasObject("http://www.vmime.org/test/image1.png"));
		VASSERT_EQ("has-obj-cid", false, htmlPart.hasObject("image1@test"));

		// Check data
		vmime::ref <const vmime::htmlTextPart::embeddedObject> obj;

		obj = htmlPart.findObject("http://www.vmime.org/test/image1.png");

		VASSERT_EQ("ref-type", vmime::htmlTextPart::embeddedObject::REFERENCED_BY_LOCATION, obj->getReferenceType());
		VASSERT_EQ("id-obj", "http://www.vmime.org/test/image1.png", obj->getId());
		VASSERT_EQ("data-obj", "Image1", extractContent(obj->getData()));
		VASSERT_EQ("type-obj", "image/png", obj->getType().generate());
	}

	// TODO: test generation of text parts

VMIME_TEST_SUITE_END

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions