import java.util.Properties;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
private static String exchangeServer = "400exch1";
private static String subject;
private static String fromAddress;
private static String footer;
private static String getSubject() {
return subject;
}
private static void setSubject(String aSubject) {
subject = aSubject;
}
private static String getFromAddress() {
return fromAddress;
}
private static void setFromAddress(String aFromAddress) {
fromAddress = aFromAddress;
}
private static String getFooter() {
return footer;
}
private static void setFooter(String aFooter) {
footer = aFooter;
}
public SendMail(String subj, String fromAdd, String footerMsg) {
subject = subj;
fromAddress = fromAdd;
footer = footerMsg;
}
private static void send(ArrayList to, String from, String host, String message) {
if (to == null || from == null || message == null || host == null) {
return;
}
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getDefaultInstance(props, null);
if (session == null) {
return;
}
MimeMessage mm = new MimeMessage(session);
try {
Address addresses[] = new Address[to.size()];
for (int i = 0; i <> 1) {
for (int j = 0; j < addresses.length - 1; j++) { mm.addRecipients(javax.mail.Message.RecipientType.CC, addresses[j + 1].toString()); } } mm.setSubject(getSubject()); mm.setContent((new StringBuilder()).append(message).append( "
\n\n\n** Do not reply to this message ***
").append(getFooter()).toString(),
"text/html");
Transport.send(mm);
} catch (AddressException ex) {
ex.printStackTrace();
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
public void sendEmail(String message, ArrayList emailList) {
if (message == null) {
return;
} else {
send(emailList, getFromAddress(), exchangeServer, message);
return;
}
}
}
Constructor Detail |
SendMail
public SendMail(java.lang.String subj,
java.lang.String fromAdd,
java.lang.String footerMsg)
Parameters:
subj - Subject of the email (String)
fromAdd - From Address (String)
footerMsg - Set Custom footer message - optional (String)
Method Detail |
sendAlert
public void sendAlert(java.lang.String message,
java.util.ArrayList emailList)
Parameters:
message - Message body to be sent in the email- Text or HTML
The Html Body could contain image with a valid link
emailList - Array list of the email addresses the email to be sent.
For example to send to email -
To send to Email Groups just add the email group to the array list
For example to send email to CIM Group add "#CIM" to the array list.
Sample Code
SendMail sm = new SendMail("Test Email", "Anonymous", "Please do not reply to this email");
ArrayList<String> addressList = new ArrayList<String>();
addressList.add(“#CIM”);
sm.sendAlert("This is a Test Message", addressList);
how i have to replace props.put("mail.smtp.host", host); to my gmail account
ReplyDelete