0

I'm trying to write a program that, after the user reviews certain equipment and answers some questions about them, it creates a report and automatically sends it to a database.

The program itself is not very complicated, I have it more or less solved, but I fail in the part of sending the mail. I have been searching, and I have found the JavaMail API, and I have even learned to send emails with it, more or less, but my company blocks any attempt of an external program to send e-mail, so I have decided to give it a different approach and try that instead of sending it automatically, prepare the mail in the Outlook editor itself, ready to be sent, and that the user only has to click to send, after reviewing it.

But looking here, or Javamail documentation, or even googling, I can't find any reference to people doing it, even knowing that it can be done, as I've been using some programs that do this by themselves!

So, the question is: can I do this with JavaMail? If yes, could you provide me with an example, or something, to learn how to use it? If not, any other libraries able to do that?

Maybe this is a simple question, maybe Java itself has a function for doing it. But I've been looking for it for a week, and I can't find anything that I can use.

I'm very very new to programming (a bit more than a year), so try to keep the answer to a basic level that some novice can understand, please.

As an example, let's say I have an equipment called X. The programs asks me "Does X makes excessive noise?" and I check "Correct" button. Then, it asks "Has X a normal pressure level?", and I check "Incorrect" button, and add a comment "Pressure level to high". And so on, until I've answered every question. Then, when I have finished with X equipment, and push the "Finish" button, I want a "New Email" outlook window to pop out, with the receiver already fulfilled, "Equipment X 27/12/2017 morning revision" as subject, and the body something like:
"Noise revision: correct Pressure level: incorrect Comment: Pressure level to high Question 3: correct Question 4: correct etc."

I've solved already how to create the body, and assign every parameter to its place. The problem is the pop out and auto fulfilling thing, how to export all that data to outlook to be ready to be sent. And yes, bosses specify that I have to use outlook.

  • It is not quite clear - what are you looking for and what are you trying to achieve. So you prepare your mail in outlook and then you want another program to take control over outlook's draft and store that draft it in database? Please elaborate .. Also it is not clear - have you implemented the server part that receives a mail (say in case that you could use javamail) and stores it in database ? – PKey Dec 27 '17 at 12:06
  • Sorry, English is not my native language, and is a bit hard to make myself clear. What I want is actually the opposite. To prepare the mail with the program (or, more accurately, the data it will contain, who will receive it, etc.), and then export all this information to outlook, so the user only has to click the "Send" button if everything is correct. The database part is not implemented yet, as is not even clear if the company will want an automated database, or if there will be someone else receiving and analyzing it. Just now, the problem I'm trying to solve is to send the email. – Hector Guerra Dec 27 '17 at 12:27
  • Ok - that makes it somewhat more clear. What about creating a message file with java - and then opening it with e-mail client? Take a look [here](https://stackoverflow.com/questions/8167583/create-an-email-object-in-java-and-save-it-to-file) – PKey Dec 27 '17 at 12:32
  • That's not actually what I'm looking for, but it's much more that what I've been able to find by myself, so thanks, that already will help me so much. Probably with little modifications, will be enough. Now, with that file (if I'm using it correctly, as I may be doing it completely wrong) I can just open it with outlook and see it as a message, but I can't send or edit it. What I need is to open the same window that would be opened when pressing "New Email" on Outlook, but with all the info already filled in, and logged with my outlook account. Not sure if this makes it more or less clear. – Hector Guerra Dec 27 '17 at 13:26

1 Answers1

2

So I would propose to create and save a message with JavaMail as discussed here

Now, you cannot send the particular message right away because the message header does not contain the following line:

"X-Unsent":1

(which will actually instruct the outlook client that the message is in draft state)

So the code should look something like this:

(note that this is not tested, just copy pasted from different sources)

public static void createMessage(String to, String from, String subject, String body, List<File> attachments) {
try {
    Message message = new MimeMessage(Session.getInstance(System.getProperties()));
    message.setFrom(new InternetAddress(from));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
    message.setSubject(subject);

    //make it a draft!!             
    message.setHeader("X-Unsent", "1");

    // create the message part 
    MimeBodyPart content = new MimeBodyPart();
    // fill message
    content.setText(body);
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(content);
    // add attachments
    for(File file : attachments) {
        MimeBodyPart attachment = new MimeBodyPart();
        DataSource source = new FileDataSource(file);
        attachment.setDataHandler(new DataHandler(source));
        attachment.setFileName(file.getName());
        multipart.addBodyPart(attachment);
    }
    // integration
    message.setContent(multipart);
    // store file
    message.writeTo(new FileOutputStream(new File("c:/mail.eml")));
} catch (MessagingException ex) {
    Logger.getLogger(Mailkit.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
    Logger.getLogger(Mailkit.class.getName()).log(Level.SEVERE, null, ex);
}
}

Hope this helps.

PKey
  • 3,715
  • 1
  • 14
  • 39
  • Yes, that's it! Thank you very much, that was just what I was looking for! – Hector Guerra Dec 27 '17 at 13:58
  • No need for FileDataSource or DataHandler. Just call [attachment.attach(File)](https://docs.oracle.com/javaee/7/api/javax/mail/internet/MimeBodyPart.html#attachFile-java.io.File-). – VGR Dec 27 '17 at 14:03