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.
Related
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.
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");
This is my class that holds methods for sending email. I am using JavaMail API to send email. I checked multiple times if I pass right arguments from EmailAccount (<- it is a class that I created) and it seems correct. The problem is that I do not get any error or warning, all that is happening is that when program reaches Transport.send(msg); nothing is happening. It does not freeze or crash it is just running (it is like Transport.send(msg) is never completed). To close the program I have to break it manually from IDE. I will glad for any solutions to my problem.
public class EmailTools
{
public void sendEmail(EmailAccount emailAccount, String addressOfReceiver, String title, String text)
{
Properties props = new Properties();
props.put("mail.smtp.host", emailAccount.getSmtpHost());
props.put("mail.smtp.socketFactory.port", Integer.toString(emailAccount.getSslPort()));
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSlSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", Integer.toString(emailAccount.getSmtpPort()));
props.put("mail.host", emailAccount.getSmtpHost());
//Creating Authenticator
javax.mail.Authenticator auth = new javax.mail.Authenticator()
{
#Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication(emailAccount.getEmailAddress(), emailAccount.getPassword());
}
};
//Starting session
Session session = Session.getDefaultInstance(props, auth);
createAndSendEmail(session, emailAccount, addressOfReceiver, title, text);
}
public void createAndSendEmail(Session session, EmailAccount emailAccount, String receiver, String title, String message)
{
try
{
MimeMessage msg = new MimeMessage(session);
//Header stuff
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress(emailAccount.getEmailAddress(), "NoReply-JD"));
msg.setSubject(title, "UTF-8");
msg.setText(message, "UTF-8");
msg.setSentDate(new Date());
msg.setReplyTo(InternetAddress.parse(emailAccount.getEmailAddress(), false));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiver, false));
Transport transport = session.getTransport("smtps");
//EDIT1 transport.connect(emailAccount.getSmtpHost(),emailAccount.getSmtpPort(),emailAccount.getEmailAddress(),emailAccount.getPassword());
transport.sendMessage(msg,msg.getAllRecipients());
transport.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String args[])
{
EmailAccount emailAccount = new EmailAccount("example#gmail.com","smtp.gmail.com","password","",465,465);
EmailTools emailTools = new EmailTools();
emailTools.sendEmail(emailAccount,"example#gmail.com","Title","Text");
}
}
EDIT1:
I managed to solve the problem and got connected. But now i got the exception
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuuA
534-5.7.14 2SN9pvdVb-g3yxJj_u5P7eeAJOoLRRJMVevSThdvunt1c2qCcjkt9FRerrmg9YkB3UDbkc
534-5.7.14 wQDcDG8k4c8GcLrteODlY_danNGhcrg_bxE2_SgYioZK4nH0SzNW1K6-ZRCSlm-mTb6Auj
534-5.7.14 UOr5UqyodVDPHCi8fAmsRF-s30sF29nAdzNMjchSccoo3gwHYixRKSTb69XYWpat1SgRHK
534-5.7.14 FIJ5GjQ> Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 ll20sm5684460wic.14 - gsmtp
EDIT2:
Problem solved. It helped me a lot with gmail problem:
Unable to send email via google smtp on centos VPS
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.
Hi I have to send email in java.
The below has successfully worked for me.
public class SendMail {
public static void main(String[] args) {
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("xxxx#gmail.com","xxxxx!1");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("krishnaveni.veeman#gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("mercy.krishnaveni#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);
}
}
}
Here I have mentioned gmail username and password. But I have to send email without using username and password in my code. How can I develop this. Please help me.
create a mail.properties file and put username and password into in that file. Use Properties to retrieve data from this file in your code.
This link could be useful to load Properties object
http://www.dzone.com/snippets/loading-property-file