Reputation:
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:[email protected]"));
} 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:[email protected]?subject=New_Profile&body=see attachment&attachment="xyz.xml"'));
Upvotes: 1
Views: 3524
Reputation: 101
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 = "[email protected]";
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("[email protected]"));
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);
}
}
}
Upvotes: 0
Reputation: 508
Desktop desktop = Desktop.getDesktop();
String message = "mailto:[email protected]?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.
Upvotes: 2
Reputation: 347324
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
Upvotes: 0
Reputation: 49804
You can specify a subject by doing this:
Desktop.getDesktop().browse(new URI("mailto:[email protected]?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.
Upvotes: 0
Reputation: 16060
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.
Upvotes: 1