Android send mail github
Here are the steps to send an email using Android and GitHub:
Step 1: Create a new Android project
Create a new Android project in Android Studio. Name it, for example, "SendEmail".
Step 2: Add the necessary dependencies
In your build.gradle
file, add the following dependencies:
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.email:email:1.0.0'
}
The androidx.email:email:1.0.0
dependency is used to send emails.
Step 3: Create a new activity
Create a new activity in your project, for example, SendEmailActivity
.
Step 4: Write the code to send an email
In your SendEmailActivity
, write the following code:
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Properties;
public class SendEmailActivity extends AppCompatActivity {
private EditText etTo;
private EditText etSubject;
private EditText etMessage;
private Button btnSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_email);
etTo = findViewById(R.id.et_to);
etSubject = findViewById(R.id.et_subject);
etMessage = findViewById(R.id.et_message);
btnSend = findViewById(R.id.btn_send);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendEmail();
}
});
}
private void sendEmail() {
String to = etTo.getText().toString();
String subject = etSubject.getText().toString();
String message = etMessage.getText().toString();
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() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "your_password");
}
});
try {
Message message1 = new MimeMessage(session);
message1.setFrom(new InternetAddress("[email protected]"));
message1.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message1.setSubject(subject);
message1.setText(message);
Transport.send(message1);
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 "[email protected]"
and "your_password"
with your actual Gmail email and password.
Step 5: Run the app
Run the app on an emulator or a physical device. Open the SendEmailActivity
and enter the recipient's email, subject, and message. Click the "Send" button to send the email.
Note: Make sure to enable less secure apps in your Google account settings to allow the app to send emails. Also, be aware that storing your email password in plain text is not recommended for production apps. You should consider using a more secure method to store and retrieve your email credentials.
That's it! You have successfully sent an email using Android and GitHub.