首页 文章

Salesforce Email Services不适用于Outlook或Lotus Notes

提问于
浏览
0

我有一个Salesforce电子邮件服务,只接受文本附件 . 一切正常,直到有人从Outlook或Lotus Notes发送电子邮件 .

从Outlook和Lotus Notes发送时,我的类甚至无法识别文本附件 . 仔细研究后,我发现当我从Gmail发送电子邮件时,文本附件以text / csv的MIME格式发送,而在Outlook / Lotus Notes的情况下,格式为application / octet-stream .

有没有人面对类似的事情,有什么建议来应对这种情况?

1 回答

  • 0

    这是我在电子邮件服务中使用的电子邮件控制器类

    global class CreativeDCoe_EmailHandler implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
    
          System.debug('<<<<<< Starting Email Listener >>>>>>');   
          List<String> attInserted = new List<String>();
          List<String> attNotInserted = new List<String>();
          String csvBody = '';
          String strAttachmentName = '';
          String strEmailSubject = '';
          String strProgramID = '';
          Integer totalRecords = 0;
          String strDebugger = 'Logs

    '; //Reading Email Subject Line to get the Program ID strEmailSubject = email.subject; strDebugger += 'Email Subject : <i>' + strEmailSubject + '</i>
    '; System.Debug('<<<<<<< Email Subject : ' + strEmailSubject + ' >>>>>>>'); //strProgramID = strEmailSubject.substring(strEmailSubject.indexOf(' : ') + 3, strEmailSubject.length()) ; strProgramID = strEmailSubject; strDebugger += 'Program ID : <i>' + strProgramID + '</i>
    '; System.Debug('<<<<<<< Program ID : ' + strProgramID + ' >>>>>>>'); String[] filelines = new String[]{}; List<Program_Assessment_PA__c> attPADataList = new List<Program_Assessment_PA__c>(); List<Data__c> fy; Boolean flag = false; if(email.textAttachments != null) { //List<User> usr = [SELECT ID, Name, Email FROM User WHERE Email =: email.fromAddress LIMIT 1]; //if(usr.size() > 0) if(true) { //strDebugger += 'Email sent from : <i>' + usr[0].Name + ' (' + usr[0].Email + ')</i>
    '; //System.Debug('<<<<<<< ' + usr[0].Name + ' >>>>>>>'); try { List<Program__c> program = [SELECT Name, ID, Program_Name__c FROM Program__c WHERE Name =: strProgramID ]; if (program.size() > 0) { strDebugger += 'Program found on records : <i>' + program[0].Name + ' (' + program[0].Program_Name__c + ')</i>
    '; System.Debug('<<<<<<< Email Attachments (TEXT) found. >>>>>>>'); strDebugger += 'Total Number of text attachments : <i>' + email.textAttachments.size() + '</i>
    '; fy = [SELECT Name, ID FROM Data__c WHERE Name =: String.valueOf(system.today().year()) LIMIT 1]; if(fy.size() == 0) { Data__c newFy = new Data__c (Name = String.valueOf(system.today().year())); insert(newFy); System.Debug('<<<<<<< FY Inserted. >>>>>>>'); fy[0] = newFY; System.Debug('<<<<<<< FY : ' + fy[0].Name + ' >>>>>>>'); } System.Debug('<<<<<<< Total number of Text Attachments : ' + email.textAttachments.size() + ' >>>>>>>'); for (integer j = 0 ; j < email.textAttachments.size() ; j++) { strAttachmentName = email.textAttachments[j].filename.trim(); strDebugger += '
    Text attachment <i>' + (j + 1) + '</i> : <i>' + strAttachmentName + '</i>
    '; System.Debug('<<<<<<< ' + (j+1) + ' Attachment Name : ' + strAttachmentName + ' >>>>>>>'); system.debug('<<<<<<< Extension of file : ' + strAttachmentName.substring(strAttachmentName.length() - 4, strAttachmentName.length()) + ' >>>>>>>'); // checking if the file is a csv file if((strAttachmentName.contains('.csv')) && (strAttachmentName.substring(strAttachmentName.length() - 4, strAttachmentName.length()) == '.csv')) { attPADataList.clear(); csvBody = email.textAttachments[j].body; System.Debug('<<<<<<< Total characters in the CSV file : ' + csvBody.length() + ' >>>>>>>'); filelines = csvBody.split('\n'); strDebugger += 'Total data lines in the CSV file : <i>' + (filelines.size() - 1) + '</i>

    '; System.Debug('<<<<<<< Total data lines in the CSV file : ' + (filelines.size() - 1) + ' >>>>>>>'); for (Integer i = 1;i < filelines.size();i++) { String[] inputvalues = new String[]{}; inputvalues = filelines[i].split(','); strDebugger += 'Total input values for line <i> ' + i + ' </i>are : <i>' + inputvalues.size() + '</i>
    '; System.Debug('<<<<<<< Total input values are : ' + inputvalues.size() + ' >>>>>>>'); if(inputvalues.size() != 0) { Program_Assessment_PA__c newPALineItem = new Program_Assessment_PA__c(); System.Debug('<<<<<<< Line : ' + i + ' >>>>>>>'); System.Debug(filelines[i]); newPALineItem.Fiscal_Year__c = fy[0].ID; newPALineItem.Date__c = system.today(); newPALineItem.Program_ID__c = program[0].ID; if(isNotNull(inputvalues[0])){ newPALineItem.Question_ID__c = inputvalues[0].trim(); flag = true; } if(isNotNull(inputvalues[1])){ newPALineItem.Objective__c = inputvalues[1].trim(); flag = true; } if(isNotNull(inputvalues[2])){ newPALineItem.Dimension__c = inputvalues[2].trim(); flag = true; } // there are some other columns if(flag) { System.Debug('<<<<<<< Line item number ' + i + ' added to list. >>>>>>>'); attPADataList.add(newPALineItem); flag = false; } } } System.Debug('<<<<<<< Inserting ' + attPADataList.size() + ' records to the PA Object >>>>>>>'); totalRecords += attPADataList.size(); insert attPADataList; attInserted.add(strAttachmentName); } else { attNotInserted.add(strAttachmentName); } } strDebugger += '
    Total CSV\'s inserted : <i>' + attInserted.size() + '</i>
    '; System.Debug('<<<<<<< Total CSV\'s inserted : ' + attInserted.size()); if(attInserted.size() > 0) { //confirmation email message to the sender and support String strSuccessHtmlBody = 'Hi,
    The Email Listener has successfully inserted ' + totalRecords + ' records to the Program Assessment (PA) from the following CSV files :

    <b>' + attInserted + '</b>

    Sender\'s Email ID : ' + email.fromAddress + '
    Subject Line : ' + email.subject + '


    Thanks,
    Email Listener Service


    ' + '


    ' + strDebugger ; String strSuccessEmailSubject = 'DCOE E-Mail Listener : Success'; System.debug('<<<<<<< DCOE Email Listener SUCCESS >>>>>>> '); sendEmail(email.fromAddress, strSuccessHtmlBody, strSuccessEmailSubject); } else { //Non CSV attachment email message to the sender and support String strNonCSVHtmlBody = 'Hi,
    The Email Listener failed to insert records from the following attachment(s) :

    <b>' + attNotInserted + '</b>

    Only CSV files are allowed for data import.
    Please re try with correctly formatted CSV file. ' + '

    Sender\'s Email ID : ' + email.fromAddress + '
    Subject Line : ' + email.subject + '


    Thanks,
    Email Listener Service' + '


    ' + strDebugger ; String strNonCSVEmailSubject = 'DCOE E-Mail Listener : Error - Non CSV Attachment(s) are not Accepted'; System.debug('<<<<<<< DCOE Email Listener No CSV Attachment(s) Found >>>>>>> '); sendEmail(email.fromAddress, strNonCSVHtmlBody, strNonCSVEmailSubject); } } else { //No Program found email message to sender and support String strNoProgramHtmlBody = 'Hi,
    The Email Listener failed to insert records through the attachment(s).

    There was no Program with program ID \'<i>' + strProgramID + '\'</i> found.' + '

    Sender\'s Email ID : ' + email.fromAddress + '
    Subject Line : ' + email.subject + '


    Thanks,
    Email Listener Service' + '


    ' + strDebugger ; String strNoProgramEmailSubject = 'DCOE E-Mail Listener : Error - No Matching Program Found'; System.debug('<<<<<<< DCOE Email Listener No Program Found >>>>>>> '); sendEmail(email.fromAddress, strNoProgramHtmlBody, strNoProgramEmailSubject); } } catch (Exception e) { //exception email message to sender and support String strErrorHtmlBody = 'Hi,
    The Email Listener failed to insert records from the CSV file ' + strAttachmentName + '.

    Sender\'s Email ID : ' + email.fromAddress + '
    Subject Line : ' + email.subject + '
    Following is the exception occured : ' + e + '
    Please check paramenters like Subject and attach a correctly formatted CSV file before re-sending.
    ' + '


    Thanks,
    Email Listener Service' + '


    ' + strDebugger ; String strErrorEmailSubject = 'DCOE E-Mail Listener : Error'; System.debug('<<<<<<< DCOE Email Listener Exception >>>>>>> ' + e); sendEmail(email.fromAddress, strErrorHtmlBody, strErrorEmailSubject); } } else { // Denial email message to sender and support String strDeniedHtmlBody = 'Hi,
    You are not permissible to use this Email Listener service.
    No records were inserted from the CSV file ' + strAttachmentName + '.

    Sender\'s Email ID : ' + email.fromAddress + '
    Subject Line : ' + email.subject + '


    Thanks,
    Email Listener Service' + '


    ' + strDebugger ; String strDeniedEmailSubject = 'DCOE E-Mail Listener : Error - You are not allowed to create records.'; System.debug('<<<<<<< DCOE Email Listener Denied User for Entry of Records>>>>>>>'); sendEmail(email.fromAddress, strDeniedHtmlBody, strDeniedEmailSubject); } } else { //No Attachments found email message to sender and support String strNoAttachmentHtmlBody = 'Hi,
    The Email Listener failed to insert records through the attachment(s).

    There were no text attachment(s) found on the email.

    Sender\'s Email ID : ' + email.fromAddress + '
    Subject Line : ' + email.subject + '


    Thanks,
    Email Listener Service' + '


    ' + strDebugger ; String strNoAttachmentEmailSubject = 'DCOE E-Mail Listener : Error - No Attachment(s) Found'; System.debug('<<<<<<< DCOE Email Listener No Attachment(s) Found >>>>>>> '); sendEmail(email.fromAddress, strNoAttachmentHtmlBody, strNoAttachmentEmailSubject); } return result; } public void sendEmail(String strFromEmailAddress, String strHtmlBody, String strEmailSubject) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String contactEmail = strFromEmailAddress; System.debug(strHtmlBody); mail.setToAddresses(new String[] {contactEmail} ); mail.setSubject(strEmailSubject); mail.setHtmlBody(strHtmlBody); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } public Boolean isNotNull(String str) { return (str != null && str != ''); } }

相关问题