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
| .setPositiveButton("Envoyer",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int idi)
{
//On récupère l'email de l'expéditeur et le mot de passe
final String emailEx = mailrobot;
final String mdp = passrobot;
final String emailDe = emailD.getText().toString();
final String sujet = sujetT.getText().toString();
final String message = "ccccccc";
/* VERIFICATION a faire si vous le souhaitez pour savoir si tout est bien rempli avant d'essayer
* d'envoyer le mail */
sendMail(emailEx, mdp, emailDe, sujet, message);
}
//Pour bien faire il faudrait exécuter cette fonction dans un thread avec une progress Bar
private void sendMail(String emailEx, String mdp, String emailDe, String sujet, String message)
{
// Création de l'objet sendMail avec le 2e constructeur
Mail sendMail = new Mail(emailEx, mdp);
// Ajout de l'expédideur
sendMail.setFrom(emailEx);
// Création du tableau des destinataires
String[] toArr = {emailDe};
// Ajouts des destinataires
sendMail.setTo(toArr);
sendMail.setSubject(sujet);
sendMail.setBody(message);
// Si on souhaite ajouter une pièce jointe
/*File f = new File(Environment.getExternalStorageDirectory() + "/temp_rapports/"+PDF);
try {
sendMail.addAttachment(f.getAbsolutePath());
} catch (Exception e1) {
e1.printStackTrace();
}
*/
try {
if(sendMail.send())
{
Toast.makeText(context.getApplicationContext(), "Email envoyé avec succès!", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(context.getApplicationContext(), "Email non envoyé!", Toast.LENGTH_LONG).show();
}
}catch(javax.mail.AuthenticationFailedException e) {
Toast.makeText(context.getApplicationContext(), "Mauvais user et/ou mot de passe!", Toast.LENGTH_LONG).show();
}catch(javax.mail.MessagingException e) {
Toast.makeText(context.getApplicationContext(), "Problème de connexion!", Toast.LENGTH_LONG).show();
}
catch(Exception e) {
Toast.makeText(context.getApplicationContext(), "Il y a eu un problème lors de l'envoi du mail!", Toast.LENGTH_LONG).show();
Log.e("SendMailAttachementActivity", "Email non envoyé", e);
}
}
})// END envoyer |
Partager