Home‎ > ‎Server‎ > ‎App Engine‎ > ‎

SendMail


One of the ways I send mail in my App Engine application.

public class SendMail {

 
private static final Logger log = Logger.getLogger(SendMail.class.getName());

 
public SendMail() {
 
}

 
public boolean sendMail(String to, String from, String subject, String html) {
   
Properties props = new Properties();
   
Session session = Session.getDefaultInstance(props, null);
   
boolean b = false;
   
try {
     
Message msg = new MimeMessage(session);
      msg
.setFrom(new InternetAddress(from, from));
      msg
.addRecipient(Message.RecipientType.TO, new InternetAddress(to, to));
      msg
.setSubject(subject);
     
//msg.setText(msgBody);
      msg
.setContent(html, "text/html");
     
Transport.send(msg);
      b
= true;
   
} catch (AddressException e) {
      log
.log(Level.SEVERE, "", e);
   
} catch (MessagingException e) {
      log
.log(Level.SEVERE, "", e);
   
} catch (UnsupportedEncodingException e) {
      log
.log(Level.SEVERE, "", e);
   
}
   
return b;
 
}

 
public boolean sendMail(String[] to, String from, String subject, String html) {
   
Properties props = new Properties();
   
Session session = Session.getDefaultInstance(props, null);
   
boolean b = false;
   
try {
     
Message msg = new MimeMessage(session);
      msg
.setFrom(new InternetAddress(from, from));
     
for (int i=0; i < to.length; i++) {
        msg
.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i], to[i]));
     
}
      msg
.setSubject(subject);
     
//msg.setText(msgBody);
      msg
.setContent(html, "text/html");
     
Transport.send(msg);
      b
= true;
   
} catch (AddressException e) {
      log
.log(Level.SEVERE, "", e);
   
} catch (MessagingException e) {
      log
.log(Level.SEVERE, "", e);
   
} catch (UnsupportedEncodingException e) {
      log
.log(Level.SEVERE, "", e);
   
}
   
return b;
 
}
 
}

Comments