Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: unable to execute the mail code

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default unable to execute the mail code

    hi i am unable to run this java mail code can any body tell me whats wrong in tis code or may be whats the error!!
    public class Class1 {
        final String senderEmailID = "from@gmail.com";
        final String senderPassword = "password";
         final String emailSMTPserver = "smtp.gmail.com";
         final String emailServerPort = "465";
         String receiverEmailID = null;
         String emailSubject = null;
         String emailBody = null;
         public Class1(String receiverEmailID, String emailSubject, String emailBody) 
         {
     this.receiverEmailID=receiverEmailID;
     this.emailSubject=emailSubject;
     this.emailBody=emailBody;
            Properties props = new Properties();
             props.put("mail.smtp.user",senderEmailID);
                        props.put("mail.smtp.host", emailSMTPserver);
                          props.put("mail.smtp.port", emailServerPort);
                props.put("mail.smtp.starttls.enable", "true");
                          props.put("mail.smtp.auth", "true");
                      SecurityManager security;
            security = System.getSecurityManager();
                 System.out.println("security");
    try {
                Authenticator auth;
                auth = new SMTPAuthenticator();
                           Session session;
                session = Session.getInstance(props, auth);
                         MimeMessage msg;
                msg = new MimeMessage(session);
                            msg.setText(emailBody);
                 msg.setSubject(emailSubject);
                          msg.setFrom(new InternetAddress(senderEmailID));
                          msg.addRecipient(Message.RecipientType.TO,
             new InternetAddress(receiverEmailID));
                              Transport.send(msg);
                } catch (Exception mex) {
             mex.printStackTrace();
             }}
    private class SMTPAuthenticator extends javax.mail.Authenticator {
         public PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication(senderEmailID, senderPassword);
             }
         }
     public static void main(String[] args) {
           System.out.println("start");
            Class1 class1;
            class1 =
                    new Class1("to@gmail.com","Test Mail from me","Here goes the body");
            System.out.println("end");
        }
    }




    i get an error as this

    javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
    	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1949)
    	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    	at javax.mail.Service.connect(Service.java:317)
    	at javax.mail.Service.connect(Service.java:176)
    	at javax.mail.Service.connect(Service.java:125)
    	at javax.mail.Transport.send0(Transport.java:194)
    	at javax.mail.Transport.send(Transport.java:124)
    	at Class1.<init>(Class1.java:69)
    at Class1.main(Class1.java:90)
    end
    Process exited with exit code 0.
    any help appreciated


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: unable to execute the mail code

    Cross-post: mail error

Similar Threads

  1. Unable to execute dex: java.nio.BufferOverflowException.
    By dsclovers in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 8th, 2014, 12:22 AM
  2. [SOLVED] Unable to send a mail with attachment
    By Ragavarajan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 25th, 2012, 09:05 AM
  3. Implement 2PC Protocol - Unable to Compile and Execute
    By The.Reaper in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 29th, 2012, 06:25 AM
  4. Unable to execute the program (no compilation error)
    By Alexie91 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 16th, 2011, 02:39 PM
  5. unable to execute prepared statement
    By nrao in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 11th, 2010, 08:26 PM