Search code examples
javaemailsmtppostfix-mtadovecot

how to send mail with javamail and postfix/dovecot


Hey I've recently been trying to send mail with javamail with my postfix/dovecot server.

First I set up my mailserver using this tutorial: https://upcloud.com/community/tutorials/secure-postfix-using-lets-encrypt/

I can send and recieve mail fine on the local machine but using this code in java:

String to = "[email protected]";
String from = "[email protected]";
final String username = "[email protected]"; // somemail does the same
final String password = "password";

String host = "mail.example.com";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true"); // same output with mail.smtp.ssl.enable
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", 25);
props.put("mail.smtp.user", username);
props.put("mail.smtp.password", password);

Session session = Session.getInstance(props, new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(username, password);
    }
});

session.setDebug(true);
try {
    Message message = new MimeMessage(session);

    message.setFrom(new InternetAddress(from));

    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

    message.setSubject("Testing Subject");

    message.setText("Hello, this is sample for to check send " + "email using JavaMailAPI ");
    Transport.send(message);

    System.out.println("Sent message successfully....");

} catch (MessagingException e) {
    throw new RuntimeException(e);
}

Here is the stack trace : https://pastebin.com/FTHmrQ2T ( wasn't able to post it because it looked like spam) and here is the nmap:

PORT    STATE SERVICE
22/tcp  open  ssh
25/tcp  open  smtp
110/tcp open  pop3
143/tcp open  imap
993/tcp open  imaps
995/tcp open  pop3s

Does someone have any idea what's happening / how to fix it ? I really need help on this one :/ Thanks ^^


Solution

  • I found the solution ! You just need to uncomment "#submission inet n – n – – smtpd" in "/etc/postfix/master.cf" ! Now it works just fine !