public class MakeSend {
String userName;
String passWord;
Logger logger = Logger.getLogger(MakeSend.class);
public MakeSend(String userName,String passWord){
this.userName = userName;
this.passWord = passWord;
}
public String doSend(String smtp, StringBuffer msgBody,
String toEmail, String subject, String mimetype){
Session mailSession;
try{
Properties mailProps = new Properties();
mailProps.setProperty("mail.transport.protocol", "smtp");
mailProps.setProperty("mail.smtp.host", smtp);
mailProps.setProperty("mail.smtp.auth", "true");
Authenticator mailAuth = new SMTPAuthenticator();
mailSession = Session.getInstance(mailProps,mailAuth);
Transport transport = mailSession.getTransport();
MimeMessage msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(userName));
msg.setSubject(subject,"UTF-8");
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(toEmail));
msg.setDataHandler(new DataHandler(
new javax.mail.util.ByteArrayDataSource(msgBody.
toString(), mimetype)));
transport.connect();
transport.sendMessage(msg, msg.getRecipients(Message.
RecipientType.TO));
transport.close();
} catch (MessagingException mex) {
logger.error("Email send failed, exception on:
[" + toEmail + "] -" + mex);
return "ERROR";
}catch (IOException ioex){
logger.error("Email send failed, exception on:
[" + toEmail + "] -" + ioex);
return "ERROR";
}
finally {
mailSession = null;
}
return "OK";
}
private class SMTPAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userName,passWord);
}
}
}
No comments:
Post a Comment