Send email automatically by java Mail API android without intent - on background
Вставка
- Опубліковано 19 вер 2024
- Today, I’m going to show you how to send email from android app using java mail API. Watch to learn how it work and don’t forget to turn on your notifications!
First you need to create app password for send email from app.Follow the instructions.
Create & use App Passwords
Go to your Google Account.
On the left navigation panel, choose Security.
On the "Signing in to Google" panel, choose App Passwords. If you don’t see this option:
2-Step Verification is not set up for your account.
2-Step Verification is set up for security keys only.
Your account is through work, school, or other organization.
You’ve turned on Advanced Protection for your account.
At the bottom, choose Select app and choose the app you’re using.
Choose Select device and choose the device you’re using.
Choose Generate.
Follow the instructions to enter the App Password. The App Password is the 16-character code in the yellow bar on your device.
Choose Done.
source code on gitHub :
github.com/Usa...
download lib :
code.google.co...
If Anyone has problems with Transport.send, the problem is with the google's permissions, you must put a aplication password. Great Video!
I love you for this, man. Really saved my ass. I don't know why SMTP servers are so needlessly annoying to set up, but they really are. Your code is so much cleaner than basically all other solutions I found AND yours actually works straight away, no fuss. props, man.
Worth noting for others in the future, it didn't work in the emulator and I was lost for a while on why it wouldn't work, but for the sake of seeing if the emulator was causing issues, I loaded it on my phone and boom, the message was sent.
brother i am sad that why didn't i found your video first, good work keep it up
Thank you so much, i wish i found this perfect source before all that wasted time,
If I may ask, how can i attach a file to this mail please ???
Congrats man, this actually helped me a lot, I'll share it alright bro.
Nice.... helped me a lot, where did u learn this? i could not find document about
Thanks a lot it helped me so much,but is there anyway to send some html code with the page?
Thanks a lot. This works like a charm...
Perfect !, I have a problem with the login video. How could I contact you to consult a thing ...?
Send me your LinkedIn or Facebook account and i will connect with you ... Thx 👍😊
@@runtimecode4058 thanks! facebook.com/kevin.geminis.7/
this doesn't work anymore
when i have use my personal email in utils.java it does not work but when i have use yours it works. what security i have to change in my personal email??
go to your google account and turn on
Go to your Google Account.
On the left navigation panel, choose Security.
On the "Signing into Google" panel, choose App Passwords. If you don’t see this option:
2-Step Verification is not set up for your account.
2-Step Verification is set up for security keys only.
Your account is through work, school, or other organizations.
You’ve turned on Advanced Protection for your account.
At the bottom, choose Select app and choose the app you’re using.
Choose Select device and choose the device you’re using.
Choose Generate.
Follow the instructions to enter the App Password. The App Password is the 16-character code in the yellow bar on your device.
Choose Done.
but in my case. it is not sending email from my app.
hi there,
good job perfect.
and can you explain how can i add a attachment in the mail ?
thx a lot.
yes i will show you
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(Utils.EMAIL));
msg.addRecipients(Message.RecipientType.TO, String.valueOf(new InternetAddress(toEmail)));
msg.setSubject("Csv Files ");
//msg.setText("This is automatic mail form text android");
Multipart emailContent = new MimeMultipart();
MimeBodyPart textbody = new MimeBodyPart();
//textbody.setText("Test msg");
//csv attachment
MimeBodyPart pdfAttachment = new MimeBodyPart();
Calendar c = Calendar.getInstance();
int day, month, year, hr, min, sec;
String date, time;
day = c.get(Calendar.DAY_OF_MONTH);
month = c.get(Calendar.MONTH);
year = c.get(Calendar.YEAR);
hr = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
sec = c.get(Calendar.SECOND);
time = hr + "-" + min + "-" + sec;
date = day + "-" + month + "-" + year + "-" + time;
String folder_main = "Sensor_Data";
String csv = (Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+folder_main+"/"+"sensor_data.csv");
pdfAttachment.attachFile(csv);
emailContent.addBodyPart(textbody);
textbody.setText(date);
emailContent.addBodyPart(pdfAttachment);
msg.setContent(emailContent);
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(context, "Error while sending file", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
here is the working code
you can change the path as you need and it will send the document to the current time date and millisecond
for msg.addRecipients(Message.RecipientType.TO, String.valueOf(new InternetAddress(toEmail)));
this line variable define your destination email address into String for Example
String toEmail= "example232@gmail.com";
thanks but i already have it.
@@gta-servicesgta-services7396could you please tell me how to attach file to this email?
I'm not able to add the loibs folder in project structure, after adding the files in libs I can't open them in project structure, can you help me please?
I can't send a message to Mail , I think since the K app can access the account in the Ultils class, can u help me ? thank u verry much
Thanks...works fine.
is it possible to send image as an attachment, if yea can u tell me how?
Hi
Do you know how can i add attachment?
It's not working for me
Bro, now this code throws no error. But I'm not receiving the mail sent from app.
same problem
Can you use one image with the text please
Thanks bro
Is this work on the fragment tab as i created navigation drawer and i wanted to send the mail from the fragment can you please help
With the new google update, from 31st of May 2022 you will need to login with a application password (can generate these in your mail account settings), if not the authentication will result as failed
Can you explain it? I have the same problem and I don't know how to fix it...
@@sergibordes8401 try using this password instead of using the acconut password support.google.com/accounts/answer/185833?hl=en
hey @Runtime Code. can you please help me in how to use app password for an android emulator?
thank you : )
Cannot resolve symbol 'context'
4:04 u do it so fast are u in a hurry
Error in .getDefaultInstance() how to fix it?!
please help me.
Hi, i want to use this code to send a specific message to an email every time someone presses the button. Is there any way to make the string for mail,subject and message to be fixed?
I'm sure it's a tad late for you now, but in MainActivity, you can change the variables in private void senEmail() to be something like String mMessage = "Sample Message Body"; and that should do it.
@@LykosMenon Well I finished the project at work by now, but still thanks your replying !
@@LykosMenon Thanks
it's free
Yup
@@runtimecode4058 how many numbers mail ? any limitations ?
thanks a lot for this video
@@harishbariya3463 💯 email per day
@@anupampremium2971 I managed to send more then 100
I try and I install the source code but I face below :
javax.mail.AuthenticationFailedException
Any help plz
where i can contact you for a help?