How to send email using gmail SMTP server directly from your Android App?

Поділитися
Вставка
  • Опубліковано 15 вер 2024
  • In this page, it shows the steps to create method to send email directly from your Android App without using the mail clients (such as gmail client or outlook App).
    This code uses gmail smtp server to send the email.
    I hope you like this video. For any questions, suggestions or appreciation please contact us at: programmerworl... or email at: programmerworld1990@gmail.com
    Complete source code and other details of this video are posted in the below link:
    programmerworl...
    However, the main Java code is copied below also for reference:
    package com.programmerworld.sendemailapp;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import java.util.Properties;
    import javax.mail.Authenticator;
    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 MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
    public void buttonSendEmail(View view){
    try {
    String stringSenderEmail = "SenderEmail963@gmail.com";
    String stringReceiverEmail = "receiveremail963@gmail.com";
    String stringPasswordSenderEmail = "Test*123";
    String stringHost = "smtp.gmail.com";
    Properties properties = System.getProperties();
    properties.put("mail.smtp.host", stringHost);
    properties.put("mail.smtp.port", "465");
    properties.put("mail.smtp.ssl.enable", "true");
    properties.put("mail.smtp.auth", "true");
    javax.mail.Session session = Session.getInstance(properties, new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(stringSenderEmail, stringPasswordSenderEmail);
    }
    });
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(stringReceiverEmail));
    mimeMessage.setSubject("Subject: Android App email");
    mimeMessage.setText("Hello Programmer,

    Programmer World has sent you this 2nd email.

    Cheers!
    Programmer World");
    Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
    try {
    Transport.send(mimeMessage);
    } catch (MessagingException e) {
    e.printStackTrace();
    }
    }
    });
    thread.start();
    } catch (AddressException e) {
    e.printStackTrace();
    } catch (MessagingException e) {
    e.printStackTrace();
    }
    }
    }
    --

КОМЕНТАРІ • 67

  • @myclassroom1649
    @myclassroom1649 2 роки тому +3

    Thanks A lot man Finally I found Really grateful video I stuck for whole day and you solve my problem Thx a lot .. : )

  • @theunwrittenquote
    @theunwrittenquote Рік тому +2

    LOTS OF RESPECT AND LOVE FOR THIS ♡

  • @experimentoselectricosdeju6882

    It works June 2023; required 2 steps authentification gmail Sender account + create app passwords for devices

    • @nurulizzatizulkepli6728
      @nurulizzatizulkepli6728 Рік тому

      its not work for me.. can you fixed my code?

    • @nurulizzatibintizulkeplist6275
      @nurulizzatibintizulkeplist6275 Рік тому

      can you help me.. im not work

    • @TirthShah133
      @TirthShah133 Рік тому

      Can you please guide me on 2-step authentication and App password method? I tired it already but didn't work. Thanks

    • @ProgrammerWorld
      @ProgrammerWorld  Рік тому

      Please refer to the below:
      programmerworld.co/android/how-to-create-your-custom-gmail-client-android-app-using-the-new-googles-app-password-approach-for-authentication-api-34/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @ashishwilson7187
    @ashishwilson7187 2 роки тому +4

    not able to get mail do this as per google update based on 30 may 2022

    • @SamuelHernandez-pd9kj
      @SamuelHernandez-pd9kj 2 роки тому

      Same issue :(

    • @song-mt6bn
      @song-mt6bn Рік тому

      something?

    • @ProgrammerWorld
      @ProgrammerWorld  Рік тому

      Below may help:
      programmerworld.co/android/how-to-create-your-custom-gmail-client-android-app-using-the-new-googles-app-password-approach-for-authentication-api-34/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @ltchanu7646
    @ltchanu7646 Рік тому +1

    It work in 2023.4.29🤗🤗🤗 Thank you.

    • @badreddine4296
      @badreddine4296 Рік тому

      How did it work cause the less secure app is not available anymore?

    • @ProgrammerWorld
      @ProgrammerWorld  Рік тому

      You can check the below:
      programmerworld.co/android/how-to-create-your-custom-gmail-client-android-app-using-the-new-googles-app-password-approach-for-authentication-api-34/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @xlivetrend
    @xlivetrend Рік тому +1

    Thank you you save my day

  • @sebastianbraga
    @sebastianbraga 2 роки тому +1

    Really helpful. Thank you

  • @phantomkahn4696
    @phantomkahn4696 Рік тому +1

    This worked perfectly after using app password, thank you so much!

    • @josealzugaray17
      @josealzugaray17 Рік тому

      how did you do it? It does not work for me uu

    • @phantomkahn4696
      @phantomkahn4696 Рік тому

      @@josealzugaray17 what error do you see?

    • @ariefariefshamsuddin350
      @ariefariefshamsuddin350 Рік тому

      How did you do it, still does not work for me

    • @TirthShah133
      @TirthShah133 Рік тому

      Can you please guide me on App password method? Thanks

    • @ProgrammerWorld
      @ProgrammerWorld  Рік тому

      Please refer to the below:
      programmerworld.co/android/how-to-create-your-custom-gmail-client-android-app-using-the-new-googles-app-password-approach-for-authentication-api-34/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @hayabusa_lonely
    @hayabusa_lonely 11 місяців тому +1

    Thank you very very match

  • @hashanmadusanka6128
    @hashanmadusanka6128 6 місяців тому +1

    Thank you so much

  • @wj-nr7bx
    @wj-nr7bx 8 місяців тому

    this help me alot i love u so much

  • @annetak693
    @annetak693 Рік тому

    As of January 2023 this method works wonderfully only on the test versions. But once the version is uploaded to googleplay store, people who upload the version can't sent the messages... or to be more exact the messages never arrive. Never figured out the problem. Any updates would be very welcomed!

    • @ProgrammerWorld
      @ProgrammerWorld  Рік тому

      I think App Password approach may be required for authentication. Below may help:
      programmerworld.co/android/how-to-create-your-custom-gmail-client-android-app-using-the-new-googles-app-password-approach-for-authentication-api-34/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @TechnicalYarana
    @TechnicalYarana 2 роки тому +2

    sir i got it issue in my project what is javax.mail.Session 08:45

    • @ProgrammerWorld
      @ProgrammerWorld  2 роки тому

      javax.mail.Session is to create session that is an instance of email client in your App using the properties, such as hostname, password, etc.
      I hope above explanation helps.
      For reference, the complete source code shown in this video is also shared in the below link:
      programmerworld.co/android/how-to-send-email-using-gmail-smtp-server-directly-from-your-android-app/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @mudasirkhan8058
    @mudasirkhan8058 2 роки тому +1

    i have error my Session and MimMessage Library is not import javaFx

    • @ProgrammerWorld
      @ProgrammerWorld  2 роки тому

      I hope below dependencies are implemented in the gradle file.
      implementation 'com.sun.mail:android-mail:1.6.6'
      implementation 'com.sun.mail:android-activation:1.6.7'
      Also, please check that below libraries are imported in your Java file.
      import javax.mail.Authenticator;
      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;
      For reference, the complete source code shown in this video is also shared in the below link:
      programmerworld.co/android/how-to-send-email-using-gmail-smtp-server-directly-from-your-android-app/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @chisomemmanuel6160
    @chisomemmanuel6160 2 роки тому

    First time commenting in my life 💕💕
    Please can you make the one that'll send one particular email message to every user of my android app maybe through firebase

    • @ProgrammerWorld
      @ProgrammerWorld  2 роки тому +1

      Adding all the receipients in the mimeMessage will easily send one message to all the people in one go:
      mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(stringReceiverEmail));
      For reference, the complete source code shown in this video is also shared in the below link:
      programmerworld.co/android/how-to-send-email-using-gmail-smtp-server-directly-from-your-android-app/
      Cheers
      Programmer World
      programmerworld.co
      -

    • @chisomemmanuel6160
      @chisomemmanuel6160 2 роки тому +1

      @@ProgrammerWorld Thanks 😊 man

  • @thanhnhan8957
    @thanhnhan8957 4 місяці тому +1

    Thank you

  • @ngocmanprocoder
    @ngocmanprocoder 11 місяців тому

    I did, but I encountered a "Messaging Exception" error. Could you help me solve it, please? thank you so much.

    • @ProgrammerWorld
      @ProgrammerWorld  11 місяців тому

      Check the updated version of this tutorial in the below. It may help:
      programmerworld.co/android/how-to-create-your-custom-gmail-client-android-app-using-the-new-googles-app-password-approach-for-authentication-api-34/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @NipunSandeepa-m1u
    @NipunSandeepa-m1u 8 місяців тому +3

    wow its work 2024

    • @user-ym7tj3gk1b
      @user-ym7tj3gk1b 8 місяців тому

      what version of android studio did you used?

    • @NipunSandeepa-m1u
      @NipunSandeepa-m1u 7 місяців тому

      @@user-ym7tj3gk1b Android Studio Giraffe | 2022.3.1

  • @prafuldeshmukh1178
    @prafuldeshmukh1178 Рік тому

    i was using this email send method and i have implemented this in my firebase project it worked well for some days and then google has suspended my account by saying this account was using to send spam messages does anyone knows how this happens

    • @ProgrammerWorld
      @ProgrammerWorld  Рік тому

      It seems like there is limit for sending number of emails with this approach (limit imposed by google for security and fare use reasons). This limit as far as we know is 100 emails at a time and 500 emails in a day. Anything beyond this may make Gmail to suspend your account temporarily.
      For reference, details shown in this video is also shared in the below link:
      programmerworld.co/android/how-to-send-email-using-gmail-smtp-server-directly-from-your-android-app/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @vulcatechdev
    @vulcatechdev 5 місяців тому

    Love it, is there some way to send files?

    • @ProgrammerWorld
      @ProgrammerWorld  5 місяців тому

      To send the files call setContent method on the mimeMessage object.
      Something like below:
      mimeMessage.setContent(MimeMultipart)
      I will try to bring up a separate tutorial on this.
      programmerworld.co/android/how-to-send-email-using-gmail-smtp-server-directly-from-your-android-app/

  • @KnowledgeTV143
    @KnowledgeTV143 Рік тому

    We need to add jar file in classpath?

    • @ProgrammerWorld
      @ProgrammerWorld  Рік тому

      Not sure about the Jar file you are referring. But android-mail dependencies needs to be added in the Gradle file:
      implementation 'com.sun.mail:android-mail:1.6.6'
      implementation 'com.sun.mail:android-activation:1.6.7'
      For reference, complete source code and details shown in this video is also shared in the below link:
      programmerworld.co/android/how-to-send-email-using-gmail-smtp-server-directly-from-your-android-app/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @manibala5799
    @manibala5799 2 роки тому

    Sir I had run your program successful with changing of sender and reciver mail I'd and password but I can't get mail when press send mail button pls help me....I will send my project to your email

    • @ProgrammerWorld
      @ProgrammerWorld  2 роки тому

      Complete source code of this app is shared in the below page. Please check.
      programmerworld.co/android/how-to-send-email-using-gmail-smtp-server-directly-from-your-android-app/
      Cheers
      Programmer World
      programmerworld.co
      -

    • @manibala5799
      @manibala5799 2 роки тому

      @@ProgrammerWorld it's done but I only change tha sender reciver mail I'd and password but mail not send

    • @manibala5799
      @manibala5799 2 роки тому

      I send you link can you check it plssss

    • @ProgrammerWorld
      @ProgrammerWorld  2 роки тому

      Kindly check whether the email service provider (gmail account) permits 3rd party Apps to access the Apps or not? If it is allowed then it should work.
      Also, check the code provided by us at:
      programmerworld.co/android/how-to-send-email-using-gmail-smtp-server-directly-from-your-android-app/
      If issue still persists then copy your code here. We will check..
      Cheers
      Programmer World
      programmerworld.co
      -

    • @vanisridhar5509
      @vanisridhar5509 2 роки тому

      Bro, did you rectified this problem??

  • @Pasa4009
    @Pasa4009 Рік тому

    I designed an app that shows my location information,
    I will be glad if you can help me how to write a code to send my location information as gmail.

    • @ProgrammerWorld
      @ProgrammerWorld  Рік тому +1

      Refer to my below pages. It may help:
      programmerworld.co/android/how-to-open-and-send-email-using-the-native-client-directly-from-your-android-app-source-code/
      programmerworld.co/android/how-to-send-email-using-gmail-smtp-server-directly-from-your-android-app/
      Cheers
      Programmer World
      programmerworld.co
      -

    • @Pasa4009
      @Pasa4009 Рік тому

      @@ProgrammerWorld thank you Professor

    • @rushikesh3363
      @rushikesh3363 11 місяців тому

      Your project title?

  • @tournamentkiller5778
    @tournamentkiller5778 2 роки тому +1

    Sir now this is not working properly please make new video

    • @ProgrammerWorld
      @ProgrammerWorld  2 роки тому +1

      Yes, google has restricted the access to less secured Apps from June 2022 onward.
      Now, an app password can be used to login. We will try to come up with an updated video on this subject soon.
      support.google.com/accounts/answer/185833
      For reference, the complete source code shown in this video is also shared in the below link:
      programmerworld.co/android/how-to-send-email-using-gmail-smtp-server-directly-from-your-android-app/
      Cheers
      Programmer World
      programmerworld.co
      -

    • @tournamentkiller5778
      @tournamentkiller5778 2 роки тому +2

      @@ProgrammerWorld but sir I created in kodular perfectly working sir

    • @ProgrammerWorld
      @ProgrammerWorld  2 роки тому +1

      Nice 👍

    • @tournamentkiller5778
      @tournamentkiller5778 2 роки тому

      @@ProgrammerWorld thanks sir I have a UA-cam channel technical yarana once check please

    • @yeowsongjie8186
      @yeowsongjie8186 2 роки тому

      @@ProgrammerWorld Sorry sir, is the video uploaded? Because i need to use it in my FYP ... Thank you so much sir

  • @sendcenter7002
    @sendcenter7002 2 роки тому +1

    I just got the codes. mail I wrote my own e-mail address, I edited it but it doesn't work

    • @ProgrammerWorld
      @ProgrammerWorld  Рік тому

      You can check below also:
      programmerworld.co/android/how-to-create-your-custom-gmail-client-android-app-using-the-new-googles-app-password-approach-for-authentication-api-34/
      Cheers
      Programmer World
      programmerworld.co
      -