public class EmailSenderServiceGmailSender extends javax.mail.Authenticator {
privateString finalmailhost Properties= properties"smtp.gmail.com";
private =final newString Properties();
user;
private final String password;
private final Session session;
privatestatic void{
init Security.addProvider(new JSSEProvider());
}
public GmailSender(String user, String password) {
this.user = user;
propertiesthis.putpassword = password;
Properties props = new Properties();
props.setProperty("mail.smtptransport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.gmailsmtp.com"auth", "true");
propertiesprops.put("mail.smtp.starttls.enable"port", "true""465");
propertiesprops.put("mail.smtp.socketFactory.port",25 "465");
propertiesprops.put("mail.smtp.mailsocketFactory.sender"class","emisor@gmail
"javax.com"net.ssl.SSLSocketFactory");
propertiesprops.put("mail.smtp.user"socketFactory.fallback", "usuario""false");
propertiesprops.putsetProperty("mail.smtp.auth"quitwait", "true""false");
session = Session.getDefaultInstance(propertiesprops, this);
}
publicprotected voidPasswordAuthentication sendEmailgetPasswordAuthentication() {
initreturn new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String Texto, String Asunto, String sender, String email) throws Exception {
try{
DataHandler handler = new DataHandler(new ByteArrayDataSource(Texto.getBytes(), "Text/plain"));
MimeMessage message = new MimeMessage(session);
message.setFromsetSender(new InternetAddress((Stringsender)properties);
message.getsetSubject("mail.smtp.mailAsunto);
message.sender"setDataHandler(handler);
if (email.indexOf(',') > 0);
message.addRecipientsetRecipients(Message.RecipientType.TO, new InternetAddress("receptor@gmail.com"parse(email));
else
message.setSubjectsetRecipient("Prueba"Message.RecipientType.TO, new InternetAddress(email));
messageTransport.setTextsend("Texto"message);
}catch(Exception e){
Transport t = session Log.getTransportd("smtp""Error", "sendingMailError"+ e.getMessage());
}
}
public class ByteArrayDataSource implements t.connect((DataSource {
private byte[] data;
private String)properties.get type;
public ByteArrayDataSource("mail.smtp.user")byte[] data, "password"String type) {
super();
tthis.sendMessage(message,data message= data;
this.getAllRecipientstype = type;
}
public ByteArrayDataSource(byte[] data) {
super();
tthis.closedata = data;
}
public void setType(String type); {
this.type = type;
}catch
(MessagingException me public String getContentType() {
if (type == null)
//Aqui se deberia oreturn mostrar"application/octet-stream";
un mensaje de error o en lugar else
return type;
}
//de nopublic hacerInputStream nadagetInputStream() conthrows laIOException excepcion,{
lanzarla para que el modulo return new ByteArrayInputStream(data);
}
public String getName() {
return //superior"ByteArrayDataSource";
la capture y avise}
al usuario con unpublic popup,OutputStream porgetOutputStream() ejemplo.throws IOException {
return;throw new IOException("Not Supported");
}
}
}