JavaMail可在localhost上运行,但不能在服务器上运行

时间:2019-03-07 16:30:43

标签: javamail

我对JavaMail有一个奇怪的问题,使用以下代码,当我在localhost上运行它时,它们都可以正常工作,但是当我将其部署在服务器上时,此代码将失败。 执行停止于:

MimeMessage message = new MimeMessage(session);

奇怪的是,执行冻结在这一行,启动时没有异常,代码没有返回或中断执行,一切都冻结了。 我在哪里错了?

完整代码如下:

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);  

    }

}

0 个答案:

没有答案