Tuesday, March 25, 2008

Sending Mails through Java With Attachment

Properties properties=System.getProperties();
properties.put("mail.smtp.host",EmailNotificationSettings.smtpHost);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "25");

// Creating instance for the Authenicator class for authentication & Get session
Authenticator auth = new PopupAuthenticator();
mailSession = Session.getDefaultInstance (properties, auth);

/**
* try catch block uses to send the mail.
* contains subject,fromAddress,toAddress,Message
*/
try {
mLog.debug(mClassName+"::"+methodName+":: try{}");

// getting the host connection from the EmailNotificationSettings Class
MimeMessage message = new MimeMessage(mailSession);
Multipart multipart = new MimeMultipart();

message.setSubject(emailsettings.messageSubject);
Address fromAddress = new InternetAddress(emailsettings.fromAddress);
message.setFrom(fromAddress);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));
for(int i=0;i message.addRecipient(Message.RecipientType.CC, new InternetAddress(EmailNotificationSettings.ccAddresses[i]));
}
for(int i=0;i message.addRecipient(Message.RecipientType.BCC, new InternetAddress(EmailNotificationSettings.bccAddresses[i]));
}

//
//Attach the file to the message
//
MimeBodyPart multipart_attachment = new MimeBodyPart();
DataSource ds = new FileDataSource(emailsettings.fileName);
multipart_attachment.setDataHandler(new DataHandler(ds));
multipart_attachment.setFileName(emailsettings.fileName);
multipart_attachment.setDisposition(Part.ATTACHMENT);
multipart.addBodyPart(multipart_attachment);

message.setSentDate(emailsettings.createdDate);

//
// Add MessageBody to message
//
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setText("Hello.. \n \t" + emailsettings.messageBody1);
multipart.addBodyPart(bodyPart);
message.setContent(multipart);

Transport.send(message);

mLog.debug("Mail sent Successfully");
sendMail = true;

} catch (AddressException exception) {
exception.printStackTrace();
throw new EJBException("Exception in EmailNotificationBean::::send() ");
}catch(AuthenticationFailedException authenticationFailedException){
mLog.error("Exception in"+mClassName+"::"+methodName+"::"+ authenticationFailedException);
}catch (MessagingException exception) {
exception.printStackTrace();
mLog.error("Exception in"+mClassName+"::"+methodName+"::"+ exception.getMessage());
throw new EJBException("Exception in EmailNotificationBean::::send()");
} catch (Exception exception) {
exception.printStackTrace();
mLog.error("Exception in"+mClassName+"::"+methodName+"::"+exception.getMessage());
throw new EJBException("Exception in EmailNotificationBean::::send()");
}

Wednesday, February 27, 2008

For Email Notification/Services in Java Using javax.mail

  



Arm.java: Manipulate a jointed arm in 3D.

Doorknob.java - listen for trojans, worms and back doors trying to connect

EachInDirectory.java - calls a callback for each file in a directory.

FTPupload.java - upload a file to an FTP server via java.net.URLConnection

Globe.java: Globe in Java. Draws a globe and rotates it in three dimensions.

Globe2.java: A fancier version, with perspective, written in more object-oriented style.

LineFieldsParser.java - parses each line in a file into fields.

MailNotifier.java - sends an email notification via JavaMail.

MboxParser.java - parses a file of email messages.

Multiple choice test applet in client-side Java, with source

Arm.java: Manipulate a jointed arm in 3D.

proxylogserverer.java: a TCP proxy with logging capability

SmtpListener.java - implements SMTP protocol, server side

SmtpSender.java - implements SMTP protocol, client side

SmtpSendAtt.java - sends an SMTP email message with text attachments

SQLLogger.java - maintains an event log as a mySQL table.

SQLMapStringToNumber.java - maintains a one-to-mapping of strings to integers in a mySQL database table.

TextToJpeg.java - creates a JPEG image of a text string.

TryMany.java - attempt to connect to a list of hosts on a given port.
Useful for scanning for usable IRC servers.

TryManyPooled.java - this version uses a thread pool.



Java 1.4 class reference

com.sun.image.codec.jpeg

J2EE 1.4 class reference

James class reference

java.nio samples

Contents of all JARs and SARs on my system




  


Wednesday, February 6, 2008

Getting started with Struts 2 and Tiles 2


I suppose this would be a easy work for 2, 3 hours but I spend some days in it, there are little information yet on internet, so I'll try to correct this.
What I've done for you - a simple war file, it is called struts2+tiles2-blank-2.0.6.zip, so you can use it as template, base to start. It's a some kind of HOW-TO, first steps or something when you decide to build applications on Struts 2 framework with the new Tiles 2.
Versions tath I use are struts 2.0.6, tiles 2.0.6.
In zip file you have mine project with lib included, and some jsp. What it can do? You have a one page with listing fo imputed news in application, and one page for editing them. They use actions & form beans.
I disabled some jar files beceuse I will not use their funcionality - JSF, String and Plexus.
Here is a list of more critical moments for me, where I spend a lot of time:
web.xml
struts.xml
tiles.xml
struts.properties
stdLayout.jsp
listNews.jsp
editNews.jsp
NewsAction.java
News.java

One of the tricks was with tiles TLD: copy tiles-core.tld from tiles-core.jar (open it with WinRAR) and extract the TLD file to WEB-INF directory, and add in web.xml
Other trick is to remove some jars: spring plugin, jsf plugin and plexus plugin. In version struts 2.0.6 they do problems.

And a little hint, Struts 2 is based on WebWorks framework, you can search with this keyword for more results in google.

If you have questions, post comments here. I'll try to update this howto with your experience.

P.S. for UTF-8 charset managment:  In version 2.0.6 there are bugs in tiles, so setting charset / encoding of page to UTF-8 is problematic. If the lastest GA release is 2.0.6 yet, please update to lastest snapshot of struts2-core, struts2-config-browser-plugin, struts2-tiles-plugin, tiles-core, tiles-api from http://people.apache.org/builds/jakarta-struts/nightlies/2.0.x/ and http://people.apache.org/maven-snapshot-repository/org/apache/struts/tiles/tiles-core/2.0-488434-SNAPSHOT/ 
This bug is reported in https://issues.apache.org/struts/browse/WW-1724;jsessionid=8E3A7086E59002AC8802744730F17487?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel