155 lines
5.7 KiB
OpenEdge ABL
155 lines
5.7 KiB
OpenEdge ABL
// public with sharing class EmailManager {
|
||
// public EmailManager() {
|
||
|
||
// }
|
||
// }
|
||
|
||
|
||
|
||
// public class EmailManager {
|
||
// // Public method
|
||
// public void sendMail(String address, String subject, String body) {
|
||
// // Create an email message object
|
||
// Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
|
||
// String[] toAddresses = new String[] {address};
|
||
// mail.setToAddresses(toAddresses);
|
||
// mail.setSubject(subject);
|
||
// mail.setPlainTextBody(body);
|
||
// // Pass this email message to the built-in sendEmail method
|
||
// // of the Messaging class
|
||
// Messaging.SendEmailResult[] results = Messaging.sendEmail(
|
||
// new Messaging.SingleEmailMessage[] { mail });
|
||
// // Call a helper method to inspect the returned results
|
||
// inspectResults(results);
|
||
// }
|
||
// // Helper method
|
||
// private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
|
||
// Boolean sendResult = true;
|
||
// // sendEmail returns an array of result objects.
|
||
// // Iterate through the list to inspect results.
|
||
// // In this class, the methods send only one email,
|
||
// // so we should have only one result.
|
||
// System.debug('### results: ' + results);
|
||
// System.debug('### results size: ' + results.size());
|
||
// for (Messaging.SendEmailResult res : results) {
|
||
// if (res.isSuccess()) {
|
||
// System.debug('Email sent successfully');
|
||
// }
|
||
// else {
|
||
// sendResult = false;
|
||
// System.debug('The following errors occurred: ' + res.getErrors());
|
||
// }
|
||
// }
|
||
// return sendResult;
|
||
// }
|
||
// }
|
||
|
||
|
||
//changed
|
||
|
||
|
||
//working for the user sharing but for file not for image
|
||
// global class EmailManager {
|
||
|
||
// global static void sendEmailWithAttachment() {
|
||
|
||
// try {
|
||
// Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
|
||
|
||
// mail.setToAddresses(new String[] { 'monikakumarib007@gmail.com' });
|
||
// mail.setSubject('Email with Attachment');
|
||
// mail.setPlainTextBody('Please find the attachment.');
|
||
|
||
// Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
|
||
// // attachment.setFileName('image (5)');
|
||
// attachment.setFileName('TestFile.txt');
|
||
// attachment.setBody(Blob.valueOf('This is file content'));
|
||
// // attachment.setBody(Blob.valueOf('Test Image Content'));
|
||
// attachment.setContentType('text/plain');
|
||
// // attachment.setContentType('image/png');
|
||
|
||
|
||
// mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment });
|
||
|
||
// Messaging.SendEmailResult[] result =
|
||
// Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
|
||
|
||
// System.debug('Result: ' + result);
|
||
|
||
// } catch (Exception e) {
|
||
// System.debug('Error: ' + e.getMessage());
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
|
||
|
||
global class EmailManager {
|
||
|
||
// Pass ContentDocumentId of the file
|
||
global static void sendEmailWithAttachment(Id contentDocumentId) {
|
||
|
||
try {
|
||
|
||
// 1️⃣ Get latest version of file
|
||
ContentVersion cv = [
|
||
SELECT Id, Title, FileExtension, VersionData
|
||
FROM ContentVersion
|
||
WHERE ContentDocumentId = :contentDocumentId
|
||
ORDER BY CreatedDate DESC
|
||
LIMIT 1
|
||
];
|
||
|
||
// 2️⃣ Create Email
|
||
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
|
||
mail.setToAddresses(new String[] { 'monikakumarib007@gmail.com' });
|
||
mail.setSubject('File Attachment from Salesforce');
|
||
mail.setPlainTextBody('Please find the attached file.');
|
||
|
||
// 3️⃣ Create Attachment
|
||
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
|
||
attachment.setFileName(cv.Title + '.' + cv.FileExtension);
|
||
attachment.setBody(cv.VersionData);
|
||
attachment.setContentType(getMimeType(cv.FileExtension));
|
||
|
||
// 4️⃣ Attach File
|
||
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment });
|
||
|
||
// 5️⃣ Send Email
|
||
Messaging.SendEmailResult[] result =
|
||
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
|
||
|
||
if (result != null && result.size() > 0) {
|
||
if (result[0].isSuccess()) {
|
||
System.debug('✅ Email Sent Successfully');
|
||
} else {
|
||
System.debug('❌ Email Failed: ' + result[0].getErrors()[0].getMessage());
|
||
}
|
||
}
|
||
|
||
} catch (Exception e) {
|
||
System.debug('❌ Error: ' + e.getMessage());
|
||
}
|
||
}
|
||
|
||
|
||
// Helper method to detect MIME type
|
||
private static String getMimeType(String ext) {
|
||
|
||
Map<String, String> mimeTypes = new Map<String, String>{
|
||
'png' => 'image/png',
|
||
'jpg' => 'image/jpeg',
|
||
'jpeg' => 'image/jpeg',
|
||
'pdf' => 'application/pdf',
|
||
'txt' => 'text/plain',
|
||
'doc' => 'application/msword',
|
||
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||
'xls' => 'application/vnd.ms-excel',
|
||
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
||
};
|
||
|
||
return mimeTypes.containsKey(ext.toLowerCase())
|
||
? mimeTypes.get(ext.toLowerCase())
|
||
: 'application/octet-stream';
|
||
}
|
||
} |