繁体   English   中英

如何使用 javamail 和 postfix/dovecot 发送邮件

[英]how to send mail with javamail and postfix/dovecot

嘿,我最近一直在尝试使用我的 postfix/dovecot 服务器使用 javamail 发送邮件。

首先,我使用本教程设置我的邮件服务器: https : //upcloud.com/community/tutorials/secure-postfix-using-lets-encrypt/

我可以在本地机器上正常发送和接收邮件,但在 java 中使用此代码:

String to = "somemail@gmail.com";
String from = "somemail@example.com";
final String username = "somemail@example.com"; // 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);
}

这是堆栈跟踪: https : //pastebin.com/FTHmrQ2T (无法发布,因为它看起来像垃圾邮件),这是 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

有人知道发生了什么/如何解决吗? 我真的需要这方面的帮助:/谢谢^^

我找到了解决方案! 您只需要在“/etc/postfix/master.cf”中取消注释“#submission inet n – n – – smtpd”! 现在它工作得很好!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM