Java - Send Email from Java Program - Java Mail API

Поделиться
HTML-код
  • Опубликовано: 16 ноя 2024

Комментарии • 257

  • @GenuineCoder
    @GenuineCoder  3 месяца назад +1

    This video is over 5 years old now. Watch the updated version here: ruclips.net/video/kLMUS0-PznE/видео.html. It covers the java email topic with the latest steps for generating an app password.

  • @husseinmasri901
    @husseinmasri901 4 года назад +4

    The perfect volume for background music, I can still hear u and focus on what u r doing, and the video gets its good feel

  • @ymoose1162
    @ymoose1162 4 года назад +7

    Worked perfectly for me however took a very long time to follow your steps as you code entire blocks at once very quickly using the suggested options in the drop down boxes. Also it would help to review the entire code at the end of the video especially the final imported packages list.
    I hope this is useful for your future videos but overall good job thanks for your help

  • @manoj__rawat
    @manoj__rawat 4 года назад +8

    Thanks working fine for me.
    if there is security problem write this -
    properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");

  • @anmolkachroo2234
    @anmolkachroo2234 4 года назад

    thankyou so much sir , firstly i got an error because i didn't mention the password, but then it worked ...

  • @viggomayn2220
    @viggomayn2220 4 года назад +3

    Thank you so much! This was such a clean video super good tutorial! Keep up the good work, love from a CS student in Denmark!

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

    Amigo, você é incrivel. Salvou meu TCC. Obrigado e toma esse like!

  • @pd.ferreira
    @pd.ferreira 4 года назад +4

    Great explanation. I'll stay tuned for more tutorials like this.
    Greetings from Brazil!

  • @kwekker
    @kwekker 3 года назад

    holy shit man you helped me so much that low security setting was very hidden, thank you very much.
    no but for real thank you so much I finally sent an email yes

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

    it worked perfectly

  • @alfredochola5971
    @alfredochola5971 3 года назад

    THANKS ALOT GENUINE CODER

  • @kingsoftamil6191
    @kingsoftamil6191 3 года назад +1

    Good job Bro...

  • @rajat1999gupta
    @rajat1999gupta 2 года назад

    Excellent tutorial

  • @fahimkamalahmed3544
    @fahimkamalahmed3544 5 лет назад

    Thanks a lot bro. You explained very easily and now my program is working perfectly.

  • @fvastmphozathebeanbagger9252
    @fvastmphozathebeanbagger9252 5 лет назад

    You are an absolute genius

  • @ebenswart12
    @ebenswart12 3 года назад +1

    Very well done, this video actually helps you and you explain everything. Thank you so much for making this video

  • @eduardfrinculeasa3325
    @eduardfrinculeasa3325 3 года назад

    Very Usefull thanks

  • @maiyao0o6
    @maiyao0o6 3 года назад

    Its working perfectly thanks alot brother

  • @rohitkarambali779
    @rohitkarambali779 5 лет назад

    U r really good teacher sir

  • @ufinityy
    @ufinityy 4 года назад +2

    Thank you so much! This was a great video and you explained everything very well!

  • @maryclareelizabethgamao5638
    @maryclareelizabethgamao5638 2 года назад

    Thaks this is very useful.

  • @DayWithMe802
    @DayWithMe802 4 года назад

    Excellent work nice keep it up bro

  • @TheDrComedy
    @TheDrComedy 4 года назад +11

    Hello, Can you help please? I receive this error on Transport.send(message). "Couldn't connect to host, port: true, 25; timeout -1"

    • @majdidani9941
      @majdidani9941 3 года назад

      i had the same error , if you solved it , can you help me ?

  • @rockysperson9102
    @rockysperson9102 4 года назад +1

    It would be nice if you would put this code on github. Thanks for all of the hard work.

  • @ivanstefanov5213
    @ivanstefanov5213 2 года назад

    Great job helped me with my finala project for It talents Bulgaria . Greetings brother. Ako znesh mamichkata mi se eba s toq proekt , Krasi rules

  • @yourownsde
    @yourownsde 2 года назад

    thank you for this awesome video

  • @shivatejapamballa808
    @shivatejapamballa808 5 лет назад

    Thanks alot dear. I got it clear now I will dig more into Java Mail API with your background.

  • @sivabalanj
    @sivabalanj 5 лет назад +4

    First comment:
    thank you so much

  • @victorjo007
    @victorjo007 5 лет назад +2

    Interesting and clearly explained video thanks!

  • @petztech
    @petztech Год назад

    you are the man

  • @mustafabaki275
    @mustafabaki275 4 года назад

    Thank you very much very helpful video.

  • @Frius94
    @Frius94 5 лет назад +1

    Nice video and explanation but this shows one more time that you have to write a lot of code in Java for simple things like sending an email. In other languages like PHP sending an email requires ONLY 1 line of code.

    • @linuxland
      @linuxland 5 лет назад +1

      PHP is designed to be a web language while Java is totally not but being able to do this is just an extra
      A lot of Java functionality cannot be found on PHP as well

    • @akashsoftengg3284
      @akashsoftengg3284 5 лет назад +1

      See robustness not line of code

    • @thegutterking
      @thegutterking 2 года назад

      let's see that one line of code, I have been trying to use PHP to send emails from a database for a school project and ended up using a Java app to do it.

  • @nithyapriyaav6327
    @nithyapriyaav6327 3 года назад

    Thank you for the video

  • @GenuineCoder
    @GenuineCoder  5 лет назад +20

    Hi Everyone,
    In case you are facing any issues with sending email (not specific to gmail, you can use any SMTP server), try to troubleshoot it as follows.
    - Make sure that the "host", "port", "email-address" and "password" are correct. www.smtper.net/ is a good tool to make sure that you have the correct values since you can use this service to send dummy emails.
    - If you encounter 'Couldn't connect to host' error, somehow your system is not able to connect to remote email server. This may be because of your antivirus, firewall blocking connection. Add exception rules in firewall or try temporarily disabling them to confirm the issue cause.

    • @bana-agjr.jimm.9737
      @bana-agjr.jimm.9737 3 года назад

      heyyy! thanks for the tutorial. I just wanna ask if how are we gonna send email to multiple mails with just a click?

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

      I am getting the couldn't connect to host error please tell me how can I fix it

  • @tejaschalke1778
    @tejaschalke1778 2 года назад

    If anyone is having problems where the email is sent and seen on the same account used for sending the email, but can't see it on other accounts, use a account that has 2 factor authentication enabled to send email. If you are concerned about the password use "App password" feature in "security" tab of your gmail account to generate random password.

  • @easy_coding0
    @easy_coding0 3 года назад

    thank you sir, it work perfectly

  • @AliVaseghnia
    @AliVaseghnia 5 лет назад +1

    Finally got it to work, thank you!

  • @gamechannel_833
    @gamechannel_833 3 года назад

    Man, thank you so much! Good tutorial!

  • @matiasmerkuri1605
    @matiasmerkuri1605 5 лет назад +1

    thank you this works flawlessly!

    • @ElyesBoudhina
      @ElyesBoudhina 5 лет назад

      could you plz parse the code here , i am getting this error :
      javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. though i am using these parameters
      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");

  • @huidrompunendrajitsingh1758
    @huidrompunendrajitsingh1758 2 года назад

    Add the below code to properties if you have exception of javax Could not convert socket to TLS;
    props.put("mail.smtp.ssl.protocols", "TLSv1.2");
    and bro thanks for this video.

  • @mohammedsardar3779
    @mohammedsardar3779 5 лет назад +2

    Why don't we have JAVA Mail in Java 8 JDK itself ? Why are we adding as third party ? Can we include if we use Maven in this case ? If anyone has done with maven what is the artifact details, please share. Thanks.

  • @leroycarrasquero9065
    @leroycarrasquero9065 2 года назад

    Can you upgrade to run this in Jakartaa and Glassfish? is returning comsunmailimapIMAPProvider not a subtype exception :c

  • @sumukhmurthy124
    @sumukhmurthy124 3 года назад

    you absolute legend

  • @mahasenmohamed1036
    @mahasenmohamed1036 4 года назад +5

    sir, when i do the same as you did, i got an error with: Transport.send(message) ,and when i use the same function inside another class as a function i got another problem with Session.getInstance() that it can`t find the symbol and the Session class in this case doesn`t have the function....please tell me what should i do

    • @asimlatif360
      @asimlatif360 4 года назад +1

      You imported wrong class. Make sure that your imports look like these:
      import java.util.Properties;
      import java.util.logging.Level;
      import java.util.logging.Logger;
      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;

  • @dipakpardeshi6152
    @dipakpardeshi6152 4 года назад

    It's working, thank you so much sir.

  • @MH-in8hz
    @MH-in8hz 4 года назад +2

    Awesome work, but is this normal? it takes 8 seconds to send an email. I thought Java is a fast-running language since it compiles lines of codes and doesn't interpret. I'm planning for sending a survey email to all people in my college. I tried only on my 4 emails it took 35 seconds. 3 seconds I think for accessing an array of strings. maybe it has something to do with authentication & other stuff.

  • @enkr1
    @enkr1 4 года назад

    Really clear. Thank you.

  • @ROHITVETRI
    @ROHITVETRI 3 года назад

    like how to send an attachment is there any set() value to be given?

  • @SovonSingha
    @SovonSingha 5 лет назад +2

    After trying so many codes from different cannel , finally it's really happening ,I am able to send mail using Java, Thanks a lot( ◜‿◝ )♡🥰

  • @riteshjain1235
    @riteshjain1235 3 года назад

    great explanation, keep it up!!!!

  • @mk20130470
    @mk20130470 3 года назад

    Use quieter keyboard. But great vid!

  • @sweetberry5808
    @sweetberry5808 4 года назад +1

    Is this only for Gmail ? can I use outlook email as a sender?

  • @techmoon_
    @techmoon_ 3 года назад

    great video man !!!

  • @pokyshorts9074
    @pokyshorts9074 2 года назад

    Thank you.

  • @xyonzx4094
    @xyonzx4094 3 года назад

    halo sir, for sending the mail it takes 1-2 minute ? im using your github but the result in performance is different.

  • @andreiiorga6813
    @andreiiorga6813 3 года назад

    Thank you very much sir it helped me a lot. Any idea where can I find a tutorial about mail listener? I wand to be able to intercept every email from an email address. Thank you.

  • @siege26
    @siege26 Год назад

    🔥thanks.

  • @Falconbyte
    @Falconbyte 3 года назад

    Great video! It was very well explained! TOP!

  • @VAMSIKRISHNA-kr3qf
    @VAMSIKRISHNA-kr3qf 4 года назад +5

    class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first.
    i am getting this error in the Transport block

    • @123Juanplay
      @123Juanplay 3 года назад +1

      i got the same error went back and look like i had the STARTTLS wrote wrong here the correct one properties.put("mail.smtp.starttls.enable", "true");

  • @Slayer0248
    @Slayer0248 Год назад

    Have you encountered this error when sending the email before by any chance and would you know how to solve it?
    javax.mail.MessagingException: Could not convert socket to TLS;
    nested exception is:
    javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair

  • @mahasenmohamed1036
    @mahasenmohamed1036 3 года назад

    I wrote this code in net beans and it worked, but when I wrote the same code in eclipse it gave me exception error..... why? or what should I do??

  • @sathishshankar9331
    @sathishshankar9331 4 года назад +1

    Hi ,
    Iam getting the following error
    javax.mail.SendFailedException: Sending failed;
    nested exception is:
    class javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
    nested exception is:
    java.net.ConnectException: Connection refused: connect
    Please help me on this

  • @chaimaebougattaya1785
    @chaimaebougattaya1785 2 года назад

    Thank you so much !

  • @ARUNKUMAR-qo1nt
    @ARUNKUMAR-qo1nt 5 лет назад +1

    I am getting this issue "Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;" please help me to find out.

  • @aryanmansani8135
    @aryanmansani8135 3 года назад

    Thanks Alott Dude!!!

  • @kyleburke5570
    @kyleburke5570 3 года назад +1

    I'm getting a class not found exception on the line where I do "Message message = new MimeMessage(session);" and im not sure how to resolve this. Any help is appreciated ty

    • @shadowgamer8549
      @shadowgamer8549 3 года назад

      yea im getting the same problem too for some reason

    • @DeadlyDudeOE
      @DeadlyDudeOE 2 года назад

      I feel like I've seen your name from somewhere...have we met my dude?

  • @misrra7477
    @misrra7477 4 года назад

    Great tutorial, thank you so much!!

  • @DChristop
    @DChristop 4 года назад

    In case that you want to send email via office 365 with java app. Is this the same code?

  • @surajnath5148
    @surajnath5148 2 года назад

    Everything works fine in my localhost, but when I configured in my live server it sending mail after 1 to 2 (too slow)minutes. kindly Help🙏🏻

  • @savanluffy5223
    @savanluffy5223 5 лет назад +4

    PasswordAuthentication is outdated. You should use the 0Authentification from google which is more secure

    • @GenuineCoder
      @GenuineCoder  5 лет назад +4

      Thanks a lot for the info. I didn't know about 0Auth before. I will surely check it and maybe make an updated video with it.

  • @techzoo1
    @techzoo1 4 года назад

    Super helpful!

  • @tranthanhvo2179
    @tranthanhvo2179 5 лет назад +1

    OMG i did it! Thank you!

    • @AlfrickOpidi
      @AlfrickOpidi 5 лет назад

      I also did it!

    • @ElyesBoudhina
      @ElyesBoudhina 5 лет назад +1

      hi guys could any one of you parse the code here i tried this one it did not work
      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 Authenticator()
      {
      @Override
      protected PasswordAuthentication getPasswordAuthentication()
      {
      return new PasswordAuthentication("***Myaccount","*****Mypassword****");
      }
      });
      try {
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("***Account***"));
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("***Account***"));
      message.setText("Test Text");
      Transport.send(message);
      System.out.println("Email sent Successfully !");
      }catch(MessagingException e){
      System.err.println("Something went wrong !");
      throw new RuntimeException(e);
      }
      -----------------------------------------------------------------
      i get this error
      Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;
      nested exception is:
      class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. v188sm10670600wma.10 - gsmtp
      at Métier.Debug.main(Debug.java:129)
      Caused by: javax.mail.SendFailedException: Sending failed;
      nested exception is:
      class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. v188sm10670600wma.10 - gsmtp
      at javax.mail.Transport.send0(Transport.java:218)
      at javax.mail.Transport.send(Transport.java:80)
      Any suggestion guys ?

  • @ufinityy
    @ufinityy 4 года назад +1

    could you also put this on github?

  • @g-sUp
    @g-sUp 3 года назад

    thx u very much!!!:)))

  • @mrtato0692
    @mrtato0692 5 лет назад

    Thanks, it helps me a lot !!

  • @musharafaahel8963
    @musharafaahel8963 3 года назад

    Can you help me out
    It shows that javax.mail.Session is not accessible
    may I know what should I do?

  • @deshbhakti5467
    @deshbhakti5467 5 лет назад +1

    Can u make a video how to style according with css using javafx facing many problem . plz its request

  • @silent1884
    @silent1884 2 года назад

    well explained, thank you

  • @Arouitta1
    @Arouitta1 5 лет назад +1

    help pleas my fire wall already off i need this for a prject i m working on
    javax.mail.AuthenticationFailedException: failed to connect, no password specified?
    my password and eamil are correct i double checked them

  • @Ben-ie2ru
    @Ben-ie2ru 2 года назад

    Interesting tutorials thanks a lot. But i have done the same things but unfortunatelly the e-mail can't be send . I don't know why. May you help me please to resolve this problem? thanks for your understanding

    • @Ngonie_3
      @Ngonie_3 2 года назад

      It is because of the Less Secure Apps option in Google Security. By default it's switched off, and when such is like that, you cannot send emails from third party applications, such as the ide you're using.

  • @kluchtube7042
    @kluchtube7042 2 года назад

    thanks man

  • @anilkumarkannelli5620
    @anilkumarkannelli5620 5 лет назад

    Can i use this code in web services as like when response getting as failure then failure response had been send through mail.please help me.

  • @markkero791
    @markkero791 4 года назад

    It's work in the ide perfectly but w
    I make a runnable jar it doesn't work the message won't send, any help please

  • @notskyz9602
    @notskyz9602 3 года назад

    Doesn't seems to work with JDK11 and Eclipse

  • @akashsoftengg3284
    @akashsoftengg3284 5 лет назад

    Thank you sir😊😊

  • @yashgoswami9227
    @yashgoswami9227 4 года назад

    thanks a lot

  • @howtovideogames3268
    @howtovideogames3268 4 года назад +1

    Hi, im getting the error java.lang.NoClassDefFoundError: java/mail/Address. i cant seem to find any fixes online.
    Thanks

  • @ryankoech8300
    @ryankoech8300 4 года назад

    having trouble with the authenticator. says something about incomparable types and i have written as you have.

    • @a96futurecreator96
      @a96futurecreator96 3 года назад

      solved , use ' import javax.mail.Authenticator ' instead of ' java.net.Authenticator '.

  • @TobySullivan
    @TobySullivan 4 года назад

    i tried importing the javax.mail thing into the eclipse package, but it says it can't access it. what do i do?

    • @123Juanplay
      @123Juanplay 3 года назад

      you may have not putting the jar in the correct place

  • @ogunodabas9332
    @ogunodabas9332 4 года назад

    Hello, I used the same code before. In 2016-2017. I could send more than 1 mail in 1 second. Now he sends 1 mail in 20 seconds. It may have caused an update. I couldn't help, can you help?

  • @chabetiico
    @chabetiico 3 года назад

    I can implement CSS in this API?

  • @GayathriS-tv6ou
    @GayathriS-tv6ou 2 года назад

    Hi every one, i havw facing issues like(javax.mail.MessagingException: Could not convert socket to TLS;) please give me some solution .

  • @Maciejos223
    @Maciejos223 4 года назад

    Did someone get Exception "Username and Password not accepted"? I'm sure that I'm writing my email account and password correctly and I don't have any security attached to my account.

  • @anusharma640
    @anusharma640 3 года назад

    can you please guide me to send bulk mails like this way

  • @shivakumar7578
    @shivakumar7578 4 года назад

    sir what if the recepient mail does not exist ? how to check it in the program?

  • @tanujjaiswal2175
    @tanujjaiswal2175 5 лет назад

    theres a probelem. when iam changing recepient mail then it doesnt sending any email and showing error like username and password not authenticated. plz help me

  • @mohamedaminelmoudene5420
    @mohamedaminelmoudene5420 4 года назад

    How find any probleme with send exception . Try with adding the flowing line
    properties.put("mail.smtp.ssl.enable");

  • @lockstrote1
    @lockstrote1 3 года назад +1

    Did not work for me:
    Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Authenticator

    • @yusof9200
      @yusof9200 3 года назад

      same :(

    • @Falconbyte
      @Falconbyte 3 года назад

      Had the same problem first. Which JAva-Version do you use? In 1.8 it worked fine for me.

    • @lockstrote1
      @lockstrote1 3 года назад

      @@Falconbyte using jdk11 here.

  • @anandohaider4557
    @anandohaider4557 5 лет назад +1

    shows this error:
    com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 587; timeout -1;

    • @GenuineCoder
      @GenuineCoder  5 лет назад +1

      Couldn't connect to host => This happened because the program can't access the email server.
      Make sure your program can access the internet and is not blocked by firewall, antivirus etc.

    • @mariaivashkina7207
      @mariaivashkina7207 5 лет назад

      At first check access to the host via telnet "telnet smtp.gmail.com 587" (javaee.github.io/javamail/FAQ#condebug).If it's ok try to turn off firewall and antivirus (as Genuine Coder replied).I has the same error because my antivirus blocked "dangerous attaches in emails" without any notifications.

  • @FADEDElement
    @FADEDElement 5 лет назад +3

    I keep getting this error: Must issue a STARTTLS command first.

    • @123Juanplay
      @123Juanplay 3 года назад

      add this properties.put("mail.smtp.starttls.enable", "true");