Pages

Tuesday, July 21, 2009

Email in GWT

Here is a simple code to send mail in GWT.

Write this code in servletImpl class

String host = "xxx.com.xx";//use your own host
String from = "..........";// email id
String to = ".............";//email id



// Get system properties
Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtp.host", host);

// Get the default Session object.
Session session = Session.getInstance(properties);

More...

// Create a default MimeMessage object.
try{
MimeMessage message = new MimeMessage(session);// error is
shown in this line

// Set the RFC 822 "From" header field using the
// value of the InternetAddress.getLocalAddress method.
message.setFrom(new InternetAddress(from));

// Add the given addresses to the specified recipient type.
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(to));

// Set the "Subject" header field.
message.setSubject("hi..!");

// Sets the given String as this part's content,
// with a MIME type of "text/plain".
message.setText("Hi ......");
Transport.send(message);

}catch(MessagingException e){

e.printStackTrace();
}
// Send message

System.out.println("Message Send.....");

Note: Include activation.jar in library of project

No comments:

Post a Comment