asmuts 02/01/14 22:46:32
Added: src/java/org/apache/stratum/jcs/utils/email
SendJavaMailAttachment.java SendMail.java
Log:
simple email stuff, mainly used by the log, to be removed later
Revision Changes Path
1.1
jakarta-turbine-stratum/src/java/org/apache/stratum/jcs/utils/email/SendJavaMailAttachment.java
Index: SendJavaMailAttachment.java
===================================================================
package org.apache.stratum.jcs.utils.email;
import javax.activation.*;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import org.apache.stratum.jcs.utils.log.*;
/////////////////////////////////////////////
public class SendJavaMailAttachment {
private String smtpHost = "";//PortalEnv.emailHost;
Logger log;
boolean debug = true;
public SendJavaMailAttachment(){
log = LoggerManager.getLogger( this );
}
////////////////////////////////////////////////
public String execute( Hashtable params ) {
String from = (String)params.get("from");
String to = (String)params.get("to");
String cc = (String)params.get("cc");
String bcc = (String)params.get("bcc");
String subject = (String)params.get("subject");
String text = (String)params.get("text");
String fullName = (String)params.get("fullName");
if ( debug ) {
log.logIt("fullName = " + fullName );
}
String status = "";
try {
// Create the JavaMail session
java.util.Properties properties = System.getProperties();
properties.put("mail.smtp.host", smtpHost);
Session session =
Session.getInstance(properties, null);
// Construct the message
MimeMessage message = new MimeMessage(session);
// Set the from address
Address fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
// Parse and set the recipient addresses
Address[] toAddresses = InternetAddress.parse(to);
message.setRecipients(Message.RecipientType.TO,toAddresses);
Address[] ccAddresses = InternetAddress.parse(cc);
message.setRecipients(Message.RecipientType.CC,ccAddresses);
Address[] bccAddresses = InternetAddress.parse(bcc);
message.setRecipients(Message.RecipientType.BCC,bccAddresses);
// Set the subject and text
message.setSubject(subject);
message.setText(text);
if ( fullName != null ) {
// Set the attachment file
MimeMultipart mmp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
//String filename =
(String)params.get("fullName");
FileDataSource fds = new FileDataSource(
fullName );
DataHandler dh = new DataHandler(fds);
mbp.setDataHandler(dh);
mbp.setFileName(fds.getName());
mmp.addBodyPart(mbp);
message.setContent(mmp);
}
Transport.send(message);
status = "Your message was sent.";
} catch (AddressException e) {
status = "There was an error parsing the addresses.";
log.logIt("Exception e = " +
e.toString() );
} catch (SendFailedException e) {
status = "There was an error sending the message.";
log.logIt("Exception e = " +
e.toString() );
} catch (MessagingException e) {
status = "There was an unexpected error.";
log.logIt("Exception e = " + e.toString() );
}
catch(Exception e){
log.logIt("Exception e = " + e.toString() );
}
finally{
return status;
}
}
}
1.1
jakarta-turbine-stratum/src/java/org/apache/stratum/jcs/utils/email/SendMail.java
Index: SendMail.java
===================================================================
package org.apache.stratum.jcs.utils.email;
import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import org.apache.stratum.jcs.utils.log.*;
////////////////////////////////////////////////////////////////////////
public class SendMail {
///////////////////// Constants //////////////////
private static final int SMTP_PORT = 25;
private static final char SMTP_ERROR_CODE1 = '4';
private static final char SMTP_ERROR_CODE2 = '5';
private static final String server = "";
boolean debug = false;//
String mailtext = "" ;
String maildata = "" ;
Vector sessionTrace = new Vector(20);
StringBuffer strBuf = new StringBuffer();
private static final String host = "";
private static final String domain = "";
Logger log = LoggerManager.getLogger( this );
/////////////////////////////////////////////////////////////////////////////
/*
public static void main( String args[] ) {
SendMail send = new SendMail();
send.execute( "[EMAIL PROTECTED]", "[EMAIL PROTECTED]", "test subject", "test
message" );
}
*/
////////////////////////////////////////////////////////////////////////////////////////
public void execute( String sender, String recipientA, String subject, String
message ) {
String recipients = recipientA;
Message( sender, recipients, subject, message );
}
////////////////////////////// SEND ////////////////////////////
public void Message( String senderP, String recipients, String subject, String
message ) {
String recipientsP = recipients;
String subjectP = subject;
if ( debug ) {
log.logIt("sender = " + senderP + "<br><br>");
log.logIt("recipients = " + recipientsP + "");
}
String mailtextP = message ;
mailtextP += "\n";
// Try to send the mail, then the response, catching
// any IO exception.
// Send the mail.
maildata =
"Date: " + (new java.util.Date()).toString() + "\r\n" +
"From: " + senderP + "\r\n" +
"To: " + recipientsP + "\r\n" +
"Subject: " + subjectP + "\r\n" +
"\r\n" +
mailtextP + "\r\n";
try {
sendMail( host, domain, "[EMAIL PROTECTED]", recipientsP, subjectP,
maildata, sessionTrace );
} catch ( Exception e) {
log.logIt( "Exception thrown when running sendMail = " + e.toString() );
}
log.logIt( "The mail was queued for delivery.\n" + sessionTrace );
} ////////////////// end sendMessage
///////////////////////////////////////////////////////////////////////
//////////////////////// sendMail() ////////////////////////////
protected void sendMail(
String host,
String domain,
String senderA,
String recipients,
String subject,
String maildata,
Vector sessionTrace) throws IOException {
if ( debug ) {
log.logIt("In sendMail: host = " + host + "; domain = " + domain + "; sender
= " + senderA + "<br><br>");
}
Socket mailSocket;
BufferedReader socketIn;
DataOutputStream socketOut;
String address;
StringTokenizer tokenizer;
// Open the connection to the SMTP server, then get references to the
// input and output streams.
mailSocket = new Socket(host, SMTP_PORT);
socketIn = new BufferedReader(
new InputStreamReader(mailSocket.getInputStream()) );
socketOut = new DataOutputStream(mailSocket.getOutputStream());
// Get the initial reply from the server.
readReply(socketIn, sessionTrace);
if ( debug ) {
System.out.println("After initial socket reply from server<br><br>");
}
// Greet the server.
sendCommand(socketOut, "HELO " + domain, sessionTrace);
readReply(socketIn, sessionTrace);
if ( debug ) {
log.logIt("After greeting the server<br><br>");
}
// Send the sender's address.
sendCommand(socketOut, "MAIL FROM: " + senderA, sessionTrace);
readReply(socketIn, sessionTrace);
if ( debug ) {
log.logIt("After sending the from address: sender = " + senderA + "<br><br>");
}
// Send the list of recipients.
tokenizer = new StringTokenizer(recipients, ",");
while (tokenizer.hasMoreElements()) {
String token = tokenizer.nextToken();
if ( debug ) {
log.logIt("token = " + token + "<br><br>");
}
sendCommand(socketOut, "RCPT TO: " + token, sessionTrace);
readReply(socketIn, sessionTrace);
}
if ( debug ) {
log.logIt("After sending the to addresses: recipients = " + recipients +
"<br><br>");
}
// Start the data section.
sendCommand(socketOut, "DATA", sessionTrace);
readReply(socketIn, sessionTrace);
// Send the mail message.
sendCommand(socketOut, maildata + ".", sessionTrace);
readReply(socketIn, sessionTrace);
// End the session.
sendCommand(socketOut, "QUIT", sessionTrace);
readReply(socketIn, sessionTrace);
if ( debug ) {
log.logIt("After ending the session<br><br>");
}
}
/////////////////////////// sendCommand() //////////////////
private void sendCommand(DataOutputStream out, String command, Vector
sessionTrace)
throws IOException {
out.writeBytes(command + "\r\n");
sessionTrace.addElement(command);
log.logIt(command);
}
/////////////////////////// readReply() //////////////////////////
private void readReply(BufferedReader reader, Vector sessionTrace)
throws IOException {
String reply;
char statusCode;
reply = reader.readLine();
statusCode = reply.charAt(0);
sessionTrace.addElement(reply);
log.logIt(reply);
if ( (statusCode == SMTP_ERROR_CODE1) |
(statusCode == SMTP_ERROR_CODE2) ) {
throw (new IOException("SMTP: " + reply));
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>