Automatic mail sent on button click android
To send an automatic email on a button click in an Android app, you can use the JavaMail API and the Android Email Intent. Here's a step-by-step guide:
Step 1: Add the JavaMail API to your project
- Add the JavaMail API to your project by adding the following dependency to your
build.gradle
file:dependencies { implementation 'com.sun.mail:android-mail:1.6.2' }
- Sync your project with Gradle files.
Step 2: Create a button and a layout
-
Create a new activity in your Android app and add a button to the layout.
-
In the
activity_main.xml
file, add the following code:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/send_email_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Email" />
- In the
MainActivity.java
file, add the following code:import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;
import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties;
public class MainActivity extends Activity {
private Button sendEmailButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendEmailButton = findViewById(R.id.send_email_button);
sendEmailButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendEmail();
}
});
}
private void sendEmail() {
Properties props = new Properties();
props.put("mail.smtp.host", "your-smtp-host");
props.put("mail.smtp.port", "your-smtp-port");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your-email", "your-password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your-email"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient-email"));
message.setSubject("Test Email");
message.setText("This is a test email");
Transport.send(message);
Toast.makeText(this, "Email sent successfully!", Toast.LENGTH_SHORT).show();
} catch (MessagingException e) {
Toast.makeText(this, "Error sending email: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
* Replace `your-smtp-host`, `your-smtp-port`, `your-email`, and `your-password` with your actual SMTP host, port, email address, and password.
* Replace `recipient-email` with the email address of the recipient.
**Step 4: Run the app**
* Run the app on an emulator or a physical device.
* Click the "Send Email" button to send an automatic email.
Note: Make sure to replace the placeholders with your actual values and test the email sending functionality before deploying the app.