Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java mail api触发邮件时在邮件中添加了不必要的附件_Java_Email_Groovy - Fatal编程技术网

使用java mail api触发邮件时在邮件中添加了不必要的附件

使用java mail api触发邮件时在邮件中添加了不必要的附件,java,email,groovy,Java,Email,Groovy,我使用java mailer发送带有内联图像和html文件的电子邮件作为附件,但今天我注意到邮件“ATT00001.bin”中发送了不必要的附件,我没有在代码中的任何地方添加这些附件 真的,这对我来说太令人震惊了,所以我尝试通过更改附件名称来达到我的水平,但我找不到解决方案(直到最后一天它还可以正常工作(我的意思是我以前在邮件中收到一个内联图像和一个附件) [![//需要提及收件人的电子邮件ID。 字符串to=”mail@example.com"; //需要提及发件人的电子邮件ID 字符串fro

我使用java mailer发送带有内联图像和html文件的电子邮件作为附件,但今天我注意到邮件“ATT00001.bin”中发送了不必要的附件,我没有在代码中的任何地方添加这些附件

真的,这对我来说太令人震惊了,所以我尝试通过更改附件名称来达到我的水平,但我找不到解决方案(直到最后一天它还可以正常工作(我的意思是我以前在邮件中收到一个内联图像和一个附件)

[![//需要提及收件人的电子邮件ID。
字符串to=”mail@example.com";
//需要提及发件人的电子邮件ID
字符串from=”sender@example.com";
最终字符串username=“user”//相应地更改
最终字符串password=“pwd”//相应地更改
//假设您通过outlook邮件发送电子邮件
字符串host=“outlook.office365.com”;
Properties props=新属性();
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.smtp.starttls.enable”、“true”);
props.put(“mail.smtp.host”,host);
props.put(“mail.smtp.port”,“587”);
会话=会话.getInstance(props,
新的javax.mail.Authenticator(){
受保护的密码身份验证getPasswordAuthentication(){
返回新密码身份验证(用户名、密码);
}
});
试一试{
//创建默认的mimessage对象。
Message Message=新的mimessage(会话);
//设置自:标题的标题字段。
message.setFrom(新的InternetAddress(from));
//设置为:标题的标题字段。
message.setRecipients(message.RecipientType.TO,
InternetAddress.parse(to));
//设置主题:标题字段
//message.setSubject(“测试主体”);
//此邮件分为两部分,正文和嵌入的图像
MimMultipart multipart=新的MimMultipart(“相关”);
//第一部分(html)
BodyPart messageBodyPart=新的MimeBodyPart();
String htmlText=“大家好,

请查找当天的报告

问候,

团队

”; setContent(htmlText,“text/html”); //加上 multipart.addBodyPart(messageBodyPart); //第二部分(图像) messageBodyPart=新的MimeBodyPart(); //本地文件路径 DataSource fds=新文件数据源(“$localDirectory”); setDataHandler(新的DataHandler(fds)); messageBodyPart.setHeader(“内容ID”,即“”); //将图像添加到多部分 multipart.addBodyPart(messageBodyPart); //同时添加附件 //第二部分是附件 messageBodyPart=新的MimeBodyPart(); 字符串filename=“$reportFileName”; DataSource source=新文件DataSource(文件名); setDataHandler(新的DataHandler(源)); messageBodyPart.setFileName(“API_SummaryReport_${fileNamePart}+.html”); multipart.addBodyPart(messageBodyPart); //把所有的东西放在一起 message.setContent(多部分); //发送消息 传输。发送(消息); log.info(“成功发送电子邮件…”); }捕获(消息异常e){ 抛出新的运行时异常(e); }][1]][1]

只需要一个附件,但得到的是bin格式的附加文件,请查找图像以供参考

请将代码修改为:

String to = "mail@example.com";

  // Sender's email ID needs to be mentioned
  String from = "sender@example.com";

  final String username = "user";
  final String password = "pwd";

  String host = "outlook.office365.com";

  Properties props = new Properties();
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.port", "587");


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

    try {
        // Create a default MimeMessage object.
        Message message = new MimeMessage(session);

       // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject(msgSubject);
        message.setSentDate(new java.util.Date());
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        String htmlText = "<p>Hi Team,</p> <p>Please find the Report for the day   </p> <p>&nbsp;</p> <img src=\"cid:image\">  <p>&nbsp;Regards,  </p> <p>&nbsp;Team</p>";
       messageBodyPart.setContent(htmlText, "text/html");
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        MimeBodyPart imagePart = new MimeBodyPart();
        imagePart.setHeader("Content-ID", "<image>");
        //add this to avoid unwanted attachment.
        imagePart.setDisposition(MimeBodyPart.INLINE);
        imagePart.attachFile(new File("C:\\abc.png"));
        multipart.addBodyPart(imagePart);
        message.setContent(multipart);
        Transport.send(message);
    } catch (MessagingException | IOException ex) {
         throw new RuntimeException(e);
    }
String to=”mail@example.com";
//需要提及发件人的电子邮件ID
字符串from=”sender@example.com";
最终字符串username=“user”;
最终字符串密码=“pwd”;
字符串host=“outlook.office365.com”;
Properties props=新属性();
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.smtp.starttls.enable”、“true”);
props.put(“mail.smtp.host”,host);
props.put(“mail.smtp.port”,“587”);
会话=会话.getInstance(props,
新的javax.mail.Authenticator(){
受保护的密码身份验证getPasswordAuthentication(){
返回新密码身份验证(用户名、密码);
}
});
试一试{
//创建默认的mimessage对象。
Message Message=新的mimessage(会话);
//设置自:标题的标题字段。
message.setFrom(新的InternetAddress(from));
message.addRecipient(message.RecipientType.TO,新Internet地址(TO));
message.setSubject(msgSubject);
message.setSentDate(新java.util.Date());
MimeBodyPart messageBodyPart=新的MimeBodyPart();
String htmlText=“大家好,

请查找当天的报告

问候,

团队

”; setContent(htmlText,“text/html”); Multipart Multipart=新的MimeMultipart(); multipart.addBodyPart(messageBodyPart); MimeBodyPart imagePart=新的MimeBodyPart(); setHeader(“内容ID”,即“”); //添加此选项以避免不必要的附件。 setDisposition(MimeBodyPart.INLINE); imagePart.attachFile(新文件(“C:\\abc.png”); multipart.addBodyPart(imagePart); message.setContent(多部分); 传输。发送(消息); }捕获(MessaginException | IOException ex){ 抛出新的运行时异常(e); }
我不知道Groovy。但是这段代码肯定会起作用(解决了内联图像的问题)这些都添加到邮件中。因为我遇到了不需要的附件的问题。非常感谢Anish,我对代码进行了一点修改,它工作得很好,但我只想知道我以前的代码的问题?因此它对我非常有用编辑后,我只需再挑剔一点(因为答案似乎对OP有效)当前位置如果你解释一下原因,你的答案会大有改进
String to = "mail@example.com";

  // Sender's email ID needs to be mentioned
  String from = "sender@example.com";

  final String username = "user";
  final String password = "pwd";

  String host = "outlook.office365.com";

  Properties props = new Properties();
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.port", "587");


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

    try {
        // Create a default MimeMessage object.
        Message message = new MimeMessage(session);

       // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject(msgSubject);
        message.setSentDate(new java.util.Date());
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        String htmlText = "<p>Hi Team,</p> <p>Please find the Report for the day   </p> <p>&nbsp;</p> <img src=\"cid:image\">  <p>&nbsp;Regards,  </p> <p>&nbsp;Team</p>";
       messageBodyPart.setContent(htmlText, "text/html");
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        MimeBodyPart imagePart = new MimeBodyPart();
        imagePart.setHeader("Content-ID", "<image>");
        //add this to avoid unwanted attachment.
        imagePart.setDisposition(MimeBodyPart.INLINE);
        imagePart.attachFile(new File("C:\\abc.png"));
        multipart.addBodyPart(imagePart);
        message.setContent(multipart);
        Transport.send(message);
    } catch (MessagingException | IOException ex) {
         throw new RuntimeException(e);
    }