I am trying to send mail using java mail api..Every thing is ok in the code except the Authenticator class .It is giving warning as ...
Constructor PasswordAuthentication can not be applied to given types.
required java.lang.String,java.lang.char[]
Here is my code snippet where i am getting warning error but not able to resolve the issue...
Authenticator auth = new Authenticator() {
#Override
public PasswordAuthentication getPasswordAuthentication() {
error return new PasswordAuthentication(userName, password);
}
};
error Session session = Session.getInstance(properties, auth);
These two lines with //error is giving error in the code..
Please help me.
Thanks in advance..
PasswordAuthentication constructor only accept a String and a char array as arguments. So you should do :
return new PasswordAuthentication(userName, password.toCharArray());
Edit :
The problem is that Session.getInstance(java.util.Properties props, Authenticator authenticator) requires an Authentificator object from the javax.mail package.
I think you've imported the wrong package. It should be javax.mail.Authenticator and not java.net.Authenticator
So you should use the object PasswordAuthentication from the javax.mail package (which accept two Strings as argument), instead of the object PasswordAuthentification from the java.net package (which accept a String and a char array).
when you call the constructor PasswordAuthentication(String a, char[] b)
the Exception is telling you that you are passing a wrong type in the parameters, for example:
your code: return new PasswordAuthentication(userName, password);
userName or password are wrong type, maybe userName is not a String or password is not a char[], take a look carefully.
Try
Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
If you are using gmail account, you must disable the two step authentication or you can either set on your gmail account app password and use the app password instead in your application.
https://support.google.com/accounts/answer/185833?hl=en
Related
My Java Windows app needs to send email and allow the user to specify his/her email account credentials: host, port, username, password. It works when I use my credentials for an account at my hosting service, but not so well when using a Gmail account, as my prospective users might want to do. The problem is Gmail insists on an "App password." So, I follow the Google instructions and create a 16-character App password which Google says has to be used one time only. However, I find that the App password MUST be used for subsequent runs.
Here's a sample program that demonstrates the problem. It will fail if I use that actual password for the Gmail account. It will work after I've created an App/device specific password (16 characters) and used it as the password, as it should. But, I have to use that 16-character password thereafter as well. What am I missing?
public class SendEmailTLS {
// Example 1 from https://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
public static void main(String[] args) {
final String username = "me123#gmail.com";
final String password = "mypassword";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true"); //TLS
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("me123#gmail.com"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse("you456#hotmail.com")
);
message.setSubject("Testing Gmail TLS");
message.setText("Dear sir,"
+ "\n\n I'm testing TLS,using port 587");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
I want to add that I'm now suspecting that what I'm seeing is the way it's supposed to work. I had been thinking the 16-char App specific pw was a "Salt" kind of thing to protect the Gmail server. I have now read that it's really just an alternative password for my Gmail account. If anyone can confirm or deny this new perspective, I would be grateful.
I am using openjdk . For porxy authentication ,I am using Authenticator but for my first Request as HTTPS the authenticators doesn't authenticates and throws error. After connecting through HTTP , HTTPS working fine.
I have tried with setting system property, jdk.http.auth.tunneling.disabledSchemes="" and jdk.http.auth.proxying.disabledSchemes="".
private static void setProxy(String proxyHostName,int proxyport){
proxy=new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyHostName,proxyport));
}
private static void setProxy(String proxyHostName,int proxyport,String username,String password){
setProxy(proxyHostName,proxyport);
if (username!=null && password!=null) {
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
System.setProperty("jdk.http.auth.proxying.disabledSchemes", "");
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication(username, password.toCharArray()));
}
};
Authenticator.setDefault(authenticator);
}
}
After setting this property in my java_opts in .sh file . It worked fine for me.
Orelse Anyone want to set in their java code means set it in start method.
I need to read some data from HTTPS page that has a local certificate and requires a username/password authentication.
Using "HttpURLConnection", is it possible to do username/password authentication and import the local certificate or ignore it?
Many Thanks,
try to use this. Work for me.
private void authenticadorRedeTendencia() {
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName(), password.toCharArray());
}
});
}
I am sending my credentials using,
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password".toCharArray());
}
});
My question is now how can I retrieve this credentials in Soap webservice Java?
Got solution,
ArrayList<String> authList = (ArrayList<String>) http_headers.get("Authorization");
It returns Credentials in Base64.
I want to create my DbxRequestConfig Object with a StandardHttpRequestor, because I need it to use a Proxy.
The Proxy is a http Proxy, Port 80, and needs authentication.
Proxyaddress: http://myproxy.com
Proxyport: 80
Proxyusername: username
Proxypassword: password
So I tried to use the global Java Proxy setup:
System.setProperty("http.proxy","proxyaddress") //... http.proxyUser, http.ProxyPassword
//and so on
It did not work.
After looking into the StandardHttpRequestor I realized I need to use this Object as well as a Proyx Object:
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));
StandardHttpRequestor requ = new StandardHttpRequestor(proxy);
Which is wrong, because it has no authentication.
For authentication, the net and google show me the following. Putting all together, my current code looks like the following:
String ip = "http://myproxy.com";
int port = 80;
final String authUser = "username";
final String authPassword = "password";
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(authUser, authPassword.toCharArray());
}
});
System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));
StandardHttpRequestor requ = new StandardHttpRequestor(proxy);
return requ;
But this does not work as well.
What am I doing wrong?
I can't seem to get the Proxy to work.
One problem was the http:// in String ip = "http://myproxy.com";
My current code looks the following, and works sometimes. Sometimes not. I have no idea why. Sometimes I have to reallow the App to be connected to my DropBox Account, because the authKey doesn't come through the proxy...
Well at least I got an example working for you guys having the same trouble. Maybe the rest of the problem is on the proxy side? I'll have a further look into this. But here comes my code:
public HttpRequestor getProxy(){
if("true".equals(config.getProperty("proxy","false"))){
String ip = "proxy.myproxy.com";
int port = 80;
final String authUser = "username";
final String authPassword = "password";
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(authUser, authPassword.toCharArray());
}
});
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(ip,port));
HttpRequestor req = new StandardHttpRequestor(proxy);
return req;
}
return null;
}
As you can see I don't use the StandardHttpRequestor anymore. For the Dropbox code it is the following:
HttpRequestor requ = con.getProxy();
if(requ!=null)
config = new DbxRequestConfig(APP_NAME, Locale.getDefault().toString(),requ);
else
config = new DbxRequestConfig(APP_NAME, Locale.getDefault().toString());
As I already said, sometimes it works. Sometimes not. I'm going to write more info about that as soon as I know if it's because of the code or because of the proxy itself.