AuthenticationFailedException in sending email from java code - java

I am trying to write a small java program that can send email from my corporate outlook account. I am planning to run this java program on my office machine only. Below is my program:
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(my_email_id, my_password);
}
});
MimeMessage mime = new MimeMessage(session);
try {
mime.setSender(new InternetAddress(emailid));
mime.setRecipient(Message.RecipientType.TO, new InternetAddress(emailid));
mime.setSubject("Test");
mime.setText("Testing");
Transport.send(mime);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am getting below error :
javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [BM1PR01CA0166.INDPRD01.PROD.OUTLOOK.COM]
I have tried removing the socketFactory properties as well, but no luck. Can anyone help me out with this one? I am guessing it might have something to do with proxy or any other security on my office machine, but I am not able to figure out what it is.

Related

Java, unable to send mail on linux

Using my home computer I am able to send an email through Gmail without error, however, when I try to run the same code on my Linux box It throws:
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:2182)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:726)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at io.paratek.updates.util.Mail.sendMail(Mail.java:34)
at io.paratek.updates.MailTest.main(MailTest.java:8)
My code
public class Mail {
public static void sendMail(String subject, String contents, String to) {
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myemail", "mypasswd");
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(to));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(contents);
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
I am using Oracle JDK 181 on both systems.
I am able to successfully connect "telnet smtp.gmail.com 465" on the Linux box
After switching to TLS it works
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");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

Javax.mail package didn't work in release mode apk

I'm using javax.mail package for mail sending,
while application in debug mode it's work fine and send every mail to my account, but when i create release application mail stop sending form application.
code is as given below:
Properties props = new Properties();
props.put("mail.smtp.user", "abc#xyz.com");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
//Creating a new session
session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc#xyz.com", "password#123");
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress("abc#xyz.com"));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress("abc#xyz.com"));
//Adding subject
mm.setSubject(subject);
//Adding message
mm.setText(message);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
Generally this problem happen because of proguard, If you add proguard into your project it'll stop some of functionality like this.
I did it by adding package javax in proguard-rules.pro file.
-keep class javax.** {*;}
this will add all javax methods's permission in proguard.

java PasswordAuthentication security

I am working on a Java program which record data from sensors and send me the result by email (the computer is far from my office).
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.enable", "false");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(LOGIN, PASSWORD);
}
});
So I put my login/password non-encrypted in my code. It works but it is not safe because anyone can uncompress the jar file and find my login/password. What could be the best approach? Even if I use an encrypted external file, I have to provided the key to decrypt the file.

Error when send mail in java using javamail?

i want to use javamail so i test this code
public class Test_Mail {
public static void main(String [] args)
{
String to="xyz#gmail.com";//change accordingly
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc#gmail.com","*****");
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc#gmail.com"));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Hello");
message.setText("Testing.......");
//send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
}
}
but i have this error:
Exception in thread "main" java.lang.RuntimeException: 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/bin/answer.py?answer=78754 gj16sm129363wic.24 - gsmtp
i tried many changes but it's the same error.
Because Google is such a large target for SPAM they have policies and practices in place to try and prevent as much SPAM as possible. It appears that your account may have been flagged by one of these policies and requires some manual intervention to get it working again.

how can we sent html content by java gwt mail api

i peek in many mail api like gmail etc. they send html content by mail api ,almost a form with some widgets. I am also trying to do that but whenever in try this with html content , its giving me error
String to="xyz#gmail.com";//change accordingly
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xyz.com","xyz");
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("hellofacebook180#gmail.com"));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(input);
message.setContent("<h1>sending html mail check</h1>","text/html" );;
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
return "massage sent";
please help how can I sent Html content with mail api
Email clients (Gmail, etc.) do not allow external CSS files. There is nothing you can do about it.

Categories