මොකක්ද බං මේකෙ අවුල?? (JAVA HELP)

මොකක්ද බං මේකෙ අවුල?? (JAVA HELP)

මොකක්ද බං මේ්කෙ අවුල??
කොතනින්ද code එක break වෙන්නෙ කියල බලන්න තැනින් තැනට 1,2,3,4 print වෙන්න දැම්ම.
ඉතිං බං error එක එන්නෙ transport.send() වලදි බං.

Code:
[SIZE=4]import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;





/**
 *
 * @author 
 */
public class JavaApplication2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args){
        // TODO code application logic here
        
        
       String to = "[email protected]";            // sender email
        String from = "[email protected]";       // receiver email
        String host = "smtp.gmail.com";                   // mail server host

       Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host);

        Session session = Session.getDefaultInstance(properties);// default session
         
        System.out.println("1");//just to check

        try {
            MimeMessage message = new MimeMessage(session);        // email message
            message.setFrom(new InternetAddress(from));                    // setting header fields
             System.out.println("2");//just to check
             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("Test Mail from Java Program"); // subject line
 System.out.println("3");//just to check
            // actual mail body
            message.setText("You can send mail from Java program by using mail API, but you need"
                    + "couple of more JAR files e.g. smtp.jar and activation.jar");
 System.out.println("4");//just to check
 
            // Send message
            Transport.send(message);
            System.out.println("Email Sent successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }



        
    }
    
}[/SIZE]

OUTPUT + ERROR



run:
1
2
3
4
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. c1sm8249455pdc.45 - gsmtp

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108) at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at javaapplication3.JavaApplication3.main(JavaApplication3.java:58)

BUILD SUCCESSFUL (total time: 8 seconds)
 

Sx3

Well-known member
  • Apr 4, 2014
    3,411
    2,391
    113
    Code:
    Session session = Session.getDefaultInstance(properties,
                    new javax.mail.Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username, password);
                        }
                    });
     

    Sx3

    Well-known member
  • Apr 4, 2014
    3,411
    2,391
    113
    Code:
    import java.util.Properties;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    /**
     *
     * @author
     */
    public class JavaApplication2 {
    
        static String username = "YOUR_USER_NAME";//ubage username eka from kiyala dana eka
        static String password = "YOUR_PASSWORD";//ubage password eka from kiyala dana eka
    
        /**
         * ex:[email protected] eke username and password eka dapan YOUR_USER_NAME and YOUR_PASSWORD kiyana thanata
         */
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
    
            String to = "[email protected]";
            String from = "[email protected]";
            String host = "smtp.gmail.com";
    
            Properties properties = System.getProperties();
            properties.setProperty("mail.smtp.host", host);
    
            Session session = Session.getDefaultInstance(properties,
                    new javax.mail.Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username, password);
                        }
                    });
    
            System.out.println("1");//just to check
    
            try {
                MimeMessage message = new MimeMessage(session);        // email message
                message.setFrom(new InternetAddress(from));                    // setting header fields
                System.out.println("2");//just to check
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                message.setSubject("Test Mail from Java Program"); // subject line
                System.out.println("3");//just to check
                // actual mail body
                message.setText("You can send mail from Java program by using mail API, but you need"
                        + "couple of more JAR files e.g. smtp.jar and activation.jar");
                System.out.println("4");//just to check
    
                // Send message
                Transport.send(message);
                System.out.println("Email Sent successfully....");
            } catch (MessagingException mex) {
                mex.printStackTrace();
            }
    
        }
    
    }