com. sun. mail. smtp. SMTPSendFailedException:530-5.5. 1 requires authentication (java mail)
So I tried to make java mail work, because the other answers I saw on this website were useless. I had to assume that there had been some changes in the past year or so So if the question looks like a repetition, I can't explain why it doesn't work Here is my code:
try{ Properties property = new Properties(); property.setProperty("mail.smtp.host","smtp.gmail.com"); property.setProperty("mail.smtp.starttls.enable","true"); //property.setProperty("mail.smpt.port","25"); property.setProperty("mail.smtp.user","myEmail@gmail.com"); property.setProperty("mail.smtp.auth","true"); System.out.println("Mail Check 1"); Session session = Session.getDefaultInstance(property); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("myEmail@gmail.com")); message.addRecipient(Message.RecipientType.TO,new InternetAddress("myEmail@gmail.com")); System.out.println("Mail Check 2"); message.setSubject("Oil Error Report"); message.setText(emailMessage); System.out.println("Mail Check 3"); Transport transport = session.getTransport("smtps"); transport.connect("smtp.gmail.com",465,"myEmail@gmail.com","myPassword"); transport.sendMessage(message,message.getAllRecipients()); transport.close(); System.out.println("Mail Sent"); }catch(Exception ex){ System.out.println("Mail fail"); System.out.println(ex); }
I got up to check my email 3, and then I got the following exception:
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 u3sm10254140ioi.27 - gsmtp
It doesn't make sense because I will mail smtp. Auth is set to true I've looked at past answers and tried to make it work before publishing I assume it's simple Any help will be greatly appreciated! thank you!
Solution
public class EmailSender {
public class EmailSender { public void sendEmail(String emailMessage){ try{ final String fromEmail = ""; //requires valid gmail id final String password = ""; // correct password for gmail id final String toEmail = ""; // can be any email id System.out.println("TLSEmail Start"); Properties props = new Properties(); props.put("mail.smtp.host","smtp.gmail.com"); //SMTP Host props.put("mail.smtp.port","587"); //TLS Port props.put("mail.smtp.auth","true"); //enable authentication props.put("mail.smtp.starttls.enable","true"); //enable STARTTLS //create Authenticator object to pass in Session.getInstance argument Authenticator auth = new Authenticator() { //override the getpasswordAuthentication method protected PasswordAuthentication getpasswordAuthentication() { return new PasswordAuthentication(fromEmail,password); } }; Session session = Session.getInstance(props,auth); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(fromEmail)); message.addRecipient(Message.RecipientType.TO,new InternetAddress(toEmail)); System.out.println("Mail Check 2"); message.setSubject("Oil Error Report"); message.setText(emailMessage); System.out.println("Mail Check 3"); Transport.send(message); System.out.println("Mail Sent"); }catch(Exception ex){ System.out.println("Mail fail"); System.out.println(ex); } } }
This is the code needed to make it run! It was in November 2014 and is currently available for Gmail! I hope this code can help save time and take up most of my time! In addition to this code, you must change your email settings to allow these emails The first time you try, you will receive an email from Google, which will guide you to change your settings
Thank you. Good luck!