Acumbamail | Documentación SMTP

Documentación SMTP

Accede a toda la documentación de nuestro servidor SMTP

Java

Para enviar emails a través de nuestro servidor SMTP utilizando el lenguaje de programación Java puedes hacerlo siguiendo este ejemplo. Recuerda sustituir LOGIN_ACUMBAMAIL y PASSWORD_ACUMBAMAIL por las que correspondan en tu caso.


import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;

public class test1 {

    private static final String SMTP_HOST_NAME = "smtp.acumbamail.com";
    private static final String SMTP_AUTH_USER = "LOGIN_ACUMBAMAIL";
    private static final String SMTP_AUTH_PWD  = "PASSWORD_ACUMBAMAIL";

    public static void main(String[] args) throws Exception{
       new test1().test();
    }

    public void test() throws Exception{
        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.port", 25);
        props.put("mail.smtp.auth", "true");
        //props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

        Authenticator auth = new SMTPAuthenticator();
        Session mailSession = Session.getDefaultInstance(props, auth);
        // uncomment for debugging infos to stdout
        // mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);

        Multipart multipart = new MimeMultipart("alternative");

        BodyPart part1 = new MimeBodyPart();
        part1.setText("Test");

        BodyPart part2 = new MimeBodyPart();
        part2.setContent("Test", "text/html; charset=\"utf-8\"");

        multipart.addBodyPart(part1);
        multipart.addBodyPart(part2);
        message.setHeader("Content-Type","text/html; charset=\"utf-8\""); 
        message.setContent(multipart,"text/alternative");
        message.setHeader("Content-Transfer-Encoding", "8bit");
        message.setFrom(new InternetAddress("EMAILFROM@EXAMPLE.COM"));
        message.setSubject("E desde java ;)");
        message.addRecipient(Message.RecipientType.TO,new InternetAddress("EMAIL_TO1@EXAMPLE.COM"));
        //message.addRecipient(Message.RecipientType.CC, new InternetAddress("EMAIL_TO2@EXAMPLE.COM"));
        transport.connect();
        transport.sendMessage(message,
            message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
           String username = SMTP_AUTH_USER;
           String password = SMTP_AUTH_PWD;
           return new PasswordAuthentication(username, password);
        }
    }
}