import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class HtmlJavaMail {
public static void main(String[] args) {
HtmlJavaMail mjm = new HtmlJavaMail();
//mjm.sendMail();
}
public HtmlJavaMail() {}
public void sendMail(String to, String subject, String msgKey, HashMap hmMsgValues) {
Properties props = new Properties();
props.put("mail.smtp.host", MailSettings.smtpHost);
props.put("mail.debug","true");
Session session = Session.getDefaultInstance(props, new ForcedAuthenticator());
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(MailSettings.fromAddress,MailSettings.fromName));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
MimeMultipart multipart = new MimeMultipart();
BodyPart msgBodyPart = new MimeBodyPart();
String body = buildMessage(MailSettings.getMessageBody(msgKey), hmMsgValues);
msgBodyPart.setContent(body,"text/html");
/*
BodyPart embedImage=new MimeBodyPart();
DataSource ds=new URLDataSource(new URL(MailSettings.inlineImage));
embedImage.setDataHandler(new DataHandler(ds));
embedImage.setHeader("Content-ID","
*/
multipart.addBodyPart(msgBodyPart);
//multipart.addBodyPart(embedImage);
message.setContent(multipart);
message.setSentDate(new Date());
Transport.send(message);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} /* catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
class ForcedAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(MailSettings.smtpUsername,
MailSettings.smtpPassword);
}
}
public String buildMessage(String body, HashMap hmMsgValues)
{
String strTempBody = body;
try
{
String strKey = null;
String strValue = null;
Iterator itrMsgValues = hmMsgValues.keySet().iterator();
while(itrMsgValues.hasNext())
{
strKey = itrMsgValues.next().toString();
strValue = hmMsgValues.get(strKey).toString();
strKey = "<<" + strKey + ">>";
while(strTempBody.indexOf(strKey) != -1)
{
strTempBody = strTempBody.substring(0, strTempBody.indexOf(strKey)) + strValue + strTempBody.substring(strTempBody.indexOf(strKey) + strKey.length());
}
}
}
catch(Exception exp)
{
exp.printStackTrace();
}
return strTempBody;
}
}
import java.util.HashMap;
public class MailSettings {
// Set smtpHost to the hostname for your mail server
public static String smtpHost="ipaddress";
// For SMTP authentication, set the next two lines to your username and password for the mail server
public static String smtpUsername="username";
public static String smtpPassword="password";
// Set toAddress to the address you want the email to go to
public static String toAddress="toaddress";
public static String[] ccAddresses=new String[] { "ccaddress1", "ccaddress2" };
public static String fromAddress="fromaddress";
public static String fromName="Mail Example App";
public static String messageSubject="This is a test mail";
public static String messageBody1="This is a test mail sent by a mail example";
public static String inlineImage="file:uk-builder-com.gif";
public static HashMap hashMap = new HashMap();
static {
hashMap.put("FORGOT_PASSWORD",
""<html>"" +
""<body>"" +
"Your User Name is "<"<USERNAME>">"" + "\n" +
"Password is "<"<PASSWORD>">"" + "\n" +
""</body>"" +
""</html>"");
}
public static String getMessageBody(String msgKey)
{
return hashMap.get(msgKey).toString();
}
}
No comments:
Post a Comment