RPA - MailSendOptions attachment throwing null error

Certified Lead Developer

Hi,

I am trying to configure an RPA bot to send a csv file via email.  I have the bot working fine sending an email *without* an attachment, but when I try to add an attachment, I am receiving the following error:

[WARN] 2020-10-07 02:51:55 01m 31s (0h) - Error sending email
java.lang.IllegalArgumentException: Attachment 'testcsv.csv' file cannot be null

My sendEmail() method is below.  I have done the proper (I think) imports above in my file, and have declared the following in my main MyRobot class:

private File currentItemOutputFile;
private IMail mail;

sendEmail() method:

public void sendEmail() throws Exception{
		windows.pause(PAUSE_LONG);
		String subject = "Test Email";
		String body = String.format("Test");
		MailSendOptions message = new MailSendOptions();

		message.setHost("localhost");
		message.setPort(25);
		message.setFromAddress("test@example.com");


		// Adds content to email
		message.toAddress(emailAddress);
		message.setSubject(subject);
		message.setTextContent(body);
		// Attaches file to email
		MailAttachment mailAttachment = new MailAttachment();
		mailAttachment.setType(MailAttachment.EType.FILE);
		mailAttachment.setName(currentItemOutputFile.getName());
		message.setAttachments(mailAttachment);

		try {
			mail.sendMailFromServer(message);
		} catch (Throwable e) {
			server.warn("Error sending email", e);
			throw new JidokaItemException(String.format("Error sending file %s by + email", currentItemOutputFile.getAbsolutePath()));
		}
		// Sends result to server
		server.setCurrentItemResultToOK("Document Sent");
}

  Discussion posts and replies are publicly visible

Parents
  • You need to include your File object in the mailAttachment object.
    The setName() method is used to display the name of the attached file in the email, but not to load the content.
    The easier way to include it is adding the your File in the MailAttachment constructor.

    		MailAttachment mailAttachment = new MailAttachment(currentItemOutputFile);
    		mailAttachment.setType(MailAttachment.EType.FILE);
    		mailAttachment.setName(currentItemOutputFile.getName());
    		message.setAttachments(mailAttachment);

  • 0
    Certified Lead Developer
    in reply to David Catalan

    David, one follow-up question: the email is coming across fine, but the attachment is coming across as a generic "file" without an extension or name, which is causing a problem downstream (the Appian PM I am sending the email to doesn't recognize it...testing out sending an email directly with a CSV file works).

    The code I am using is below.  Can you identify what I need to do to explicitly set the name and the type to CSV?

    message.toAddress(emailAddress);
    		message.setSubject(subject);
    		message.setTextContent(body);
    		// Attaches file to email
    		MailAttachment mailAttachment = new MailAttachment(currentItemOutputFile);
    		mailAttachment.setName("test");
    		mailAttachment.setFileName("test");
    		message.setAttachments(mailAttachment);

Reply
  • 0
    Certified Lead Developer
    in reply to David Catalan

    David, one follow-up question: the email is coming across fine, but the attachment is coming across as a generic "file" without an extension or name, which is causing a problem downstream (the Appian PM I am sending the email to doesn't recognize it...testing out sending an email directly with a CSV file works).

    The code I am using is below.  Can you identify what I need to do to explicitly set the name and the type to CSV?

    message.toAddress(emailAddress);
    		message.setSubject(subject);
    		message.setTextContent(body);
    		// Attaches file to email
    		MailAttachment mailAttachment = new MailAttachment(currentItemOutputFile);
    		mailAttachment.setName("test");
    		mailAttachment.setFileName("test");
    		message.setAttachments(mailAttachment);

Children
No Data