I want to sent an Email but I don't know what I've doing wrong here. Can anyone of you help me pls?
EmailSender:
public class EmailSender {
public static boolean sendMail(String from, String password, String message, String to[]) {
String host = "smtp.gmail.com";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage mineMessage = new MimeMessage(session);
try {
mineMessage.setFrom(new InternetAddress(from));
InternetAddress[] toAddresses = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
toAddresses[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < to.length; i++) {
mineMessage.setRecipients(Message.RecipientType.TO, toAddresses);
}
mineMessage.setSubject("mail using javam");
mineMessage.setText(message);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, password);
transport.sendMessage(mineMessage, mineMessage.getAllRecipients());
transport.close();
return true;
} catch (MessagingException ex) {
Logger.getLogger(EmailSender.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
}
and here it's Main:
public class Main {
public static void main(String[] args) {
String[] to = {"[email protected]"};
if (EmailSender.sendMail("[email protected]", "******", "message to recive", args)){
System.out.println("The mail has sent");
}
else{
System.out.println("Don't work");
}
}
}
And here it's my error message:
Jan 08, 2016 10:01:49 PM recruitment.system.utils.EmailSender sendMail SEVERE: null javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 w73sm621753wmw.21 - gsmtp
I need to sent one mail from on address(any kind of address) no just Gmail or Yahoo to another address where again.. that can be anything. I've try to use that method but I don't know where or what I'm doing wrong because it don't work... And no just it, all methods I've try to send a mail has failed... Just please tell me where I've fail and how can I fix it to work.
You need to enter to your Gmail Settings and enable IMAP Access
see this link: https://support.google.com/mail/answer/78775?hl=en