【问题标题】:Sending Emails (Java)发送电子邮件 (Java)
【发布时间】:2016-09-21 08:31:52
【问题描述】:

只要按下按钮,下面的代码就会打开 Outlook 电子邮件。 有没有办法自动将文件连同主题一起附加到邮件中?

public void onSubmit() {
            try {
                Desktop.getDesktop().browse(new URI("mailto:username@domain.com"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
    }

我尝试将桌面行更改为此。这应该工作吗?但它没有编译:

                    Desktop.getDesktop().browse(new URI('mailto:username@domain.com?subject=New_Profile&body=see attachment&attachment="xyz.xml"'));

【问题讨论】:

标签: java


【解决方案1】:
Desktop desktop = Desktop.getDesktop(); 
    String message = "mailto:username@domain.com?subject=New_Profile&body=seeAttachment&attachment=c:/Update8.txt"; 
    URI uri = URI.create(message); 
    desktop.mail(uri); 

您不能自动将任何内容附加到电子邮件中,只能手动附加。

【讨论】:

    【解决方案2】:

    不,无法附加文件。您可以指定主题和正文。

    http://skm.zoomquiet.org/data/20100419224556/index.html

    顺便说一句,您不是通过这种方式通过 Java 发送邮件。标签和问题不是同一个话题。

    【讨论】:

      【解决方案3】:

      您可以通过以下方式指定主题:

      Desktop.getDesktop().browse(new URI("mailto:username@domain.com?subject=My+subject"));
      

      请注意,主题需要进行 URL 编码。

      据我所知,没有通用的方法来添加附件,尽管某些邮件客户端可能有特定于供应商的方法来执行此操作。

      【讨论】:

        【解决方案4】:

        简短的回答是否定的。 Jave 不支持通过这种方式进行附件,这已经困扰我 2 年了。

        长答案是,您可以使用 mapi 和 jni 让它工作,但要为世界的伤害做好准备,因为并非所有邮件客户端都是平等的

        【讨论】:

          【解决方案5】:
          【解决方案6】:

          是的,你可以做到。我下面的代码完美无缺。使用它

          只需调用此函数即可向客户发送自动电子邮件。 参数中的“to”是您要发送电子邮件的电子邮件地址。

          用于附加pdf参考https://www.tutorialspoint.com/java/java_sending_email.htm

          我通常在 Maven 项目中这样做。如果您使用的是 maven 项目,则导入以下依赖项。 https://mvnrepository.com/artifact/javax.mail/mail/1.4

          private void sendMail(String to, String subject, String emailBody) throws MessagingException{
              final String username = "youremail@gmail.com";
              final String password = "emailPassword";
          
              Properties props = new Properties();
              props.put("mail.smtp.auth", "true");
              props.put("mail.smtp.starttls.enable", "true");
              props.put("mail.smtp.host", "smtp.gmail.com");
              props.put("mail.smtp.port", "587");
          
              Session session = Session.getInstance(props,
                      new javax.mail.Authenticator() {
                  protected PasswordAuthentication getPasswordAuthentication() {
                      return new PasswordAuthentication(username, password);
                  }
              });
          
              try {
          
                  Message message = new MimeMessage(session);
                  message.setFrom(new InternetAddress("shubham20.yeole@gmail.com"));
                  message.setRecipients(Message.RecipientType.TO,
                          InternetAddress.parse(to));
                  message.setSubject(subject);
                  message.setContent(emailBody, "text/html; charset=utf-8");
          
                  Transport.send(message);
          
                  System.out.println("Done");
          
              } catch (MessagingException e) {
                  throw new RuntimeException(e);
              }
          }
          

          }

          【讨论】:

            猜你喜欢
            • 2018-05-10
            • 1970-01-01
            • 2016-03-16
            • 2023-03-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-03-01
            相关资源
            最近更新 更多