从java发送邮件

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

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

c:\Java Code\Practice\Report.html

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

c:\Java Code\Practice\Screenshot\MyPics.png

Screenshot<a href="file:///Java Code\Practice\Screenshot\MyPics.png">

Screenshot<a href="file:///c:\Java Code\Practice\Screenshot\MyPics.png">

Screenshot<a href="file:/c:\Java Code\Practice\Screenshot\MyPics.png">

Screenshot<a href="\\192.145.6.5\Java Code\Practice\Screenshot\MyPics.png">

Screenshot<a href="file:/192.145.6.5\c:\Java Code\Practice\Screenshot\MyPics.png">

Screenshot<a href="file:///192.145.6.5\c:\Java Code\Practice\Screenshot\MyPics.png">

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

public class SendingEmail {
    public static void main(String[] args) throws IOException {
        File mailReportFolder = new File("Mail Report");
        File fileClientResult = new File(mailReportFolder.getAbsolutePath() + "\\Email Report.txt");
        String clientTextPath = fileClientResult.getAbsolutePath().replaceAll("\\\\", "/");
        String emailBodyText = new String(Files.readAllBytes(Paths.get(clientTextPath)));

        final String username = "sample.user@test.com";
        final String password = "Password123";
        String fromEmail = "sample.user@test.com";
        String toEmail = "user1.rec@test.com";

        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.debug", "true");
        properties.put("mail.smtp.host", "192.145.40.56");

        // set the port of SMTP server
        properties.put("mail.smtp.port", "25");

        Session session = Session.getInstance(properties, 
                new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        MimeMessage msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(fromEmail));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            msg.setSubject("Report Checking");

            BodyPart messageBodyPart = new MimeBodyPart();
            StringBuilder sb = new StringBuilder();
            sb.append(emailBodyText);
            String message = sb.toString();
            messageBodyPart.setContent(message, "text/html");

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            messageBodyPart = new MimeBodyPart();
            String fileName = "C:\\Java Code\\Practice\\Report.html";
            DataSource source = new FileDataSource(fileName);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileName);
            multipart.addBodyPart(messageBodyPart);
            msg.setContent(multipart);

            Transport.send(msg);
            System.out.println("Message Sent");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

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

暂无答案!

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

相关问题