Here i was made one Inbound Email Service..For that i made one Apex Class,here the code of the class…
global class EmailDemoReceive implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.Inboundenvelope envelope) {
Account account;
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
try {
if ([select count() from Account where Name = :email.subject] == 0) {
account = new Account();
account.Name = email.subject;
insert account;
} else {
account = [select Id from Account where Name = :email.subject];
}
// Convert cc'd addresses to contacts
for (String address : email.ccAddresses) {
Contact contact = new Contact();
Matcher matcher = Pattern.compile('<.+>').matcher(address);
// Parse addresses to names and emails
if (matcher.find()) {
String[] nameParts = address.split('[ ]*<.+>')[0].replace('"', '').split('[ ]+');
contact.FirstName = nameParts.size() > 1 ? nameParts[0] : '';
contact.LastName = nameParts.size() > 1 ? nameParts[nameParts.size()-1] : nameParts[0];
contact.Email = matcher.group().replaceAll('[<>]', '');
} else {
contact.LastName = address;
contact.Email = address;
}
// Add if new
if ([select count() from Contact where Email = :contact.Email] == 0) {
contact.AccountId = account.Id;
insert contact;
}
}
// Save attachments, if any
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
Attachment attachment = new Attachment();
attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
attachment.ParentId = account.Id;
insert attachment;
}
// Turn email body into note
Note note = new Note();
note.Title = email.fromName + ' (' + DateTime.now() + ')';
note.Body = email.plainTextBody;
note.ParentId = account.Id;
insert note;
result.success = true;
} catch (Exception e) {
result.success = false;
result.message = 'Oops, I failed.';
}
return result;
}
}
after that i was made Email service,here is the snapshot for that…
whenever i received a mail As a ToAddress of Above Encoded mail address in red portion…then and then Account Record is created…..
But here i want to use user's real Email Address Instead of Encoded email address…
whenever the mail is received from user's real email address,Apex class is run and Account record is created ….that exactly here i want…..
If any solution then please respond me……
Best Answer
I suspect you may have a misunderstanding on what an inbound email service is. All email must be sent to the encoded email address for processing by your inbound service.
I also see an issue in your code. You're converting your "cc'd" addresses to contacts instead of your "from" addresses.
You'll note that I've omitted your matcher code in my response. I don't really think its necessary, but feel free to implement it in your code if you wish.
As @tedweinberg also mentioned in his comment, your class' security settings will affect the email addresses you can receive email from. If an address doesn't pass at least one of the security protocols you've enabled, it will be rejected and won't be processed. Hope this gets you up and running.