Determine Mail Settings from Domain - java

I'm currently writing a simple Java application that will allow the user to send and receive email. When the application starts, it prompts the user to log in in the following format:
username#provider.com
passwd
Currently I have the field split so that provider.com is placed in its own string. Is there any way I can use the java mail api or something else to retrieve the settings for "provider.com?" I'm looking to get back strings such as "smtp.gmail.com" when the user has a gmail account and so-forth. Any help would be appreciated!
EDIT: To clarify what I am after...
final Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com"); // I want to get this (smtp.gmail.com)
properties.put("mail.smtp.port", "465"); // And this (465)
// from the user entered "gmail.com" or "live.com"

I just found this after some more searching: Finding SMTP host and port knowing the e-mail address using JAVA API
It seems there is no predictable way to do this. Very disappointing.

Related

java outlook client application getting 535 5.7.3 Authentication unsuccessful exception [duplicate]

I get this error when I try to send a mail using JavaMail API. I am sure that the username and password are 100% correct. The Gmail account which I'm connecting is an older account, because they say it takes time for it to work with new accounts.
DEBUG SMTP RCVD: 535-5.7.1 Username and Password not accepted. Learn more at
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 x35sm3011668
wfh.6
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.AuthenticationFailedException
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at Main.(Main.java:41)
at Main.main(Main.java:51)
and this is my code:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Main
{
String d_email = "abc#gmail.com",
d_password = "pass",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "abc#gmail.com",
m_subject = "Testing",
m_text = "testing email.";
public Main()
{
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setText(m_text);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
Transport.send(msg);
}
catch (Exception mex)
{
mex.printStackTrace();
}
}
public static void main(String[] args)
{
Main blah = new Main();
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(d_email, d_password);
}
}
}
I had same issue :
I refer this link, I have followed below steps it worked for me.
By default Gmail account is highly secured. When we use gmail smtp from non gmail tool, email is blocked. To test in our local environment, make your gmail account less secure as
Login to Gmail.
Access the URL as https://www.google.com/settings/security/lesssecureapps
Select "Turn on"
The given code snippet works fine on my Gmail account, so this problem lies somewhere else. Did you follow the link given in the error message? It contains the following hints:
Make sure that you've entered your full email address (e.g. username#gmail.com)
Re-enter your password to ensure that it's correct. Keep in mind that passwords are case-sensitive.
Make sure your mail client isn't set to check for new mail too often. If your mail client checks for new messages more than once every 10 minutes, your client might repeatedly request your username and password.
Especially the last point is important. Google is very strict in this. If you're trying to connect Gmail for example more than 10 times in a minute programmatically, then you may already get blocked. Have a bit of patience, after some time it will get unblocked.
If you'd like more freedom in sending mails, I recommend to look for a dedicated mail host or to setup your own mail server, such as Apache James or Microsoft Exchange. I've already answered this in detail in one of your previous questions.
I encountered the exact same problem, for me the reason is I have turned on 2-step verification on my gmail account.
After generating a new application-specific password and use that in my java application, this "535 5.7.1" issue is gone.
You can generate a new application-specific password following this official google guide.
I faced the same problem although my username and password were correct, but after some research, I was able to send email through applications by enabling the Less secure app access option on my Gmail account. You can find this feature under security option in the left menu.
Related links:
I can't sign in to my email client
Enable access to less secure applications
Now, Google not supported Less Secure Apps so we are getting this issue.
You follow bellow steps to resolve your Authentication issue during send mail using SMTP.
1 - Enable 2-step Verification in your mail
2 - Once you enabled above, you will able to see App Passwords option in same Security tab
3 - Now generate App Password for Other(Custom)
4 - Use this generated password in place of your gmail password in your code.
Enjoy :)
I have the same error message and this is how I have solved the issue,
Create an app password: here is how we can generate an app password,
1. Visit your App passwords page. You may be asked to sign in to your Google Account.
2. At the bottom, click Select app and choose the app you’re using.
Click Select device and choose the device you’re using.
3. Select Generate.
4. Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.
5. Select Done.
I worked for a Spring boot app and I get the app password say, sadsadaffferere for the email address, xyz#gmail.com. So, I need to configure the app properties like the following,
# the email settings
# ------------------
spring.mail.host=smtp.gmail.com
spring.mail.username=xyz#gmail.com
spring.mail.password=sadsadaffferere
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=false
support.email=xyz#gmail.com
Everything works fine afterwards
You need to turn on the 'less secure apps allowed' in gmail settings.
If the same credential was working earlier and it stopped working then the primary reason for this problem is password mismatch/changed on gmail client and not updated in Jenkins or other CI server. If that is not the case then check for reasons mentioned by #BalusC
i turned off antivirus and enabled less secure app in gmail, but now google has disabled this option, for that to work you need to do two steps verification and genetare 16 digit password and paste in application.properties replace this 16 digit password.

setup email connection in java

I am trying to make connection to the email server using property files and session store value. and all the parameters are given dynamically.
this is what i'm trying
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", emailHost);
props.put("mail.smtp.port", "2525");
Session sessions = Session.getDefaultInstance(props);
Store store = sessions.getStore(emailAccType);
store.connect(emailHost, emailId, emailPwd);
Even if i am giving the correct email and password, the connection is not setting up.
please do the needful help.
Thanks in advance.
Couple of ideas:
make sure the email server does not require pop3 login first.
make sure port is open in firewall
Do you get any exceptions during your tries?
Does it throw an exception or it just doesn't work?
I am using gmail for sending emails and I used something like this:
Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(email, password);
}
});

Java, send mail using SMTP always mail goes to spam folder, Why?

i'm say apology to ask this repeated Question..
i'm using gmail smtp server to send mails..
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.starttls.enable", "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");
But always my mail sending spam folder only....
Note: i'm using gmail as company name myname#mtmr.in
if it is normal gmail like myname#gmail.com,, it is working fine.. but i want through company mail..
It is difficult to say for sure why your email is being marked as SPAM, but one possibility1 is that your company has gotten itself a reputation for spamming. (Or more precisely, lots of gmail users have hit the "report spam" button on emails that they received from your company.)
If that is the problem, then there is not much you can do about that except create a filter on your gmail account to move those emails out of your spam. Of course, that only affects emails going to your account ... not anyone else's.
The long term remedy is to convince the people who are sending spam / spammy messages using your company's email address to STOP IT!
1 - The fact that the same email sent using your personal email address is NOT marked as SPAM suggests that this is more than just "a possibility" ...

Session timeout during connecting Hotmail account with Java mail API

For connecting with hotmail account through java mail API , I m setting these properties
pop3Props.setProperty("mail.pop3.ssl.enable", "true");
pop3Props.setProperty("mail.pop3s.socketFactory.class", SSL_FACTORY);
pop3Props.setProperty("mail.pop3s.socketFactory.fallback", "false");
pop3Props.setProperty("mail.pop3s.port", "995");
pop3Props.setProperty("mail.pop3s.socketFactory.port", "995");
Properties pop3Props = new Properties();
pop3Props.setProperty("mail.pop3s.port", "995");
Session session = Session.getInstance(pop3Props, null);
Store store = session.getStore("pop3s");
store.connect(host, 995, username, password);
I am able to login into my hotmail account and do other operations(send/receive) but
after some time (I think) session time out happens i.e not able to connect with hotmail
server.
Later sometime onward again it is working fine ( i m able to connect with hotmail
server).
And I checked in my code that whenever I open a new connection , I m closing it also.
Please help .
Are you leaving the connection open for long periods of time without doing anything?
Are you opening and closing the connection frequently over short periods of time?
Servers have lots of ways of preventing you from "abusing" their resources. You may be running into one of them.
Or, maybe you have an unreliable network connection?
See the JavaMail FAQ for debugging tips; the debug output might provide more clues as to why it's failing.
Also see the list of common mistakes; you can simplify your code.

Must issue a STARTTLS command first when trying to send a SimpleMailMessage in Grails

I try to send a message in a grails application. I use Java code with this problem tho, here is my code
SimpleMailMessage message_ref = new SimpleMailMessage();
JavaMailSenderImpl sender_ref = new JavaMailSenderImpl();
sender_ref.setHost("smtp.gmail.com")
sender_ref.setUsername("testAccount#googlemail.com")
sender_ref.setPassword("topsecret")
message_ref.setTo("testRecipient#gmx.de")
message_ref.setSubject("Hello there")
message_ref.setText("How are you")
sender_ref.send(message_ref)
I'm getting the following exception:
SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
I found a similar problem here on stackoverflow here
Must issue a STARTTLS command first. Sending email with Java and Google Apps
but it didn't help me cause he used an different approach.
Can somebody tell me what's wrong? I'm expecting the error is not in the code but in some configuration file and this is where my knowledge edges.
Quoting from the Grails mail plugin docs:
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "youracount#gmail.com"
password = "yourpassword"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
} }
I can't help you much, but your problem is basically that you need to use SSL to communicate with the server. Google does not allow plain-text communication for a lot of good reasons. I don't know much about grails, but I assume it has some sort of ssl-support. If it does not, you're probably better off doing it in javax.mail.
StartTLS is just a text-command you send to the smtp-server to explicitly start secure communications.
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);
properties.put("mail.smtp.port", smtpPort);
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.user", userName);
properties.put("mail.password", password);

Categories

Resources