Javamail send message from 2 smtp servers

SMTP is internet standard for sending and receiving Email across IP networks it uses TCP with most popularly port 25 to send and receive. SSL ...

Javamail send message from 2 smtp servers

I have a problem and need some help

In my program I need to send a mail and smtp server depends of one var, if var is 1 then must send mail from a gmail address or if var is 2 the mail is send from a diferent smtp server

I use TLS from both accounts and all is ok (if only send from 1 server)... but when I try to send from both servers (because my var change depend from 1 to 2, or 2 to 1 many times during the execution) I recieved always an error

Here my code:

public static void correo(String empresa, String[] dest, String archivo, String nom_arch){
    String cuenta = "", asunto = "", pw = "", pto = "", server = "";
    try
    {
        Properties props = new Properties();

        if("1".equals(empresa)){
            cuenta = "example@gmail.com";
            asunto = "xxxxxxx";
            pw = "password";
            pto = "587";
            server = "smtp.gmail.com";
        }
        else if ("2".equals(empresa)) {
            cuenta = "example@server.com";
            asunto = "yyyyyyy";
            pw = "password";
            pto = "25";
            server = "smtpout.secureserver.net";
        }

        props.setProperty("mail.smtp.host", server);
        props.setProperty("mail.smtp.port", pto);
        props.setProperty("mail.smtp.user", cuenta);
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.starttls.enable", "true");

        Session session = Session.getDefaultInstance(props,null);

        BodyPart texto = new MimeBodyPart();
        texto.setText("Some Text");

        BodyPart adjuntoPDF = new MimeBodyPart();
        adjuntoPDF.setDataHandler(new DataHandler(new FileDataSource(archivo+".pdf")));
        adjuntoPDF.setFileName(nom_arch+".pdf");

        MimeMultipart multiparte = new MimeMultipart();
        multiparte.addBodyPart(texto);
        multiparte.addBodyPart(adjuntoPDF);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(cuenta));

        InternetAddress[] direcciones = new InternetAddress[dest.length];
        for(int i=0; i<dest.length; i++){
            direcciones[i] = new InternetAddress(dest[i]);
        }
        message.addRecipients(Message.RecipientType.TO,direcciones);
        message.setSubject(asunto);
        message.setContent(multiparte);

        Transport t = session.getTransport("smtp");
        t.connect(cuenta, pw);
        t.sendMessage(message, message.getAllRecipients());

        t.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Click Here to Visit

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0