Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. sending an e-mail

sending an e-mail

Scheduled Pinned Locked Moved Java
questioncomdebugginghelp
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    prasadbuddhika
    wrote on last edited by
    #1

    i found the code for sending e-mail , but my problem is that mail is not received by the recipient .

    public void postMail(String recipients[], String subject, String message, String from) throws MessagingException {
    boolean debug = false;

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
    
        System.out.println("step 1 ");
        
    
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);
    
        Message msg = new MimeMessage(session);
    
       InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
    
        InternetAddress\[\] addressTo = new InternetAddress\[recipients.length\];
        for (int i = 0; i < recipients.length; i++) {
            addressTo\[i\] = new InternetAddress(recipients\[i\]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);
    
       msg.addHeader("MyHeaderName", "myHeaderValue");
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
    
        Transport.send(msg);
    } 
    

    i call this method with

    String [] st = new String[1];
    st[0] = "nuwan";

    this.postMail(st, "test", "prasadbuddhika42@gmail.com", "prasadbuddhika42@gmail.com");

    can anyone tell me what is wrong in this code ?

    G 1 Reply Last reply
    0
    • P prasadbuddhika

      i found the code for sending e-mail , but my problem is that mail is not received by the recipient .

      public void postMail(String recipients[], String subject, String message, String from) throws MessagingException {
      boolean debug = false;

          Properties props = new Properties();
          props.put("mail.smtp.host", "smtp.gmail.com");
      
          System.out.println("step 1 ");
          
      
          Session session = Session.getDefaultInstance(props, null);
          session.setDebug(debug);
      
          Message msg = new MimeMessage(session);
      
         InternetAddress addressFrom = new InternetAddress(from);
          msg.setFrom(addressFrom);
      
          InternetAddress\[\] addressTo = new InternetAddress\[recipients.length\];
          for (int i = 0; i < recipients.length; i++) {
              addressTo\[i\] = new InternetAddress(recipients\[i\]);
          }
          msg.setRecipients(Message.RecipientType.TO, addressTo);
      
         msg.addHeader("MyHeaderName", "myHeaderValue");
          msg.setSubject(subject);
          msg.setContent(message, "text/plain");
      
          Transport.send(msg);
      } 
      

      i call this method with

      String [] st = new String[1];
      st[0] = "nuwan";

      this.postMail(st, "test", "prasadbuddhika42@gmail.com", "prasadbuddhika42@gmail.com");

      can anyone tell me what is wrong in this code ?

      G Offline
      G Offline
      Gil Messerman
      wrote on last edited by
      #2

      Gmail SMTP server requires authentication and uses SSL. You should add following to your properties: mail.transport.protocol=smtps mail.smtps.host=smtp.gmail.com mail.smtps.user=your gmail username mail.smtps.password=your gmail password mail.smtps.auth=true

      -------------   Gil Messerman

      S 1 Reply Last reply
      0
      • G Gil Messerman

        Gmail SMTP server requires authentication and uses SSL. You should add following to your properties: mail.transport.protocol=smtps mail.smtps.host=smtp.gmail.com mail.smtps.user=your gmail username mail.smtps.password=your gmail password mail.smtps.auth=true

        -------------   Gil Messerman

        S Offline
        S Offline
        shrims4u
        wrote on last edited by
        #3

        Im also developing an email client adding the lines given by you it gave an error from gmail Issue STARTTLS command how to code for this??

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups