I have a program that allows users to send emails. To do that my code asks the user to enter their email (the sender email). Then the program checks to see what comes after the '#'. If its gmail then I set the properties as such:
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
If its yahoo, then I set the properties as such:
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.host","smtp.mail.yahoo.com");
props.put("mail.smtp.port","465");
Then I create a session object using the properties as such:
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
The above works fine. What I want to do is let the program know what the settings should be in case the user has a domain that is hosted on google or microsoft. So for example instead of the email being yyy#gmail.com, it would be yyy#customer-domain.com. Is there a way for my program to know where the email is hosted or will I have to let the program ask the user? Thanks.
Related
I have written a simple java program to connect to TLS enabled SMTP server as below
props.put("mail.smtp.host",GMAIL SMTP SERVER );
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", 587);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
loSession = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("ak#gmail.com",
GMAIL ACCOUNT PASSWORD)
Later setting the from email address as
InternetAddress loFromIA = new InternetAddress( "ashu#gmail.com");
loMsg.setFrom( loFromIA );
Now once i executed the code then i am receiving email from address ak#gmail.com which is SMTP server account address instead of ashu#gmail.com. I have explicitly set the from address as ashu#gmail.com but Gmail is overriding it. Same is happening with Microsoft server also.
Can someone suggest what needs to be fixed in code ? Also this happens after adding startTLS in code. without starttls it was working fine.
I'm having some troubles sending emails via Javamail using my company exchange server. We have an application that was sending emails via the gmail server without any problems, but for some changes in the policies of Google we want to use the company server to do the job.
Im sure the problem in the session properties, but i cant find a way to make it work
Properties props = new Properties();
props.put("mail.smtp.port", 465);
props.put("mail.smtp.socketFactory.port", 465);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.host", _server);
session = Session.getInstance(props, this);
try {
transport = session.getTransport("smtp");
transport.connect("mail.company.com",_user,_pass);
transport.close();
This is the error is showing the log
javax.mail.MessagingException: Could not connect to SMTP host: mail.company.com, port: 443;
nested exception is:
avax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
You have to check your email provider and its SMTP settings; server, port and encryption method.
The following code snippet works with me
Put
//1) get the session object
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
// You have missed this line.
properties.put("mail.smtp.starttls.enable", "true");
// This SMTP server works with me for all Microsoft email providers, like: -
// Outlook, Hotmail, Live, MSN, Office 365 and Exchange.
properties.put("mail.smtp.host", "smtp.live.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.user", user);
properties.put("mail.smtp.pwd", password);
Session session = Session.getInstance(properties, null);
session.setDebug(true); // To trace the code implementation.
Transport transport = session.getTransport("smtp");
transport.connect("smtp.live.com", 587, user, password);
transport.close();
instead of
props.put("mail.smtp.port", 465);
props.put("mail.smtp.socketFactory.port", 465);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.host", _server);
session = Session.getInstance(props, this);
try {
transport = session.getTransport("smtp");
transport.connect("mail.company.com",_user,_pass);
transport.close();
I found this website so helpful, in getting other email providers SMTP settings information.
When I try to send mail in java from my personal email like (sp#gmail.com) it is sent successfully.
But when I am using my company email(sp#example.com) it throws Authentication failed exception. I am using TLS authentication and it is successfully connected to host.
When I am manually login on my email it will always ask for Two Step
verification. Even if I have disabled my two step verification and have also done the change to make it less secure, it still asks for two step verification as it is showing this message after putting my username and password:
2-Step Verification
Based on your organization's policy, you need to turn on 2-step verification. Contact your administrator to learn more.
Enter one of your 8-digit backup codes
In this situation what should I do? As this is my first task in this company, I would be so happy if you could help me. How I can solve this problem?
My code :
String to = "abc#example.com";
String user = "sp#example.com";
String pass = "1234";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user,pass);
}
});
session.setDebug(true);
try
{
/* Create an instance of MimeMessage,
it accept MIME types and headers
*/
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
/* Transport class is used to deliver the message to the recipients */
Transport.send(message);
}
catch(Exception e)
{
e.printStackTrace();
}
Error message:
535 5.7.3 Authentication unsuccessful javax.mail.AuthenticationFailedException at javax.mail.Service.connect(Service.java:319) at javax.mail.Service.connect(Service.java:169) at javax.mail.Service.connect(Service.java:118) at javax.mail.Transport.send0(Transport.java:188) at javax.mail.Transport.send(Transport.java:118) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)
SMTP configuration:
configuration:props.put("mail.smtp.host", "smtp.oceaneering.com"); props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
First is need to turn on 2-Step Verification .(after that google script to give new option for creation app passwords) https://support.google.com/accounts/answer/185839?
Next you must to create the app passwords https://support.google.com/accounts/answer/185833?hl=en
Last step to replace this line in your code
String pass = "1234"; - with app specific password, (it must generate on step 2) Some like String pass = "djd5n7hsd";
And For future reader in may be useful to check network and firewall setting on your system and mail setting your ISP. To check if packet can to reach gmail server.
Try telnet command : telnet smtp.gmail.com 587
While sent email showing error:
javax.mail.MessagingException: Could not connect to SMTP host: smtp8.net4india.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
My email settings is:
String mailProcessed = "";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp8.net4india.com");
props.put("mail.smtp.socketFactory.port", "25");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(frmMail, frmPass);
}
});
remove props.put("mail.smtp.starttls.enable", "true");
and add props.put("mail.smtp.ssl.enable", "true");
You are using SSL, not TLS.
Also make sure the mail server supports SSL.
Also make sure that the mail server is using TCP protocol SMTP, they might be using IMAP or POP3.
In that case you need to change the properties you reference by the protocol they use, i.e:
props.put("mail.pop3.socketFactory.port", "25");
props.put("mail.pop3.socketFactory.class","javax.net.ssl.SSLSocketFactory");
You are using getDefaultInstance for the session, but if a session was already established it will return the existing session and your properties will be lost.
Check the common mistakes page, it has it all:
https://javaee.github.io/javamail/FAQ#commonmistakes
I am creating a application in which i am sending mail to users.
But the problem is while i am using Transport.send(msg) method, it displays the following error:
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required.
I am using the following properties to send mail.
props.put("mail.transport.protocol", "smtp");
props.put("mail.host", smtpHostName);
props.put("mail.user", smtpUser);
props.put("mail.password", smtpPass);
props.put("mail.smtp.auth", smtpAuth == 1 ? true : false);
props.put("mail.port", smtpPort);
props.put("mail.smtp.starttls.enable", smtpTLS == 1 ? true : false);
Please help me to resolve this problem.
Try this
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "false");
props.put("mail.smtp.port", port);
And while creating session use this authentication code
l_session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
l_session.setDebug(true);
Our environment is running under WebSphere 7. To support secured smtps, correct variant to turn on SMTPS AUTHorization is:
"mail.smtps.auth=true"
Actually, according to decompiled sources, before authorizing the mail session WebSphere checks if there is a property composed as: "mail."+ transportProtocol + ".auth" set to "true".
This helped me to send emails via gmail.