Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SendMail extends AsyncTask<Void, Void, Void> {
- //Declaring Variables
- private Context context;
- private Session session;
- //Information to send email
- private String email;
- private String subject;
- private String message;
- //Progressdialog to show while sending email
- private ProgressDialog progressDialog;
- //Class Constructor
- public SendMail(Context context, String email, String subject, String message) {
- //Initializing variables
- this.context = context;
- this.email = email;
- this.subject = subject;
- this.message = message;
- }
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- //Showing progress dialog while sending email
- progressDialog = ProgressDialog.show(context, "Sending message", "Please wait...", false, false);
- }
- @Override
- protected void onPostExecute(Void aVoid) {
- super.onPostExecute(aVoid);
- //Dismissing the progress dialog
- progressDialog.dismiss();
- //Showing a success message
- Toast.makeText(context, "Message Sent", Toast.LENGTH_LONG).show();
- }
- @Override
- protected Void doInBackground(Void... params) {
- String prot = "smtp";
- final String user = Config.EMAIL;
- String to = Config.EMAIL;
- final String pass = Config.PASSWORD;
- String mailhost = "smtp-mail.outlook.com";
- Properties props = System.getProperties();
- props.put("mail." + prot + ".host", mailhost);
- props.put("mail." + prot + ".auth", "true");
- props.put("mail." + prot + ".starttls.enable", "true");
- props.put("mail." + prot + ".port", "587");
- props.put("mail." + prot + ".ssl.enable", "false");
- Session session = Session.getDefaultInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user, pass);
- }
- });
- session.setDebug(true);
- try {
- Message msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress(user));
- msg.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(to, false));
- msg.setSubject("TESTE ENVIO EMAIL");
- msg.setText("Teste de envio de email conteudo");
- msg.setHeader("X-Mailer", user);
- msg.setSentDate(new Date());
- SMTPTransport t = (SMTPTransport) session.getTransport(prot);
- t.connect(mailhost, user, pass);
- t.sendMessage(msg, msg.getAllRecipients());
- t.close();
- Log.d("------------", "done");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
Add Comment
Please, Sign In to add comment