【问题标题】:Send attached in mail java在邮件中发送附件 java
【发布时间】:2020-12-27 03:54:36
【问题描述】:

在这里它工作得很好,但我没有使用收到的文件,而是使用文件路径 我怎样才能做到这一点 我虽然将它转换为输入流会做一些好事,但没有输入流的构造函数 请告诉我 提前致谢

@RequestMapping("/SendMail")
    public String mail(@RequestParam("prescription") MultipartFile prescription,@RequestParam("email") String email,HttpSession session) {
        try {
            customer ct=custServ.customerExists(email);
            InputStream in = prescription.getInputStream();
            String filename=prescription.getName();
            if(ct!=null){
                final String SEmail="email@gmail.com";
                final String SPass="passowrd";
                final String REmail=email;
                final String Sub="Your prescription is here!";
                //mail send Code
            Properties props=new Properties();
            props.put("mail.smtp.host","smtp.gmail.com");
            props.put("mail.smtp.socketFactory.port","465");
            props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth","true");
            props.put("mail.smtp.port","465");
            Session ses=Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication(){
                    return new PasswordAuthentication(SEmail,SPass);
                }
            }
            );
            Message message=new MimeMessage(ses);
            message.setFrom(new InternetAddress(SEmail));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(REmail));
            message.setSubject(Sub);
            
            BodyPart messageBodyPart = new MimeBodyPart();
             messageBodyPart.setText("This is your prescription here");
             Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            messageBodyPart = new MimeBodyPart(); 
            
           // File filep=new File(prescription);
            DataSource source = new FileDataSource("C:\\Users\\Narci\\Desktop\\frontend\\Myqr3.jpg");
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);
             message.setContent(multipart);
            
            Transport.send(message);
            session.setAttribute("msg","Mail Sent successfully.");
            }
            else{
                session.setAttribute("msg", "Wrong Emial ID");
            }
            return "Doctor";
        }
        catch(Exception e) {
        e.printStackTrace();
        return "Error";
        }
    } ```

【问题讨论】:

  • 你到底想做什么?输入的来源是什么?
  • 简而言之,我想从前端获取文件并通过我从前端收到的文件所附的代码发送邮件
  • 所以为了澄清你的意图:你想基本上通过你的前端附加一个文件,然后使用 Gmail API 将它附加到邮件中吗?您在使用Multipart upload 时遇到什么问题?
  • @MateoRandwolf 我需要知道我可以在此代码中进行哪些更改,这样我就不必在此代码中使用文件路径来附加文件
  • 那么,您基本上是想使用带有附件的 Gmail API 发送电子邮件吗?并且您不想编写文件路径以将此文件附加到电子邮件内容?

标签: java spring jakarta-mail gmail-api multipartconfig


【解决方案1】:

这有点像在黑暗中拍摄,因为我不相信我真正理解这个问题。如果问题是; 我如何使用 File 而不是 MultipartFile 并获得 InputStream 那么答案是使用现有的库,如 Files.newInputStreamPath 作为参数。

Path path = Paths.get("path", "to", "my", "file");

try (InputStream input = Files.newInputStream(path)) {

}

【讨论】:

  • 当您不清楚问题时,我认为最好使用 cmets 部分进行讨论/澄清。
  • @GiorgiTsiklauri 这是一个非常好的观点,我应该在回答之前寻找更多信息。我唯一的答案是由于提供的代码。感谢您的洞察力,我可能应该寻找信息。
【解决方案2】:

解决方案

来自this other Stack Overflow answer (此答案发布为社区wiki)您可以在前端获取文件对象并将其转换为base64对象,如下所示:

const toBase64 = file => new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
});

async function Main() {
   const file = document.querySelector('#myfile').files[0];
   console.log(await toBase64(file));
}

Main();

然后,使用此 Base64 对象,您可以通过 PUT 请求将其发送到后端,然后按照 Gmail API 说明将其附加到电子邮件以进行分段上传。

我希望这对您有所帮助。让我知道您是否需要其他任何内容,或者您​​是否不理解某些内容。 :)

【讨论】:

    猜你喜欢
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2018-06-04
    相关资源
    最近更新 更多