Created
April 13, 2018 04:34
-
-
Save SalaSuresh/8f4d9bd31cfde73bb0ac06ce310efa6b to your computer and use it in GitHub Desktop.
sending mail from android application..(need to add INTERNET permission in manifest and need to add jar library files, use this link to download (https://drive.google.com/file/d/1L488o5wIDQE-X9ajq2EDpp7asjmjd3fp/view?usp=sharing))
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.salasuresh.androidpractice.sendmail; | |
import android.os.AsyncTask; | |
import java.io.UnsupportedEncodingException; | |
import java.util.Properties; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.PasswordAuthentication; | |
import javax.mail.Session; | |
import javax.mail.Transport; | |
import javax.mail.internet.AddressException; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeMessage; | |
public class MailService { | |
private static final String USER_NAME = "<SENDER EMAIL ID>"; | |
private static final String PASSWORD = "<SENDER EMAIL PASSWORD>"; | |
private static final String PERSONAL = "Send Mail Demo"; | |
public void sendMail(String email, String subject, String messageBody) { | |
try { | |
Message message = createMessage(email, subject, messageBody, createSession()); | |
new SendMailTask().execute(message); | |
} catch (AddressException e) { | |
e.printStackTrace(); | |
} catch (MessagingException e) { | |
e.printStackTrace(); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
} | |
private Message createMessage(String email, String subject, String messageBody, Session session) throws MessagingException, UnsupportedEncodingException { | |
Message message = new MimeMessage(session); | |
message.setFrom(new InternetAddress(USER_NAME, PERSONAL)); | |
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email)); | |
message.setSubject(subject); | |
message.setText(messageBody); | |
return message; | |
} | |
private Session createSession() { | |
Properties properties = new Properties(); | |
properties.put("mail.smtp.auth", "true"); | |
properties.put("mail.smtp.starttls.enable", "true"); | |
properties.put("mail.smtp.host", "smtp.gmail.com"); | |
properties.put("mail.smtp.port", "587"); | |
return Session.getInstance(properties, new javax.mail.Authenticator() { | |
protected PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication(USER_NAME, PASSWORD); | |
} | |
}); | |
} | |
private class SendMailTask extends AsyncTask<Message, Void, Void> { | |
@Override | |
protected Void doInBackground(Message... messages) { | |
try { | |
Transport.send(messages[0]); | |
} catch (MessagingException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.salasuresh.androidpractice.sendmail; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.Button; | |
import com.example.salasuresh.androidpractice.R; | |
public class SendMailActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.layout_common); | |
Button sendButton = findViewById(R.id.button_common); | |
sendButton.setVisibility(View.VISIBLE); | |
sendButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
String email = "<RECEIVER EMAIL ID>"; | |
String subject = "TEST SUBJECT FROM PROGRAM"; | |
String message = "TEST MESSAGE FROM PROGRAM"; | |
new MailService().sendMail(email, subject, message); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment