Reputation: 29
I was trying to send a mail from java but I am getting the same error in every case I tried. I'm using windows 8 and before i had problems with sockets, but i run this code in a virtual machine with xp and it send me the same mistake! I read that it could be my jdk,firewall, antivirus or network problems but i already tried that and i have no solution. I hope someone could tell whats happening here! I'll leave the code here:
package mail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Mail
{ // SE DECLARAN LAS VARIABLES DE CORREO , DOMINIO, PUERTO Y PASS
private static final String SMTP_HOST_NAME = "smtp.gmail.trust";
private static final int SMTP_HOST_PORT = 587;
private static final String SMTP_AUTH_USER = "[email protected]";
private static final String SMTP_AUTH_PWD = "password";
public static void main(String[] args) throws Exception {
new Mail().test();
}
public void test() throws Exception {
Properties props = new Properties();`
props.put("mail.transport.protocol", "smtps");`
props.put("mail.smtps.trust", SMTP_HOST_NAME);`
props.put("mail.smtps.auth", "true");`
props.put("mail.smtps.quitwait", "false");`
props.setProperty("mail.smtp.host", "stmp.gmail.com");`
props.setProperty("mail.smtp.starttls.enable", "true");`
props.setProperty("mail.smtp.port", "587");`
props.setProperty("mail.smtp.user", "[email protected]");`
props.setProperty("mai.smtp.auth", "true");`
Session mailSession = Session.getDefaultInstance(props);`
mailSession.setDebug(true); Transport transport = mailSession.getTransport();`
MimeMessage message = new MimeMessage(mailSession); `
message.setSubject("Testing SMTP-SSL");`
// Aqui VA EL TITULO DEL EMAIL
message.setContent("Este correo ha sido enviado desde Netbeans con Java", "text/plain");
// Aqui VA EL CONTENIDO,,
message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
// DESTINARIO
transport.connect (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
The output is:
DEBUG: setDebug: JavaMail version 1.5.0
DEBUG: getProvider() returning
javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.trust", port 587, isSSL true
Exception in thread "main" com.sun.mail.util.MailConnectException: Couldn't connect to
host, port: smtp.gmail.trust, 587; timeout -1;
nested exception is:
java.net.UnknownHostException: smtp.gmail.trust
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:345)
at mail.Mail.test(Mail.java:44)
at mail.Mail.main(Mail.java:17)
Caused by: java.net.UnknownHostException: smtp.gmail.trust
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:176)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 4 more
Java Result: 1
Thank you, and greetings from Costa Rica.
Upvotes: 0
Views: 2395
Reputation: 3390
The issue can be "smtp.gmail.trust". Did you tried with "smtp.gmail.com"?
To send mail using Gmail SMTP, try below code, it works for me:
public void sendMail(final String senderEmailID, final String password, javax.mail.Address [] addresses) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = null;
try {
session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(senderEmailID, password);
}
});
} catch (Exception e) {
e.printStackTrace();
return;
}
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(senderEmailID));
message.setRecipients(Message.RecipientType.TO, addresses);
message.setSubject("Subject");
String messageBody = "<h1>Message</h1>";
message.setContent(messageBody.toString(), "text/html");
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 1
Reputation: 11543
If you read the exception it says UnknownHostException: smtp.gmail.trust
. It can not find the host smtp.gmail.trust. You probably meant smtp.gmail.com.
Also, look though your code, you have several mistakes when setting your properties.
Upvotes: 1