I've setup javamail to run through a socks proxy with ssl, but it won't go through it. I tried setting both mail.smtp.socks.host and mail.smtp.socks.port but it keeps going direct to the mail host. Here's my properties:
smtpProps = new Properties();
smtpProps.setProperty("mail.smtp.host", sendHost);
smtpProps.setProperty("mail.smtp.port", String.valueOf(sendPort));
smtpProps.setProperty("mail.transport.protocol", "smtps");
smtpProps.setProperty("mail.smtp.ssl.enable", "true");
smtpProps.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
smtpProps.setProperty("mail.smtp.socketFactory.fallback", "true");
smtpProps.setProperty("mail.smtp.auth", "true");
smtpProps.setProperty("mail.smtp.connectiontimeout", String.valueOf(sendConnTimeout) );
smtpProps.setProperty("mail.smtp.writetimeout", String.valueOf(sendTimeout) );
smtpProps.setProperty("mail.smtp.socks.host", proxyHost);
smtpProps.setProperty("mail.smtp.socks.port", String.valueOf(proxyPort));
smtpProps.setProperty("mail.debug", "true");
smtpProps.setProperty("mail.socket.debug", "true");
I've tried all smtps property name combination without any success. I'm using Java 7 and javamail 1.4.7.
The only one workaround I found for now is to use a custom version of SSL socket factory
smtpProps.setProperty("mail.smtp.socketFactory.class",
proxied?
"my.socks.ProxiedSSLSocketFactory" :
"javax.net.ssl.SSLSocketFactory");
`
obviously I can't use system properties socksProxyHost & socksProxyPort.
Any ideas?
Related
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.
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);
}
});
One of my customers is using Gmail for business (part of Google Apps) and I had to reconfigure the website I've developed so it would match the new credentials. After a bit of struggle due to TLS errors, I've managed to make the code work on localhost and on my test server (both Apache Tomcat 5.5). Everything was going smooth until I had to move it on his server (another Tomcat 5.5 as the hosting company told me). On the client's server I get the following error:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.io.IOException: Couldn't connect using "javax.net.ssl.SSLSocketFactory" socket factory to host, port: smtp.gmail.com, 465; Exception: java.lang.reflect.InvocationTargetException
The strange thing is that on localhost and the test server the port 465 works fine, and the guys from hosting said that port is opened on their server.
The code that connects to the mailserver is:
private void initConfigParams() throws CMSGeneralException {
try {
props = System.getProperties();
String smtpHost = Messages.getDBConfString("mail.smtp.host");
String mailPort = Messages.getDBConfString("mail.smtp.port");
String socketFallback = Messages.getDBConfString("mail.smtp.socketFactory.fallback");
String enableTls = Messages.getDBConfString("mail.smtp.starttls.enable");
String authSmtp = Messages.getDBConfString("mail.smtp.auth");
String tlsRequired = Messages.getDBConfString("mail.smtp.stattls.required");
String sktFactory = Messages.getDBConfString("mail.smtp.socketFactory.class");
props.put("mail.smtp.starttls.enable", enableTls);
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", authSmtp);
props.put("mail.smtp.starttls.required", tlsRequired);
props.setProperty("mail.smtp.socketFactory.class", sktFactory);
props.setProperty("mail.smtp.socketFactory.fallback", socketFallback);
props.setProperty("mail.smtp.port", mailPort);
props.setProperty("mail.smtp.socketFactory.port", mailPort);
props.put("mail.transport.protocol", Messages.getDBConfString("mail.transport.protocol"));
Authenticator auth = null;
userName = Messages.getDBConfString("mail.username");
userPassword = Messages.getDBConfString("mail.userpassword");
if (!CMSUtils.isEmptyString(userName) && !CMSUtils.isEmptyString(userPassword)){
/* props.put("mail.smtp.auth", "true"); */
auth = new SMTPAuthenticator(userName, userPassword);
}
session = Session.getDefaultInstance(props, auth);
session.setDebug(false);
address = new InternetAddress[1];
address[0] = new InternetAddress(recipients);
mbText = new MimeBodyPart();
mbText.setContent(text, "text/html");
mp = new MimeMultipart();
mp.addBodyPart(mbText);
} catch (MessagingException e) {
e.printStackTrace();
throw new CMSGeneralException();
}
}
With the following .properties file
mail.smtp.starttls.enable=true
mail.smtp.host=smtp.gmail.com
mail.smtp.auth=true
mail.smtp.starttls.required=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
mail.smtp.port=465
mail.transport.protocol=smtps
mail.username=website#example.com
mail.userpassword=password
mail.from=website#example.com
mail.to=mail#example.com
I tried the other ports for GMail, 587, but it doesn't work on any of the servers. Not even port 25 won't do the trick.
So, what am I doing wrong, and what should I do to make the mailing work?
Get rid of all the socket factory properties; if you're using a reasonably recent version of JavaMail you don't need them. See the JavaMail FAQ for how to configure JavaMail to access Gmail. You'll also find debugging tips there if it still doesn't work.
Also, change Session.getDefaultInstance to Session.getInstance.
And finally, if you're setting "mail.transport.protocol" to "smtps", you need to set the other properties as "mail.smtps." properties, not "mail.smtp." properties.
It seems to you have problems with java.lang.reflect.InvocationTargetException. So, I mean it's not a network problem. May be you've specified some parameters wrong and JavaMail API routes couldn't invoke a method. May be you should also specify property mail.smtp.ssl.socketFactory.
Some documentation here http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html .
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.
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);