/*sendMail function for single recipient*/
public void sendMail(String from, String to, String subject, String messageBody)
throws MessagingException, AddressException
{
Properties props = System.getProperties();
props.put("hostname", "servername");
Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress addressTo = new InternetAddress(to);
message.setRecipient(javax.mail.Message.RecipientType.TO, addressTo);
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageBody);
message.setContent(messageBody, "text/html");
Transport.send(message);
}
/*sendMail function for multiple recipient*/
public void sendMail(String from, String to[], String subject, String messageBody)
throws MessagingException, AddressException
{
Properties props = System.getProperties();
props.put("hostname", "servername");
Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress addressTo[] = new InternetAddress[to.length];
for(int i = 0; i < to.length; i++)
addressTo[i] = new InternetAddress(to[i]);
message.setRecipients(javax.mail.Message.RecipientType.TO, addressTo);
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageBody);
message.setContent(messageBody, "text/plain");
Transport.send(message);
}
/*sendMail function for single recipient with cc and bcc*/
public void sendMail(String from, String to, String cc, String bcc, String subject, String messageBody)
throws MessagingException, AddressException
{
Properties props = System.getProperties();
props.put("hostname", "servername");
Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress addressTo = new InternetAddress(to);
InternetAddress addressCC = new InternetAddress(cc);
InternetAddress addressBCC = new InternetAddress(bcc);
message.setRecipient(javax.mail.Message.RecipientType.TO, addressTo);
message.addRecipient(javax.mail.Message.RecipientType.CC, addressCC);
message.addRecipient(javax.mail.Message.RecipientType.BCC, addressBCC);
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageBody);
message.setContent(messageBody, "text/html");
Transport.send(message);
}
Thursday, July 23, 2009
java mail function
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment