我需要使用 JavaMail 发送简单的 html 消息。而当我试图在网上找一些有解释的好例子时,下一个例子都让我更加愤怒和愤怒。
所有这些愚蠢的示例都包含复制和粘贴的 Java 代码,它们的区别仅在于注释和一个很好的免责声明,即首先您应该配置您的 smtp 和 pop3 服务器。
我知道没有人愿意为某些具体产品做广告,但配置服务器是最难的部分。那么,谁能给我一些关于配置具体服务器(例如 Kerio 或任何其他服务器)的非常有用的信息(没有 java 代码)?
我现在拥有的是下一个例外:
250 2.0.0 Reset state
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Relaying to <mymail@mycompany.com> denied (authentication required)
UPD。对前面所有文本的简单重新表述是:想象你有 Windows、jdk,没有别的。你想制作java程序并在你的机器上运行它。这个程序应该发送“Hello world!” 到您的 gmail 帐户。列出你的步骤。
UPD2。这是代码:
Properties props = new Properties ();
props.setProperty ("mail.transport.protocol", "smtp");
props.setProperty ("mail.host", "smtp.gmail.com");
props.setProperty ("mail.user", "my_real_address_1@gmail.com");
props.setProperty ("mail.password", "password_from_email_above");
Session mailSession = Session.getDefaultInstance (props, null);
mailSession.setDebug (true);
Transport transport = mailSession.getTransport ();
MimeMessage message = new MimeMessage (mailSession);
message.setSubject ("HTML mail with images");
message.setFrom (new InternetAddress ("my_real_address_1@gmail.com"));
message.setContent ("<h1>Hello world</h1>", "text/html");
message.addRecipient (Message.RecipientType.TO,
new InternetAddress ("my_real_address_2@gmail.com"));
transport.connect ();
transport.sendMessage (message,
message.getRecipients (Message.RecipientType.TO));
例外是:
RSET
250 2.1.5 Flushed 3sm23455365fge.10
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 3sm23455365fge.10
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
at com.teamdev.imgmail.MailSender.main(MailSender.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
...