从java发送邮件

cngwdvgl  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(278)

我有一个html代码,其中包含一个链接到截图。当我使用java代码从远程计算机将此html文件作为电子邮件(outlook)中的附件发送时,附件已成功发送。但当我点击屏幕截图链接(从本地机器打开的附件邮件)时,我在浏览器中收到“找不到文件”的消息。我尝试了下面不同的html文件中的代码,但没有一个能够打开屏幕截图。
html文件位置(在远程计算机-192.145.6.5中):

  1. c:\Java Code\Practice\Report.html

屏幕截图位置(在远程计算机-192.145.6.5中):

  1. c:\Java Code\Practice\Screenshot\MyPics.png
  2. Screenshot<a href="file:///Java Code\Practice\Screenshot\MyPics.png">
  3. Screenshot<a href="file:///c:\Java Code\Practice\Screenshot\MyPics.png">
  4. Screenshot<a href="file:/c:\Java Code\Practice\Screenshot\MyPics.png">
  5. Screenshot<a href="\\192.145.6.5\Java Code\Practice\Screenshot\MyPics.png">
  6. Screenshot<a href="file:/192.145.6.5\c:\Java Code\Practice\Screenshot\MyPics.png">
  7. Screenshot<a href="file:///192.145.6.5\c:\Java Code\Practice\Screenshot\MyPics.png">

下面是使用java发送电子邮件的代码

  1. public class SendingEmail {
  2. public static void main(String[] args) throws IOException {
  3. File mailReportFolder = new File("Mail Report");
  4. File fileClientResult = new File(mailReportFolder.getAbsolutePath() + "\\Email Report.txt");
  5. String clientTextPath = fileClientResult.getAbsolutePath().replaceAll("\\\\", "/");
  6. String emailBodyText = new String(Files.readAllBytes(Paths.get(clientTextPath)));
  7. final String username = "sample.user@test.com";
  8. final String password = "Password123";
  9. String fromEmail = "sample.user@test.com";
  10. String toEmail = "user1.rec@test.com";
  11. Properties properties = new Properties();
  12. properties.put("mail.smtp.auth", "true");
  13. properties.put("mail.smtp.starttls.enable", "true");
  14. properties.put("mail.debug", "true");
  15. properties.put("mail.smtp.host", "192.145.40.56");
  16. // set the port of SMTP server
  17. properties.put("mail.smtp.port", "25");
  18. Session session = Session.getInstance(properties,
  19. new javax.mail.Authenticator() {
  20. protected PasswordAuthentication getPasswordAuthentication() {
  21. return new PasswordAuthentication(username, password);
  22. }
  23. });
  24. MimeMessage msg = new MimeMessage(session);
  25. try {
  26. msg.setFrom(new InternetAddress(fromEmail));
  27. msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
  28. msg.setSubject("Report Checking");
  29. BodyPart messageBodyPart = new MimeBodyPart();
  30. StringBuilder sb = new StringBuilder();
  31. sb.append(emailBodyText);
  32. String message = sb.toString();
  33. messageBodyPart.setContent(message, "text/html");
  34. Multipart multipart = new MimeMultipart();
  35. multipart.addBodyPart(messageBodyPart);
  36. messageBodyPart = new MimeBodyPart();
  37. String fileName = "C:\\Java Code\\Practice\\Report.html";
  38. DataSource source = new FileDataSource(fileName);
  39. messageBodyPart.setDataHandler(new DataHandler(source));
  40. messageBodyPart.setFileName(fileName);
  41. multipart.addBodyPart(messageBodyPart);
  42. msg.setContent(multipart);
  43. Transport.send(msg);
  44. System.out.println("Message Sent");
  45. } catch (MessagingException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }

请让我知道,如果在任何情况下,需要从我这边。谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题