All pastes #807756 Raw Edit

Tyler Smalley

public text v1 · immutable
·published 2007-12-06 23:43 UTC
rendered paste body
<cfscript>
	sMailServer.host = "localhost";
	sMailServer.username = "";
	sMailServer.password = "";
	
	// set javamail properties
	oProps = createObject("java", "java.util.Properties").init();
	oProps.put("javax.mail.smtp.host", sMailServer);
	
	// get static recipient types
	oRecipientType = createObject("java", "javax.mail.Message$RecipientType");
	
	// create the session for the smtp server
	oMailSession = createObject("java", "javax.mail.Session").getInstance(oProps);
	
	// create a new MIME message
	oMimeMessage = createObject("java", "javax.mail.internet.MimeMessage").init(oMailSession);
	
	// create the to and from e-mail addresses
	oAddressFrom = createObject("java", "javax.mail.internet.InternetAddress").init(variables.getFrom());
	oAddressTo = createObject("Java", "javax.mail.internet.InternetAddress").init(variables.getTo());
	
	// build message
	// set who the message is from
	oMimeMessage.setFrom(oAddressFrom);
	
	// add a recipient
	oMimeMessage.addRecipient(oRecipientType.TO, oAddressTo);
	
	// set the subject of the message
	oMimeMessage.setSubject(variables.getSubject());
	
	// create multipart message: only needed if you're including both plain/text and html
	oMimeMultipart = createObject("java", "javax.mail.internet.MimeMultipart").init();

	// specifies that the message contains both inline text and html, this is so that
	// images given a cid will show up when rendered by the e-mail client
	oMimeMultipart.setSubType("related");

	// make sure were sending the correct formats
	if (variables.getFormat() EQ "txt") {
		// create plain text multipart
		oBody = createObject("java", "javax.mail.internet.MimeBodyPart").init();
	
		// create the plain/text for the message
		oBody.setText(variables.getBody());
	
		// add the body part to the message
		oMimeMultipart.addBodyPart(oBody);
	} else {
		// create an html message
		oBody = createObject("java", "javax.mail.internet.MimeBodyPart").init();

		// add the html content (the setText() method shortcut/only works for "plain/text")
		oBody.setContent(variables.getBody(), "text/html");

		// add the body part to the message
		oMimeMultipart.addBodyPart(oBody);
	}				
	
	// place all the multi-part sections into the body of the message
	oMimeMessage.setContent(oMimeMultipart);

	// in this section we'll build the message into a string. you could dump
	// the string to a file in most SMTP server's queue file for delivery
	// this is exactly what would be pass to the SMTP server
	// create a bytearray for output
	outStream = createObject("java", "java.io.ByteArrayOutputStream");

	// create a budder for the output stream
	outStream.write(repeatString(" ", 1024).getBytes());

	// save the contents of the message to the output stream
	oMimeMessage.writeTo(outStream);

	// save the contents of the message to the sMailMsg variable
	sMailMsg = outStream.toString();

	// reset the output stream (for stability)
	outStream.reset();

	// close the output stream
	outStream.close();

	// create a transport to actually send the message via SMTP
	oTransport = oMailSession.getTransport("smtp");

	// connect to the SMTP server using the parameters supplied;
	oTransport.connect(sMailServer.host, sMailServer.username, sMailServer.password);

	// send the message to all recipients
	oTransport.sendMessage(oMimeMessage, oMimeMessage.getAllRecipients());

	// close the transport
	oTransport.close();
	
	// Get the message-id from the header.
	messageID = REFind('Message-ID: <[^<]*>', sMailMsg, 1, true);
	messageID.string = REReplace(Mid(sMailMsg, messageID.pos[1], messageID.len[1]), 'Message-ID: <([^<]*)>', '\1');
</cfscript>