Email Via JCN
Good Morning,
Here is the way u can send mail via JCN ....
For This you need two jars to add on the classpath ....
1. javax.mail
2. javax.activation
How to
add the jars in the Classpath ........
Click on the java perspective ....................
........... Right Click on Application where you need to
attach the jars
........................ Click
on Build Path Followed by Configure Build Path
.........................................Click on the Tab Libraries.
On the Right
side click on Add
External Jars and than Click ok ...ok.
The Massage You put in the Email Queue will be in xml Form
For Example:-
<Email_Configuration>
<To>ashi2uashi@gmail.com,xyz@gmail.com,abc@gmail.com</To>
<Subject>Hi Little Master</Subject>
<Body>Hi Now Its Working Fine</Body>
</Email_Configuration>
Following are the code .............................................................................................................................
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.MbElement;
import com.ibm.broker.plugin.MbException;
import com.ibm.broker.plugin.MbMessage;
import com.ibm.broker.plugin.MbMessageAssembly;
import com.ibm.broker.plugin.MbOutputTerminal;
import com.ibm.broker.plugin.MbUserException;
public class Sending_Emails_JavaCompute extends MbJavaComputeNode {
Integer port =
(Integer)getUserDefinedAttribute("Port");
String Server =
(String)getUserDefinedAttribute("SmtpSever");
String EnvTypte
= (String)getUserDefinedAttribute("ENV");
String password
= (String)getUserDefinedAttribute("PASSWORD");
String emailid =
(String)getUserDefinedAttribute("EMAILID");
public void
evaluate(MbMessageAssembly inAssembly) throws MbException {
MbOutputTerminal
out = getOutputTerminal("out");
// MbOutputTerminal alt =
getOutputTerminal("alternate");
MbMessage
inMessage = inAssembly.getMessage();
MbMessageAssembly
outAssembly = null;
try
{
MbMessage
outMessage = new MbMessage();
outAssembly
= new MbMessageAssembly(inAssembly, outMessage);
//String
to = inMessage.getRootElement().getLastChild().getFirstChild().getFirstChild().getValueAsString();
Object
obj =
inMessage.getRootElement().evaluateXPath("/Email_Configuration/To");
String
to = obj.toString();
int
s = to.lastIndexOf("value:");
int
en= to.length();
to
= to.substring(s+7,en-3);
//String
to=
inMessage.getRootElement().getLastChild().getFirstElementByPath("/Email_Configuration/To").getDOMNode().getFirstChild().getNodeValue();
String[]
recipientList = to.split(",");
InternetAddress[]
recipientAddress = new InternetAddress[recipientList.length];
int
counter = 0;
for
(String recipient : recipientList) {
recipientAddress[counter] = new
InternetAddress(recipient.trim());
counter++;
}
//String
Subject=inMessage.getRootElement().getLastChild().getFirstChild().getFirstChild().getNextSibling().getValueAsString();
Object
objSubject =
inMessage.getRootElement().evaluateXPath("/Email_Configuration/Subject");
String
Subject = objSubject.toString();
int
sSubject = Subject.lastIndexOf("value:");
int
enSubject= Subject.length();
Subject
= Subject.substring(sSubject+7,enSubject-3);
Subject
= "String"+EnvTypte+": "+Subject;
//String
Subject= inMessage.getRootElement().getLastChild().getFirstElementByPath("/Email_Configuration/Subject").getDOMNode().getFirstChild().getNodeValue();
//String
Body =
inMessage.getRootElement().getLastChild().getLastChild().getLastChild().getValueAsString();
Object
objBody= inMessage.getRootElement().evaluateXPath("/Email_Configuration/Body");
String
Body = objBody.toString();
int
sBody = Body.lastIndexOf("value:");
int
enBody= Body.length();
Body
= Body.substring(sBody+7,enBody-3);
//String
Body= inMessage.getRootElement().getLastChild().getFirstElementByPath("/Email_Configuration/Body").getDOMNode().getFirstChild().getNodeValue();
//Get
the session object
Properties props = new Properties();
props.put("mail.smtp.host",
Server);
props.put("mail.smtp.starttls.enable",
"true");
props.put("mail.smtp.auth",
"true");
props.put("mail.smtp.port",
port);
Session session =
Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new
PasswordAuthentication(emailid,password);//change accordingly
}
});
//compose message
try {
MimeMessage message = new
MimeMessage(session);
message.setFrom(new
InternetAddress(emailid));//change accordingly
message.addRecipients(Message.RecipientType.TO,recipientAddress);
//message.setSubject("Hello");
//
message.setText("Testing.......");
message.setSubject(Subject);
message.setText(Body);
//send message
Transport.send(message);
//System.out.println("message sent
successfully");
} catch (MessagingException e) {throw new
RuntimeException(e);}
finally
{
Object message = null;
}
out.propagate(outAssembly);
}
catch (MbException e) {
//
Re-throw to allow Broker handling of MbException
throw
e;
}
catch (RuntimeException e) {
//
Re-throw to allow Broker handling of RuntimeException
throw
e;
}
catch (Exception e) {
//
Consider replacing Exception with type(s) thrown by user code
//
Example handling ensures all exceptions are re-thrown to be handled in the
flow
throw
new MbUserException(this, "evaluate()", "", "",
e.toString(),
null);
}
//
The following should only be changed
//
if not propagating message to the 'out' terminal
//out.propagate(outAssembly);
finally
{
MbMessage
outMessage = null;
//outMessage.clearMessage();
Object
props = null;
Object
recipientAddress = null;
Object
objBody = null;
}
}
}
|
No comments:
Post a Comment