1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
class RunnableSendMails implements Runnable {
private InternetAddress[] from;
private InternetAddress[] to;
private String objMail;
private String username;
private String pw;
public InternetAddress[] getFrom() {
return from;
}
public InternetAddress[] getTo() {return to;}
public String getObjMail() {return objMail; }
public String getUsername() {return username;}
public String getPw() {return pw;}
public RunnableSendMails(InternetAddress[] from, InternetAddress[] to, String objMail, String username, String pw) {
this.from = from;
this.to = to;
this.objMail = objMail;
this.username = username;
this.pw = pw;
}
@Override
public void run() {
try {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "outlook.office365.com");
props.put("mail.smtp.port", "587");
final Session session = Session.getDefaultInstance(props);
final MimeMessage message = new MimeMessage(session);
message.addRecipients(Message.RecipientType.TO, getTo());
message.addFrom(getFrom());
message.setSubject(getObjMail());
message.setText("");
Transport trpt = session.getTransport("smtp");
trpt.connect(getUsername(), getPw());
trpt.sendMessage(message, message.getAllRecipients());
trpt.close();
long sendimgMail = System.currentTimeMillis();
Date d = new Date(sendimgMail);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String s = sdf.format(d);
textViewResult.setText(new StringBuilder().append(R.string.txtResultEnvoi).append(getTo()).append(" - ").append(s).toString());
} catch (AuthenticationFailedException e) {
Log.d("envoi", e.toString());
textViewResult.setText(R.string.txtResultEnvoi);
} catch (SMTPSendFailedException e) {
Log.d("envoi", e.toString());
textViewResult.setText(R.string.txtResultEnvoi);
} catch (MessagingException e) {
Log.d("envoi", e.toString());
textViewResult.setText(R.string.txtResultEnvoi);
} catch (Exception e) {
Log.d("envoi", e.toString());
textViewResult.setText(R.string.txtResultEnvoi);
}
}
} |
Partager