React Native
React Native
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 React Native Downloads
import { BinData, Email, Ftp2 } from '@chilkat/react-native'
async function chilkatExample() {
let success = false;
// 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.
const ftp = new Ftp2();
ftp.hostname = 'www.my-ftp-server.com';
ftp.username = 'mFtpLogin';
ftp.password = 'myFtpPassword';
// Connect to the FTP server.
success = true;
try {
await ftp.connectOnlyAsync();
} catch {
success = false;
}
if (!success) {
console.log(ftp.lastErrorText);
return;
}
// Authenticate with the FTP server.
success = true;
try {
await ftp.loginAfterConnectOnlyAsync();
} catch {
success = false;
}
if (!success) {
console.log(ftp.lastErrorText);
return;
}
// Move to the remove directory where our file is located.
success = true;
try {
await ftp.changeRemoteDirAsync('qa_data');
} catch {
success = false;
}
if (success) {
success = true;
try {
await ftp.changeRemoteDirAsync('pdf');
} catch {
success = false;
}
}
if (!success) {
console.log(ftp.lastErrorText);
return;
}
// Download...
const pdfData = new BinData();
success = true;
try {
await ftp.getFileBdAsync('fishing.pdf', pdfData);
} catch {
success = false;
}
if (!success) {
console.log(ftp.lastErrorText);
return;
}
await ftp.disconnectAsync();
// Create an email object, and add the PDF as an attachment.
const email = new Email();
email.subject = 'Test with PDF attachment.';
email.body = 'This is a plain-text body..';
// Add the PDF attachment. (This method call requires Chilkat v9.5.0.63 or later)
success = true;
try {
email.addAttachmentBd('fishing.pdf', pdfData, 'application/pdf');
} catch {
success = false;
}
if (!success) {
console.log(email.lastErrorText);
return;
}
// Save the email and examine with a text editor to see the PDF attachment is present..
email.saveEml('qa_output/out.eml');
console.log('Success.');
}