Vaadin, Tomcat and Javamail

I’m trying to follow this tutorial about sending Emails in Vaadin
https://vaadin.com/wiki/-/wiki/Main/Sending+Email+from+Java+Applications

The thing is, I have a program in a regular Java application that works:

import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.mail.Session;

import java.text.SimpleDateFormat;

public class SendEmail {
    public static void main(String[] args) {
          //Creates a connection with the Exchange Server.
        String smtpHostServer = "MSExchangeServer";

        Properties props = System.getProperties();
        props.put("mail.smtp.host", smtpHostServer);
        props.put("mail.smtp.auth", "false");
        props.put("mail.smtp.socketFactory.port", "25");
        props.put("java.net.preferIPv4Stack","True");
        Session session = Session.getInstance(props, null);

        String todayStr = new SimpleDateFormat("MM-dd-yyyy").format(new Date());
        
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 14);
        Date d = c.getTime();
        String dateStr = new SimpleDateFormat("MM/dd/yyyy").format(d);
        
        SendEmailUtility.sendEmail(session, "email@host.com", "Test <b>Email</b>");
        
    }
    
}

and

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendEmailUtility {
    public static void sendEmail(Session session, String toEmail, String subject, String body){   
        try        
        {          
            //Create a default MimeMessage object.
         Message message = new MimeMessage(session);
    
         // Set From: header field of the header.
         message.setFrom(new InternetAddress("blah@test.com"));
    
         // Set To: header field of the header.
         message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toEmail));
    
         // Set Subject: header field
         message.setSubject(subject);
    
         // This mail has 2 part, the BODY and the embedded image
         MimeMultipart multipart = new MimeMultipart("related");
    
         // first part (the html)
         BodyPart messageBodyPart = new MimeBodyPart();
         String htmlText = "<img src=\"cid:image\"><p>"+body;
         messageBodyPart.setContent(htmlText, "text/html");
         // add it
         multipart.addBodyPart(messageBodyPart);
    
         // second part (the image)
         messageBodyPart = new MimeBodyPart();
         String fdsImg;
        fdsImg = "c:\download.jpg";
         
         DataSource fds = new FileDataSource(fdsImg);

         messageBodyPart.setDataHandler(new DataHandler(fds));
         messageBodyPart.setHeader("Content-ID", "<image>");
    
         // add image to the multipart
         multipart.addBodyPart(messageBodyPart);
    
         // put everything together
         message.setContent(multipart);
         // Send message
         Transport.send(message); [b]
//ERROR HAPPENS HERE
[/b] ON TOMCAT
        }
        catch (Exception e) {          
            e.printStackTrace();        
        }    
    }
}

This works in a regular application, but when i try to use it in Tomcat, I get this Network is Unreachable: Connect error:

I read on stackoverflow, and apparently I need the java mail jar in the Tomcat Library folder, so I downloaded and added that, but still no help.

The only other thing I can think of is that this doesn’t support a microsoft exchange server?

Is tomcat installed on the same machine that correctly runs the SendEmail java app?

Yes, but I think I need to explain clearer, the SendEmail app works when it’s
Run as an Application
But it doesn’t work when running on a server, like so:

btnSendEmail.addClickListener(new ClickListener(){

        @Override
        public void buttonClick(ClickEvent event) {
            try {
                String smtpHostServer = "exchangeserver";

                Properties props = System.getProperties();
                props.put("mail.smtp.host", smtpHostServer);
                props.put("mail.smtp.auth", "false");
                props.put("mail.smtp.socketFactory.port", "25");
                props.put("java.net.preferIPv4Stack","True");
                Session session = Session.getInstance(props, null);

                String todayStr = new SimpleDateFormat("MM-dd-yyyy").format(new Date());
                
                Calendar c = Calendar.getInstance();
                c.add(Calendar.DAY_OF_MONTH, 14);
                Date d = c.getTime();
                String dateStr = new SimpleDateFormat("MM/dd/yyyy").format(d);
                
                SendEmailUtility.sendEmail(session, "blah@test.com", "test <b>email");
                

            } catch (Exception e) {
                e.printStackTrace();
                Notification.show("Error sending the email", Notification.Type.ERROR_MESSAGE);
    }
        }
           
       });
       
       
       layout.addComponent(btnSendEmail);
       

I get an error that it can’t connect. Is it because the tomcat server on my computer (localhost) doesn’t have access to the network?

It seems so. Have you tried to ping the hostname you want to connect?

From my computer, it works.

From within the tomcatserver? I’m not sure how to send a ping to the hostname from within tomcat.

Sorry, i don’t have understand. Is tomcat running on your computer or in another computer?

My computer.

Like it’s just a folder on my computer like this:

That’s weird. Just to be sure try to set the property java.net.preferIPv4Stack as true (all lowercase) instead of True

Still socket error, maybe I need to configure Tomcat some special way to reach the email server?

Try adding
-Djava.net.preferIPv4Stack=true
in CATALINA_OPTS or JAVA_OPTS env variable in tomcat startup script (or in setenv.sh or setenv.bat if you are on windows)

Hi,

Check also your PC’s firewall and antivirus. Tomcat is probably running on System account (as a service), so not running with your user like the run-app profile.

Also could it be Exchange server having some access rules ?
See : https://technet.microsoft.com/en-us/library/dd277329.aspx

In the stacktrace you posted, you seems to call some quite long server name. Are you having same results using IP address instead ?

Have you multiple network interfaces on this machine ? Is Tomcat binding to the good one ?
See : http://wiki.apache.org/tomcat/FAQ/Connectors#Q6

Regards