這裏是我的電子郵件控制類,這是作爲一個在電子郵件服務
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<br /><br />';
//Reading Email Subject Line to get the Program ID
strEmailSubject = email.subject;
strDebugger += 'Email Subject : <i>' + strEmailSubject + '</i><br />';
System.Debug('<<<<<<< Email Subject : ' + strEmailSubject + ' >>>>>>>');
//strProgramID = strEmailSubject.substring(strEmailSubject.indexOf(' : ') + 3, strEmailSubject.length()) ;
strProgramID = strEmailSubject;
strDebugger += 'Program ID : <i>' + strProgramID + '</i><br />';
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><br />';
//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><br />';
System.Debug('<<<<<<< Email Attachments (TEXT) found. >>>>>>>');
strDebugger += 'Total Number of text attachments : <i>' + email.textAttachments.size() + '</i><br />';
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 += '<br />Text attachment <i>' + (j + 1) + '</i> : <i>' + strAttachmentName + '</i><br />';
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><br /><br />';
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><br />';
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 += '<br />Total CSV\'s inserted : <i>' + attInserted.size() + '</i><br />';
System.Debug('<<<<<<< Total CSV\'s inserted : ' + attInserted.size());
if(attInserted.size() > 0)
{
//confirmation email message to the sender and support
String strSuccessHtmlBody = 'Hi, <br /> The Email Listener has successfully inserted ' + totalRecords + ' records to the Program Assessment (PA) from the following CSV files : <br /><br /><b>' + attInserted + '</b><br /><br /> Sender\'s Email ID : ' + email.fromAddress + '<br /> Subject Line : ' + email.subject + '<br /><br /><br /> Thanks, <br /> Email Listener Service <br/><br/><br/>' + '<br /><br /><br />' + 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, <br /> The Email Listener failed to insert records from the following attachment(s) : <br /><br /><b>' + attNotInserted + '</b><br /><br /> Only CSV files are allowed for data import. <br /> Please re try with correctly formatted CSV file. ' + '<br /><br /> Sender\'s Email ID : ' + email.fromAddress + '<br /> Subject Line : ' + email.subject + '<br /><br /><br /> Thanks, <br /> Email Listener Service' + '<br /><br /><br />' + 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, <br /> The Email Listener failed to insert records through the attachment(s). <br /><br /> There was no Program with program ID \'<i>' + strProgramID + '\'</i> found.' + '<br /><br /> Sender\'s Email ID : ' + email.fromAddress + '<br /> Subject Line : ' + email.subject + '<br /><br /><br /> Thanks, <br /> Email Listener Service' + '<br /><br /><br />' + 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, <br /> The Email Listener failed to insert records from the CSV file ' + strAttachmentName + '.<br /><br /> Sender\'s Email ID : ' + email.fromAddress + '<br /> Subject Line : ' + email.subject + '<br />Following is the exception occured : ' + e + '<br />Please check paramenters like Subject and attach a correctly formatted CSV file before re-sending.<br />' + '<br /><br /><br /> Thanks, <br /> Email Listener Service' + '<br /><br /><br />' + 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, <br /> You are not permissible to use this Email Listener service. <br /> No records were inserted from the CSV file ' + strAttachmentName + '.<br /><br /> Sender\'s Email ID : ' + email.fromAddress + '<br /> Subject Line : ' + email.subject + '<br /><br /><br /> Thanks, <br /> Email Listener Service' + '<br /><br /><br />' + 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, <br /> The Email Listener failed to insert records through the attachment(s). <br /><br /> There were no text attachment(s) found on the email. <br /><br /> Sender\'s Email ID : ' + email.fromAddress + '<br /> Subject Line : ' + email.subject + '<br /><br /><br /> Thanks, <br /> Email Listener Service' + '<br /><br /><br />' + 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 != '');
}
}
請澄清使用?你定製的東西?或者它是由Salesforce提供給您的產品嗎? –
這是我開發的一個Salesforce電子郵件服務。 – Vinar
有沒有你可以發佈的任何代碼? –