【发布时间】:2018-07-02 01:15:52
【问题描述】:
如何从域的地址发送一封信?我就是不明白。它显示
DEBUG: getProvider() returning
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.yandex.ru", port 995, isSSL false
无法连接到主机,但端口是正确的。 从域发送电子邮件的属性应该是什么? 我的是:
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");//Enable tls session
props.put("mail.smtp.auth", "true");//Enable authentication
props.put("mail.smtp.host", "smtp.yandex.ru");//Server's host
props.put("mail.smtp.port", "995");//Server's port
我有 [something]@domain.ru
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");//Enable tls session
props.put("mail.smtp.auth", "true");//Enable authentication
props.put("mail.smtp.host", "smtp.yandex.ru");//Server's host
props.put("mail.smtp.port", "995");//Server's port
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("name@domain.ru", "password102030");
}
});
session.setDebug(true);
try {
Scanner to = new Scanner(toWho);
while (to.hasNextLine())
{
String touser = to.nextLine();
try {
if (howMany <= batch)
{
howMany++;
System.out.println("Задержка "+delayevery/1000+" секунд");
Thread.sleep(delayevery);
}
else
{
howMany = 0;
System.out.println("Задержка "+delaybatch/1000+" секунд");
Thread.sleep(delaybatch);
}
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username,alias));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(touser));
message.setSubject(subject);
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
BodyPart imagePart = new MimeBodyPart();
if (image != "")
imagePart.setContent("<img src=\""+image+"\">","text/html");
// Now set the actual message
messageBodyPart.setText(MailSender.message);
// Create a multipar message
Multipart multipart = new MimeMultipart();
//Set image message part
multipart.addBodyPart(imagePart);
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
if (attaching != "")
{
DataSource source = new FileDataSource(attaching);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(javax.mail.internet.MimeUtility.encodeWord(source.getName(),
"UTF-8", null));
multipart.addBodyPart(messageBodyPart);
}
// Send the complete message parts
message.setContent(multipart);
Transport.send(message);
System.out.println("Done "+(++counter));
} catch (MessagingException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (FileNotFoundException ex) {
System.out.print("База была не найдена.");
} catch (InterruptedException ex) {
System.out.print("Что-то неверно.");
}
}
【问题讨论】:
标签: java api email java-8 jakarta-mail