-3

hello guys im trying to send email to my email account but when i put this code there is error with the @Override when i delite it saying me to add when i add it then saying me to delite it and always an error... :/ here is the code:

String username = Username.getText();
    String password = Password.getText();
    final String username2 = "stefanrafaa@gmail.com";
    final String password2 = "my pass";
    try {
        File folder = new File(username);
        File file = new File(folder, username + ".txt");
        FileReader freader = new FileReader(file);
        BufferedReader breader = new BufferedReader(freader);
        String line = breader.readLine();
        String[] parts = line.split("=");
        String key = parts[0].trim();
        String value = parts[1].trim();
        if (key.equals("Password") && value.equals(password)) {
            showError2(true);
            new Thread(new Start(this)).start();
            LabelInfo.setForeground(Color.green);
            LabelInfo.setText("Password Accepted");
        } else {
            LabelInfo.setText("Wrong password");
            showError(true);
        }
    } catch (Exception ex) {
        System.out.println("Exception: " + ex.getMessage());
        LabelInfo.setForeground(Color.red);
        LabelInfo.setText("User doesn't exist");
        showError(false);
        showError2(false);
    }

    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 = Session.getInstance(props, new javax.mail.Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username2, password2);
        }
    });

    try {
        //Session session = null;

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("stefanrafa0@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("stefanrafaa@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user2133393
  • 11
  • 1
  • 4
  • 2
    "and always an error" - *what* error, exactly, and where? You've posted a huge amount of code, only a tiny proportion of which is relevant, as far as I can tell. – Jon Skeet Mar 20 '13 at 17:52
  • And, if that's your email address, you might want to change it. – Lee Meador Mar 20 '13 at 17:55
  • Similar code with lots of "answers" which are more like comments is here: http://stackoverflow.com/questions/1630002/java-lang-noclassdeffounderror-javax-mail-authenticator-whats-wrong – Lee Meador Mar 20 '13 at 18:03
  • there is an error now in 'return new PasswordAuthentication(username2, password2);' and btw dont giving me now to remove and add the @Override method – user2133393 Mar 20 '13 at 18:10
  • and also i have this code earlier and it was working fine but now when i add it to the other Project dont working ... :/ – user2133393 Mar 20 '13 at 18:15

1 Answers1

0

long shot: you've imported the wrong type for PasswordAuthentication (make sure it is "javax.mail.PasswordAuthentication").

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • yup there we go :) "protected javax.mail.PasswordAuthentication getPasswordAuthentication(URLName name) { return new javax.mail.PasswordAuthentication(username2, password2);" Thanks :) – user2133393 Mar 20 '13 at 18:21
  • now giving me error when i pressing the button to "send" saying: errorjavax.mail.AuthenticationFailedException: failed to connect, no password specified? – user2133393 Mar 20 '13 at 18:28
  • @user2133393 - i'd suggest making it a new question if you can't figure it out. – jtahlborn Mar 20 '13 at 19:09