简体   繁体   中英

Java - Send Email using Default Email Provider

I have written a Java Code so that if a button is pressed the default email provider would open up automatically to be able to send an email. is there a possibility that i can attach a file to the email automatically and set a subject for the emails?

this is the code so far:

        if(role.getValue().equals("1")) {
                try {
                    Desktop.getDesktop().browse(new URI("mailto:username@domain.com?subject=New_Profile&body=see attachment&attachment="PVS_XML.xml""));

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }       
        }

the code above is for some reason not working: its underlining the whole of mailto method saying : Syntax error, insert ";" to complete BlockStatements.

Any ideas why ?

Add a "mailto:username@domain.com" link. This should trigger your browser to startup the standard mail programm.

This has nothing to do with Wicket. It is standard browser behavior.

'mailto:email@email.com?subject=Software&body=see attachment&attachment="C:/abc/def/qwertyp.zip"'

Try above it should work

EDIT 1:

Not sure if it will work from Desktop but as a link from a web page

From java as a string:

"'mailto:username@domain.com?subject=New_Profile&body=see attachment&attachment=\"PVS_XML.xml\"";

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

Also found out that attachment part is not implemented in Outlook so it doesn't support attachments using mailto. It depends on the mail client to implement parsing the attachment parameter.

Desktop won't work because it runs on the server, not on the client. It may open an Outlook window in your server, though :)

You should yse an mailto: link, as others said. The user will have to attach the file manually, though. The browser forces this due security concerns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM