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

Поделиться
HTML-код
  • Опубликовано: 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
      -

  • @xlivetrend
    @xlivetrend Год назад +1

    Thank you you save my day

  • @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
      -

  • @sebastianbraga
    @sebastianbraga 2 года назад +1

    Really helpful. Thank you

  • @thanhnhan8957
    @thanhnhan8957 4 месяца назад +1

    Thank you

  • @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
      -

  • @hashanmadusanka6128
    @hashanmadusanka6128 6 месяцев назад +1

    Thank you so much

  • @hayabusa_lonely
    @hayabusa_lonely 11 месяцев назад +1

    Thank you very very match

  • @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

  • @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

  • @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
      -

  • @wj-nr7bx
    @wj-nr7bx 8 месяцев назад

    this help me alot i love u so much

  • @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
      -

  • @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/

  • @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
      -

  • @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
      -

  • @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
      -

  • @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
      -

  • @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
      -

  • @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?

  • @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??

  • @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 RUclips 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
      -