Skip to content

EmailSender.java (exemplo simples) #16

@drykxs

Description

@drykxs

package com.example.mamae;

import android.os.AsyncTask;
import android.util.Log;
import java.util.Properties;
import javax.mail.;
import javax.mail.internet.
;

public class EmailSender extends AsyncTask<Void, Void, Void> {
private final String toEmail;
private final String subject;
private final String body;

public EmailSender(String toEmail, String subject, String body) {
    this.toEmail = toEmail;
    this.subject = subject;
    this.body = body;
}

@Override
protected Void doInBackground(Void... voids) {
    try {
        final String fromEmail = "seu.email@gmail.com"; // configure
        final String password = "sua-senha-de-app";      // use uma senha de app

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(fromEmail, password);
            }
        });

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromEmail));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        message.setSubject(subject);
        message.setText(body);

        Transport.send(message);
        Log.d("EmailSender", "Email enviado com sucesso.");

    } catch (Exception e) {
        Log.e("EmailSender", "Erro ao enviar email", e);
    }

    return null;
}

}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions