Java
Chilkat2 Java
Add Email Attachment from FTP
Downloads (into memory) a file from an FTP server and adds it as an attachment to an email.Note: This example requires Chilkat v9.5.0.63 or later.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.Ftp2;
public class ChilkatExample {
public static void main(String[] args) {
// Note: This example requires Chilkat v9.5.0.63 or later.
// This example assumes Chilkat Ftp2 to have been previously unlocked.
// See Unlock Ftp2 for sample code.
Ftp2 ftp = new Ftp2();
ftp.setHostname("www.my-ftp-server.com");
ftp.setUsername("mFtpLogin");
ftp.setPassword("myFtpPassword");
// Connect to the FTP server.
try {
ftp.connectOnly();
// Authenticate with the FTP server.
ftp.loginAfterConnectOnly();
// Move to the remove directory where our file is located.
ftp.changeRemoteDir("qa_data");
ftp.changeRemoteDir("pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Download...
BinData pdfData = new BinData();
try {
ftp.getFileBd("fishing.pdf", pdfData);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
ftp.disconnect();
// Create an email object, and add the PDF as an attachment.
Email email = new Email();
email.setSubject("Test with PDF attachment.");
email.setBody("This is a plain-text body..");
// Add the PDF attachment. (This method call requires Chilkat v9.5.0.63 or later)
try {
email.addAttachmentBd("fishing.pdf", pdfData, "application/pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Save the email and examine with a text editor to see the PDF attachment is present..
email.saveEml("qa_output/out.eml");
System.out.println("Success.");
}
}