Results 1 to 6 of 6

Thread: mail error

  1. #1
    desiretolearn is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default mail error

    Java Code:
    import java.util.Properties;
    
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class Class1 {
        final String senderEmailID = "username@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.password",senderPassword);
             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();
                 
            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) {
                   Class1 class1;
            class1 =
                    new Class1("tousername@gmail.com","Test Mail from me","Here goes the body");
               }
    }



    here is my sample email in java i am enable to send mail can any one help me in figuring out the problem????
    Last edited by desiretolearn; 03-06-2014 at 11:04 AM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: mail error

    What problem?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    desiretolearn is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default Re: mail error

    i am unable to send the mail using this its showing as
    Java Code:
    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.
    Last edited by desiretolearn; 03-06-2014 at 11:06 AM.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: mail error

    You can't just code connection problems away, you need to figure out why you can't connect to the SMTP server. The port may be wrong, a firewall may be blocking it, etc.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: mail error

    Take a look at:

    If you continue to have the same (or other problems), see https://support.google.com/mail/answer/78775?hl=en.

    You can also google for "smtp.gmail.com" java (include the double-quotes in your search term).

  6. #6
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

Similar Threads

  1. error in sending mail on my web app
    By imanesmaeili in forum Java Servlet
    Replies: 0
    Last Post: 02-06-2011, 12:45 PM
  2. Error in sending mail using ssl server
    By fierof2 in forum Networking
    Replies: 5
    Last Post: 01-27-2011, 06:43 AM
  3. Error in forwading file to mail...
    By rakesh_n_mehta in forum Advanced Java
    Replies: 15
    Last Post: 02-09-2009, 02:28 PM
  4. error in reading mail contents
    By kib_tse in forum Networking
    Replies: 1
    Last Post: 08-22-2008, 01:14 AM
  5. Mail error
    By orchid in forum New To Java
    Replies: 1
    Last Post: 04-22-2007, 03:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •