email

 

That is for sending Email using “mail.smtp.auth”

public static String sendEmail(final String username, final String password, String replyto, String port, String host, String from, String to, String messagesubject, String messageboday, String attactmentpath) {

//   final String username = “zakhter@truemeridian.com”;
//  final String password = “password”;

Properties props = new Properties();
props.put(“mail.smtp.auth”, “true”);
//props.put(“mail.smtp.starttls.enable”, “true”);
props.put(“mail.smtp.host”, host);//”smtp.truemeridian.com”);
props.put(“mail.smtp.port”, port);
props.setProperty(“mail.smtp.ssl.trust”, host);

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));//”zakhter@truemeridian.com”));
message.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(to));//”zeeshanakhter2009@gmail.com”));

InternetAddress replyAddresses[] = new InternetAddress[1];
replyAddresses[0] = new InternetAddress(replyto);
message.setReplyTo(replyAddresses);
message.setSubject(messagesubject);
message.setText(messageboday);
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(messageboday);

// Create a multipar message
Multipart multipart = new MimeMultipart();

// Set text message part
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();

DataSource source = new FileDataSource(attactmentpath);
messageBodyPart.setDataHandler(new DataHandler(source));
attactmentpath = attactmentpath.replace(“\\”, “/”);
String[] fileName = attactmentpath.split(“/”);
System.out.println(“\n\nPath3=” + attactmentpath);
// for(int i=0;i
// {
//    attactmentpath=fileName[i];
// }
attactmentpath = fileName[fileName.length – 1];
messageBodyPart.setFileName(attactmentpath);
multipart.addBodyPart(messageBodyPart);

// Send the complete message parts
message.setContent(multipart);
message.setContent(multipart);

Transport.send(message);

System.out.println(“Done”);

return “Sent”;
} catch (Exception e) {
e.printStackTrace();
return “Fail”;
}
}

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s