package project;
import javax.mail.Message.RecipientType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codemonkey.simplejavamail.Email;
import org.codemonkey.simplejavamail.Mailer;
import org.codemonkey.simplejavamail.TransportStrategy;
import org.springframework.stereotype.Service;
import java.util.*l;
@Service
public class MailsService {
protected final Log log = LogFactory.getLog(getClass());
public String sendEmail(String fromAddress, String fromName,
String toAddress, String toName,
String subject, String body) {
String status = "";
try {
Email email = new Email();
String emailHost = AppUtil.getProperty("EMAIL_HOST");
String emailPort = AppUtil.getProperty("EMAIL_PORT");
email.setFromAddress(fromName, fromAddress);
email.setReplyToAddress(toName, toAddress);
email.addRecipient(toName, toAddress, RecipientType.TO);
email.setSubject(subject);
// email.setTextHTML(body);
email.setTextHTML("<html> <body>" + body + "</body> </html>");
String mailboxUserName = AppUtil.getProperty("EMAIL_MAILBOX_LOGIN");
String mailboxPassword = AppUtil.getProperty("EMAIL_MAILBOX_PASSWORD");
//String transportStrategy = AppUtil.getProperty("TRANSPORT_STRATEGY");
Mailer mailer = new Mailer(emailHost, Integer.parseInt(emailPort),
mailboxUserName, mailboxPassword,TransportStrategy.SMTP_SSL);
log.info("Email sent Successfully");
mailer.setDebug(true);
boolean isValid = mailer.validate(email);
mailer.sendMail(email);
status = "SENT";
} catch (Exception ex) {
ex.printStackTrace();
}
return status;
}
}
-------------------------------------------
Sending multiple mails :-
public String sendEmail(String fromAddress, String fromName,
String toAddress, String toName,
String subject, String body) {
String status = "";
try {
Email email = new Email();
String emailHost = AppUtil.getProperty("EMAIL_HOST");
String emailPort = AppUtil.getProperty("EMAIL_PORT");
email.setFromAddress(fromName, fromAddress);
//email.setReplyToAddress(toName, toAddress);
//email.addRecipient(toName, toAddress, RecipientType.TO);
email.setSubject(subject);
String[] toAdd = toAddress.split(",");
// Add To address
for (int i = 0; i < toAdd.length; i++) {
email.addRecipient(toAdd[i].toString(),
toAdd[i].toString(), RecipientType.TO);
// email.setTextHTML(body);
email.setTextHTML("<html> <body>" + body + "</body> </html>");
String mailboxUserName = AppUtil.getProperty("EMAIL_MAILBOX_LOGIN");
String mailboxPassword = AppUtil.getProperty("EMAIL_MAILBOX_PASSWORD");
Mailer mailer = new Mailer(emailHost, Integer.parseInt(emailPort),
mailboxUserName, mailboxPassword,TransportStrategy.SMTP_SSL);
mailer.setDebug(true);
boolean isValid = mailer.validate(email);
mailer.sendMail(email);
}
status = "SENT";
log.info("Email sent Successfully");
} catch (Exception ex) {
ex.printStackTrace();
}
return status;
}
-----------------------------------------
In properties file:
#Email Propertices
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=465 //(ssl)
#TRANSPORT_STRATEGY=TransportStrategy.SMTP_SSL
EMAIL_MAILBOX_LOGIN=phanikumar.r@gmail.com
EMAIL_MAILBOX_PASSWORD=xxxxxxxx
We have to add two jar files for running this program:
<!-- Mail dependencies -->
<dependency>
<groupId>org.codemonkey</groupId>
<artifactId>simple-java-mail</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.4.4</version>
</dependency>
Thanks for visiting...........
No comments:
Post a Comment