首页 > 解决方案 > JavaMail work on localhost but not on server

问题描述

I've strange issue with JavaMail, with the following code, all work fine when I run it on localhost, but when I deploy it on server this code fail. The execution stop on:

MimeMessage message = new MimeMessage(session);

The strange thing is that the execution comes freeze on this line, no Exception where launched and code not return or break execution, everything freezes. Where am I wrong?

Here the full code:

package app.util.mail;

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;

import java.util.logging.Logger;

public class MailSender {


    //Destinatari [un indirizzo per ciascuna delle 3 tipologie]
    private static final String INFO_ADDR = "xxx@domain.eu";
    private static final String ADMIN_ADDR = "xxx@domain.eu";
    private static final String ISTITUZ_ADDR = "xxx@domain.eu";

    //Mittente
    private static final String FROM = "postmaster@domain.eu";  
    //PWD di autenticazione mittente
    private static final String PWD = _PWD_;

    //Provider SMTP in Uscita 
    private static final String SMTP_PROVIDER = "smtps.aruba.it";
    private static final String SMTP_PORT = "465";



    public static void send(String type, String subject, String text) throws Exception{
        //Logger log = Logger.getLogger(MailSender.class.getName());

        // Get system properties
        Properties properties = System.getProperties();
        // Setup mail server (smtp protocol)
        properties.put("mail.smtp.host", SMTP_PROVIDER);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true"); 
        properties.put("mail.smtp.port", SMTP_PORT);    
        properties.put("mail.smtp.ssl.trust", "*"); 

        // Get the default Session object.
        Session session = Session.getInstance(properties, new Auth(FROM, PWD) );

        //Destinatario in base al tipo di richiesta
        String to = "";
        switch( type ) {
            case "info":{   to = INFO_ADDR; break;}
            case "admin":{  to = ADMIN_ADDR; break;}
            case "istituz":{    to = ISTITUZ_ADDR; break;}
            default:{ /* lancia errore */ break;}
        }

        //Encapsulation and than send Message
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        //Mittente
        message.setFrom(new InternetAddress(FROM));

       //Destinatario
       message.addRecipient(Message.RecipientType.TO,
                                            new InternetAddress(to));

       //Oggetto del Messaggio
       message.setSubject(" XXX ");


       //Corpo del Messaggio
       message.setText(text);


       Transport.send(message);

}

}

/**
 * Inner-Class for Client Authentication
 */
class Auth extends Authenticator {  

    private static String FROM, PWD;

    protected Auth(String from, String pwd) {
        FROM = from;
        PWD = pwd;
    }

    protected PasswordAuthentication getPasswordAuthentication() {  

        return new PasswordAuthentication(FROM, PWD);  

    }

}

标签: jakarta-mail

解决方案


推荐阅读