Sending Mail in Android Without Intents Using Smtp

Sending mail in android without intents using SMTP

Put in your manifest file,

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

check if you have internet connection,

public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}

and finnaly use this code to send email

final String username = "username@gmail.com";
final String password = "password";

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

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");

MimeBodyPart messageBodyPart = new MimeBodyPart();

Multipart multipart = new MimeMultipart();

messageBodyPart = new MimeBodyPart();
String file = "path of file to be attached";
String fileName = "attachmentName"
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) {
throw new RuntimeException(e);
}

Sending Emails without Intents in Android

Try this- I had tried it worked for me...

http://androidautomail.blogspot.in/2013/06/auto-generated-mail-from-android.html?m=1

Sending Email in Android using JavaMail API without using the default/built-in app

Email Without Intent

I still don't know what exactly the problem was with my solution but changing it to this fixed it for me!
https://github.com/steveholt55/JavaMail-API-Android

For anyone's future reference, someone that looked at my code said the transport is getting disconnected but not sure how to fix that.

Send email without intent

When it comes to Firebase and sending emails to users, the best option that you have is to use Trigger Email Extension from Firebase. There is no need to open your Gmail account in order to send an email. To implement the extension please follow the steps that are explained in the following article:

  • How to install and use Trigger Email Extension from Firebase?

Is it possible to send an email in android without launching the corresponding application?

The answer is no, if you consider the following to be "external libraries":

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.AccessController;
import java.security.Provider;

If you do not consider those "external", I can give you code that works on Android 8.1 if you need. Tested with Gmail, Yahoo, and others.

Otherwise, AFAIK, there is no native Android API allowing one to send emails directly (i.e. not via an app).



Related Topics



Leave a reply



Submit