1

The code below opens up Outlook Email as soon as a button is pressed. Is there a way to automatically attach a file to the mail as well along with a subject possibly?

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();
            }       
    }

I tried changing the Desktop line to this. Should this work? Its not compiling though:

                    Desktop.getDesktop().browse(new URI('mailto:username@domain.com?subject=New_Profile&body=see attachment&attachment="xyz.xml"'));
  • See here: [Java: Start Mail-Client with Attachment?](http://stackoverflow.com/questions/6029579/java-start-mail-client-with-attachment) – Harry Joy Jul 11 '12 at 10:01

6 Answers6

2
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); 

You can't attach anything to the email automatically though, only manually.

Pita
  • 498
  • 5
  • 11
  • 21
1

No, there is no way to attach a file. You may specify a subject and body.

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

By the way, you are not sending mail via Java, this way. the tags and the question are not about the same topic.

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
0

You can specify a subject by doing this:

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

Note that subject needs to be URL encoded.

As far as I know there's no generic way to add attachments, although some mail clients might have a vendor-specific way to do it.

biziclop
  • 48,926
  • 12
  • 77
  • 104
0

Short answer is no. Jave does not support attachments via the means, it's been annoying me for 2 years.

The long answer is, you can get it to work using mapi & jni, but be prepared for world of hurt, as not all mail clients are equal

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

How to send email with an attachment in Java.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Yes you can do. My below code works perfect. Use it

Just call this function to send automated email to client. In parameter "to" is email address to which you want to send an email.

For attaching pdf reffer https://www.tutorialspoint.com/java/java_sending_email.htm

I usually do it in Maven project. If you are using maven project then import following dependencies. 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);
    }
}

}

Shubham Yeole
  • 101
  • 1
  • 2
  • 7