Resolve own address - java

I'm connecting to a Mail-Server (IMAP) where the address of a postbox is not equal to the username that one uses to login.
For instance, to connect to the postbox with address myaddress#myhost.com, the code would look like this:
Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imap");
store.connect("mail.myhost.local", "user123", "pass");
Note that the user-name is different from the address.
How can the email-address of a specific user-host be resolved?

In general, it can't. The user has to tell you both.

Related

Determine Mail Settings from Domain

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.

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);
}
});

JavaMail IMAP not using specified properties

I'm seeing an issue where when connected to a mailbox using IMAP the infinite timeout default is causing an issue. I am having an issue getting Java Mail to recgonise IMAP properties. I verified IMAP did not seem to be using the properties by setting things like port number to the value 1, which should not work.
This is the code snippit:
Properties props = new Properties()
props.put("mail.imap.port", "1");
props.put("mail.imap.timeout", "1");
props.put("mail.imaps.connectiontimeout", "1");
Session session = Session.getInstance(props, null);
Store store = session.getStore("imaps");
store.connect(***,***,***);
If anyone knows where the problem is arising from that would great, all help is appreciated.
I believe you should be using props.setProperty(key, value) instead of using props.put(key, value). The documentation here: http://docs.oracle.com/javase/tutorial/essential/environment/properties.html warns you not to use hashTable methods
You're using the "imaps" protocol but setting properties for the "imap" protocol. Change your property names to "mail.imaps.*".

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.

Gmail Imap - Can't connect when I am in office network

I have been working at home on Gmail-Imap-Api on the weekend. It was working Properly But when i returned to office and am trying here it throws exception.
Properties props = System.getProperties();
props.put("mail.store.protocol", "imaps");
props.put("mail.smtp.auth", "true");
try {
boolean debug = false;
Authenticator auth = new SMTPAuthenticator(
"***", "***");
Session session = Session.getInstance(props, auth);
session.setDebug(debug);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "***#gmail.com",
"****");
......
......
This was working fine at home netwrok.
Now i thought i would add proxy and added these lines of code.
System.setProperty("http.proxyHost", "****.com");
System.setProperty("http.proxyPort", "8080");
Still it doesnt work and the exception is.
com.google.code.javax.mail.MessagingException: imap.gmail.com;
nested exception is:
java.net.UnknownHostException: imap.gmail.com
at com.google.code.com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:665)
at com.google.code.javax.mail.Service.connect(Service.java:295)
at com.google.code.javax.mail.Service.connect(Service.java:176)
at openReports.OpenReportsProject.main(OpenReportsProject.java:43)
Caused by: java.net.UnknownHostException: imap.gmail.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:367)
at java.net.Socket.connect(Socket.java:524)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:545)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at com.google.code.com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
at com.google.code.com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
at com.google.code.com.sun.mail.iap.Protocol.<init>(Protocol.java:113)
at com.google.code.com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:110)
at com.google.code.com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:632)
... 3 more
The proxy settings that you have entered will work only for HTTP connections. IMAP is a different protocol operating on a different port (993 in this case). If you are behind firewall, your firewall needs to allow connection to the external host:port AND the protocol. You need to request to your Network Administrator for this. Once the settings are in place you will be able to communicate to Gmail Imap server on default/specified port with specified protocol.

Categories

Resources