All pastes #807765 Raw Edit

Tyler Smalley

public text v1 · immutable
·published 2007-12-07 00:00 UTC
rendered paste body
<cfscript>
	sMailServer = StructNew();
	sMailServer.host = "localhost";
	sMailServer.username = "";
	sMailServer.password = "";
	
	sMail = StructNew();
	sMail.format = "html"; // or text
	sMail.to = "test@email.com";
	sMail.from = "test@email.com";
	sMail.subject = "Test From Java";
	sMail.body = "This is a test email";
	
	// 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(sMail.from);
	oAddressTo = createObject("Java", "javax.mail.internet.InternetAddress").init(sMail.to);
	
	// 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(sMail.subject);
	
	// 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 (sMail.format EQ "text") {
		// create plain text multipart
		oBody = createObject("java", "javax.mail.internet.MimeBodyPart").init();
	
		// create the plain/text for the message
		oBody.setText(sMail.body);
	
		// add the body part to the message
		oMimeMultipart.addBodyPart(oBody);
	} else {
		oBody = createObject("java", "javax.mail.internet.MimeBodyPart").init();
		// add the html content (the setText() method shortcut/only works for "plain/text")
		oBody.setContent(sMail.body, "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>