I'm trying to send a email in Java, I'm using a server proxy.
I've specified the proxy settings like that but doesn't works.
Properties props = System.getProperties();
props.put("http.proxyHost", "192.168.20.2");
props.put("http.proxyPort", "321");
props.put("java.net.useSystemProxies", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(sender));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("asdf#gmail.com"));
message.setSubject("SUBJECT");
message.setText("BODY");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", sender,pass); //exception here!!!
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (MessagingException me) {
me.printStackTrace();
}
And I'm getting this exception because the code doesn't connect to internet.
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
Somebody knows why the program isn't taking the specified proxy?
I assume you're using JavaMail?
If that's the case, you probably are using the wrong properties, there are different ways to configure it depending on your proxy's authentication and JavaMail version you're using: https://javaee.github.io/javamail/FAQ.html#proxy
Related
i have a class that is called "NotifiationService":
#Service("notificacionService")
public class NotificacionServiceImpl implements NotificacionService{
//servicio llama a repositorio
#Autowired
#Qualifier("notificacionService")
private NotificacionService notificacionService;
#Override
public void send(String to, String subject, String text) {
//proxy
Properties p = System.getProperties();
p.setProperty("proxySet","true");
p.setProperty("socksProxyHost","MYPROXY");
p.setProperty("socksProxyPort","MYPORT");
Properties props = new Properties();
// smtp.gmail.com
props.setProperty("mail.smtp.host", "smtp.gmail.com");
// TLS
props.setProperty("mail.smtp.starttls.enable", "false");
// port
props.setProperty("mail.smtp.port","465");
//
props.setProperty("mail.smtp.user", "reciever#gmail.com");
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress("sender#gmail.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(text);
//
Transport t = session.getTransport("smtp");
t.connect("sender#gmail.com","senderpassword");
t.sendMessage(message,message.getAllRecipients());
t.close();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
As you can see i have tried to configure a proxy, since the computer is connected to one that redirects the traffic. So even adding all the specifications about the proxy, it keeps giving me an error saying:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
nested exception is:
java.net.SocketException: Permission denied: connect
I have also tried different ports like: 25,485,587 and none of the respond, so i think its a problem with the proxy.
To be able to find the information about the proxy that is implemented i have typed this command in the console:
reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" | find /i "proxyserver"
and it responds with:
ProxyServer REG_SZ MYPROXY:MYPORT
If i type: "ping google.com" in cmd, it says its inaccessible
Is there a way to be able to connect from java with javamail to gmail and be able to send an email with the current configuration?
Thanks.
If it's not working after setting the SOCKS properties, most likely your proxy server is just a web proxy server and not a SOCKS proxy server. The JavaMail FAQ has pointers to other software that will allow to tunnel connections through a web proxy server.
Since JavaMail 1.6.2, You can set proxy authentication properties for Session object for sending emails.
Refer to the following documentation link.
https://javaee.github.io/javamail/docs/api/
The following properties are introduced newly and works fine with Proxy Authentication (Basic).
mail.smtp.proxy.host
mail.smtp.proxy.port
mail.smtp.proxy.user
mail.smtp.proxy.password
To clarify at least one point concerning the usage of https.proxySet=true and http.proxySet=true.
JDK-4632974 proxySet=false is ignored for HttpURLConnection.getOutputStream
"The use of property http.proxySet was eliminated in favor of simply testing
for the presence of the http.proxyHost property. This property existed up to
JDK1.0.2 but was removed for JDK1.1 in 1996."
In other words, do not use proxySet as it serves no purpose. It is non-existing Java system property nowadays.
The code I am currently using is as follows:
String to = "....#gmail.com";
String from = ".....#gmail.com";
String host = "127.0.0.1";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
If I paste the above code in my Java servlet and run it, the following exception gets thrown:
javax.mail.NoSuchProviderException: smtp
I have also tried following the solutions outlined in these resources but to no avail: link1 link2 link3 link4.
It would help if you included the the full stacktrace for the javax.mail.NoSuchProviderException and JavaMail debug output. Because you are running this in a servlet container you could be running into Bug 6668 -skip unusable Store and Transport classes. Change your call to Transport.set to the following:
final ClassLoader ccl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(Session.class.getClassLoader());
try {
Transport.send(message);
} finally {
Thread.currentThread().setContextClassLoader(ccl);
}
That code will isolate which transport class the code is allowed to see. That will rule out that bug in your code.
NoSuchProviderException means something is messed up in your JavaMail API configuration. Where and how have you installed the JavaMail jar file?
Also, in case it's not clear from the other responses, the only way you're going to be able to send mail without authentication is if you're running your own mail server. You can't do it with general purpose online e-mail services (e.g. Gmail).
First of all, if you wanna use gmail to send emails from your program you need to define stmp host as gmail. Your current host "127.0.0.1" is localhost meaning your own computer? Do you have mail server running on your computer?
Here you can see some tutorial how to send an email using gmail: http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
If you're afraid to pass your normal gmail account details then create some kind of test email like kselvatest or something.
You are missing pretty much those:
final String username = "username#gmail.com";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
So basicly all you need to add is:
1.login and password so JavaMail can use some gmail account
2.declare which mail server you wanna use
change String host = "127.0.0.1"; to String host = "smtp.gmail.com";
3.set mail port
For this code you should get javax.mail.MessagingException: Could not connect to SMTP host: 127.0.0.1, port: 25; exception
But I suspect you have a jre issue. May be smtp implementation has a problem. Why don't you download java mail implementation from http://www.oracle.com/technetwork/java/javamail/index-138643.html and add it to your project class path and try?
I am trying to send an email with javamail api, and then copy it to sent folder.
I am using yahoo mail.
I can send the email, but the copy to sent folder doesn't work.
Here is the code to copy to sent folder :
private void copyIntoSent(Session session,Message msg) throws MessagingException{
Store store = session.getStore("imap");
store.connect("imap.mail.yahoo.com", SMTP_AUTH_USER, SMTP_AUTH_PWD);
Folder folder = (Folder) store.getFolder("Inbox.Sent.Notifications");
if (!folder.exists()) {
folder.create(Folder.HOLDS_MESSAGES);
}
folder.open(Folder.READ_WRITE);
folder.appendMessages(new Message[]{msg});
}
I am getting this exception :
com.sun.mail.util.MailConnectException: Couldn't connect to host,
port: imap.mail.yahoo.com, 143; timeout -1; nested exception is:
java.net.ConnectException: Connexion terminée par expiration du délai
d'attente
I don't know if the problem only comes from my settings of IMAP or if the method to copy to sent folder is wrong.
Thanks in advance.
EDIT : send email code :
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pass);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = "SendAttachment.java";//change accordingly
File f = new File("/path_to_file/test.pdf");
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(f);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
copyIntoSent(session, message);
transport.close();
You need to use SSL with IMAP for Yahoo. Set "mail.imap.ssl.enable" to "true".
Also, since you're using transport.connect explicitly, you shouldn't need to set "mail.smtp.host" and "mail.smtp.user"; and there is no "mail.smtp.password" property so you don't need to set that either.
And you should probably change Sesion.getDefaultInstance to Session.getInstance.
From your exception it seems that your code is trying to connect to port 143 that's wrong use below settings for yahoo.
Incoming Mail (IMAP) Server
Server - imap.mail.yahoo.com
Port - 993
Requires SSL - Yes
And yes, imap allows 2-way synchronizing, which means everything you do remotely is reflected back in your Yahoo Mail account.so i don't think you externally need to move a mail to sent folder by default it will be reflected in sent mails of account
I am trying to send email to gmail using java. I am using this code.
final String username = "xyz#gmail.com";
final String password = "**********";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.port","587");
Session session = Session.getInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("xyz#gmail.com"));
message.setRecipient(Message.RecipientType.TO,newInternetAddress("abcdef#gmail.com"));
message.setSubject("First email to using java");
message.setContent("<h:body style =background-color:white> This is a test mail sent using java" + "</body>","text/html; charset=utf-8");
Transport.send(message);
System.out.println("Message Sent");
But When i run the above code it shows follwoing error:
Exception in thread "main" com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
I have an Internet connection which uses proxy server and requires authentication. Is this error because of Proxy or there is some problem in my code. Please tell me how to resolve it.
JavaMail can't use a web proxy server directly, although it can use a SOCKS proxy server. If you only have a web proxy server, programs like Corkscrew can help.
You can setup SOCKS proxy server in JavaMail with like this:
Properties p = System.getProperties();
p.setProperty("proxySet","true");
p.setProperty("socksProxyHost","192.168.1.1");
p.setProperty("socksProxyPort","1234");
Yes, it's because of your proxy server.
How do I configure JavaMail to work through my proxy server?
the above link expired: check this link
http://www.oracle.com/technetwork/java/faq-135477.html#proxy
I am using this code in my java GWT application
public String greetServer(String input) throws Exception {
try{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.host", "smtp.random.com");
props.setProperty("mail.user", "foo#bar.com");
props.setProperty("mail.password", "000000000");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("hello");
message.setContent("helloo sss", "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress("junaidp#gmail.com"));
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
} catch(NoSuchProviderException e){
throw new Exception(e);
}
return input;
}
Error: javax.mail.MessagingException: Could not connect to SMTP host: smtp.random.com, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
if i use
props.setProperty("mail.host", "smtp.live.com");
and use my hotmail account , it gives this error
javax.mail.MessagingException: can't determine local email address
Any idea what could be the solution
thanks
I just used Simple Java Mail in a GWT project. You might want to try it. It's very simple to configure.
There are lots of example there, including one of sending using gmail's SMTP server using for example TLS:
Email email = new Email.Builder()
.from("Michel Baker", "m.baker#mbakery.com")
.to("mom", "jean.baker#hotmail.com")
.to("dad", "StevenOakly1963#hotmail.com")
.subject("My Bakery is finally open!")
.text("Mom, Dad. We did the opening ceremony of our bakery!!!")
.build();
new Mailer("smtp.gmail.com", 25, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 587, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 465, "your user", "your password", TransportStrategy.SMTP_SSL).sendMail(email);
If you have two-factor login turned on, you need to generate an application specific password from your Google account.
Here's some Gmail settings which work for me:
//these are fed into the Properties object below:
mail.smtp.starttls.enable = true
mail.transport.protocol = smtp
mail.smtp.auth = true
and some Java:
Properties properties = ...
javax.mail.Session session = javax.mail.Session.getInstance(properties, null);
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", username, password);
Error: javax.mail.MessagingException: Could not connect
to SMTP host: smtp.random.com port: 25;
nested exception is: java.net.ConnectException: Connection refused: connect
This error means that your SMTP server you provided is invalid. The code you have is correct, but smtp.random.com must not be a valid SMTP server.
I suggest you look into using Google's SMTP server that is free provided you use a valid gmail account.
Refer to this page for more information on using Gmail's STMP server: http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm
This tutorial worked for me in the past