我用javax写了一个emailSender。邮件库。我不知道我的错误在哪里,因为在视频中,代码是从哪里工作的。
视频:https://www.youtube.com/watch?v=bkpHQD9e5hc&t=1630s
我也在这个网站上看到了关于这个主题的其他问题,但是没有解决这些问题的方法
代码:
public class MailSender {
protected Session mailSession;
public void login(String smtpHost, String smtpPort, String username, String password) {
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.socketFactory.port", smtpPort);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", smtpPort);
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
this.mailSession = Session.getDefaultInstance(props, auth);
System.out.println("Eingeloggt.");
}
public void send(String senderMail, String senderName, String receiverAddresses, String subject, String message, String username, String password)
throws MessagingException, IllegalStateException, UnsupportedEncodingException {
if (mailSession == null) {
throw new IllegalStateException("Du musst dich zuerst einloggen (login()-Methode)");
}
MimeMessage msg = new MimeMessage(mailSession);
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress(senderMail, senderName));
msg.setReplyTo(InternetAddress.parse(senderMail, false));
msg.setSubject(subject, "UTF-8");
msg.setText(message, "UTF-8");
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiverAddresses, false));
System.out.println("Versende E-Mail...");
Transport.send(msg, username, password);
System.out.println("E-Mail versendet.");
}
}
此错误信息来自本地系统上安装的另一个电子邮件服务器:
如果是"530 SMTP认证是必需的"然后POPcon试图通过IP端口25 (SMTP)到达您的Exchange服务器,但另一个邮件服务器回答此特定的错误消息。您需要从系统中删除hMailServer并重新启动Exchange,或者将两个服务器中的一个移动到不同的本地IP端口,因为它们不能在端口25上共存。您可以在POPcon配置的EXCHANGE配置页面上选择POPcon尝试到达的IP端口。