3
package jmail;

import java.util.Date; import java.util.Properties; 
import javax.mail.Authenticator; import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage;

public class HtmlJavaSend {

public void sendHtmlEmail(String host, String port,
        final String userName, final String password, String toAddress,
        String subject, String message) throws AddressException,
        MessagingException {

    // sets SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.man.com", host);
    properties.put("mail.25", port);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.ssl.trust","mail.man.com");

    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    };

    Session session = Session.getInstance(properties, auth);

    // creates a new e-mail message
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = {new InternetAddress(toAddress)};
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    // set plain text message
    msg.setContent(message, "text/html");

    // sends the e-mail
    Transport.send(msg);

}

public static void main(String[] args) {
    // SMTP server information
    String host = "mail.man.com";
    String port = "25";
    String mailFrom = "admin@man.com";
    String password = "Man";

    // outgoing message information
    String mailTo = "ji@man.com";
    String subject = "Hello my friend";

    // message contains HTML markups
    String message = "<i>Greetings!</i><br>";
    message += "<b>Wish you a nice day!</b><br>";
    message += "<font color=red>Duke</font>";

    HtmlJavaSend mailer = new HtmlJavaSend();

    try {
        mailer.sendHtmlEmail(host, port, mailFrom, password, mailTo,
                subject, message);
        System.out.println("Email sent.");
    } catch (Exception ex) {
        System.out.println("Failed to sent email.");
        ex.printStackTrace();
    }
} }

The error is:

eror run: Failed to sent email. com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1; nested exception is: java.net.ConnectException: Connection refused: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699) at javax.mail.Service.connect(Service.java:388) at javax.mail.Service.connect(Service.java:246) at javax.mail.Service.connect(Service.java:195) at javax.mail.Transport.send0(Transport.java:254) at javax.mail.Transport.send(Transport.java:124) at jmail.HtmlJavaSend.sendHtmlEmail(HtmlJavaSend.java:62) at jmail.HtmlJavaSend.main(HtmlJavaSend.java:85) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 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:331) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066) ... 8 more BUILD SUCCESSFUL (total time: 1 second)

Angel Cuenca
  • 1,637
  • 6
  • 24
  • 46
ips
  • 51
  • 1
  • 1
  • 3
  • error which i get is – ips Sep 21 '16 at 10:03
  • you only put a lot of code, add some description for your question. – DimaSan Sep 21 '16 at 10:05
  • this is my error Failed to sent email. com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1; nested exception is: java.net.ConnectException: Connection refused: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100) – ips Sep 21 '16 at 10:10
  • Make up your mind. The stack trace you posted shows `java.io.IOException: Server is not trusted:`, which can only happen *after* a successful TCP connect; and there is no such message as 'connection refused: connect timeout'. – user207421 Sep 21 '16 at 11:59
  • i corrected the stack – ips Sep 21 '16 at 15:26

2 Answers2

4

You have error here:

properties.put("mail.man.com", host);
properties.put("25", port);

It should be:

properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
porgo
  • 1,729
  • 2
  • 17
  • 26
  • I am trying to use html content in the email not only just text. I was able to send just text format but apparently html format is giving me issues Do i copy paste exactly? properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", port); Because i tried krzosik i get error com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1; nested exception is: java.net.ConnectException: Connection refused: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.jav‌​a:2100) – ips Sep 21 '16 at 13:21
  • I can confirm that the port 25 is open . I ran a telnet session via port 25 for , mail.man.com, and ip. They all returned a 202 service ready message. So I do not believe it is ports or firewall access that is the reason for the refused connection. – ips Sep 21 '16 at 15:28
1

According to:

properties.put("mail.25", port);

you are connecting to the port offering plaintext SMTP. This is backed by your observation to receive 202 service ready message message when using telnet. But you are also setting properties to use TLS:

properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl.trust","mail.man.com");

If you use port 25 just don't enable TLS, or, use port 465 (standard port for SMTP with TLS) with TLS enabled.

mottek
  • 929
  • 5
  • 12