この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Newer Version Available

This content describes an older version of this product. View Latest

送信メール

個別メール送信または一括メール送信に Apex を使用することができます。メールには、件名、BCC アドレスなど標準的なメールの属性をすべて含めることができます。Salesforce メールテンプレートを使用することが可能で、平文テキスト、HTML 形式または Visualforce で生成されたテンプレートのいずれかを選択できます。

Visualforce メールテンプレートは一括メール送信には使用できません。

メモ

Salesforce を使用し、メールが送られた日付、最初に開かれた日付と最後に開かれた日付、開かれた合計回数など HTML 形式のメールの状況を追跡できます (詳細は、Salesforce オンラインヘルプの「HTML メールの追跡」を参照してください)。

個別メール送信または一括メール送信に Apex を使用するには、次のクラスを使用します。
SingleEmailMessage
単一のメールメッセージの送信に使用されるメールオブジェクトをインスタンス化します。構文は次のとおりです。
1Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
MassEmailMessage
メールメッセージの一括メール送信に使用されるメールオブジェクトをインスタンス化します。構文は次のとおりです。
1Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
Messaging
静的な sendEmail メソッドを含みます。このメソッドでは、SingleEmailMessage クラスまたは MassEmailMessage クラスのいずれかを使用してインスタンス化するメールオブジェクトを送信し、SendEmailResult オブジェクトを返します。

メールを送信する構文は次のとおりです。

1Messaging.sendEmail(new Messaging.Email[] { mail } , opt_allOrNone);

EmailMessaging.SingleEmailMessage または Messaging.MassEmailMessage のいずれかとなります。

(省略可能) opt_allOrNone パラメータでは、任意のメッセージがエラーで失敗した場合、sendEmail でその他すべてのメッセージの配信を行わない (true) か、エラーのないメッセージの配信を行う (false) かを指定します。デフォルトは、true です。

静的な reserveMassEmailCapacity メソッドおよび reserveSingleEmailCapacity メソッドを含みます。これらのメソッドはメールを送信する前にコールし、トランザクションをコミットしてメールを送信するときに、送信する組織の 1 日あたりのメール送信量の制限を超えていないことを確認します。構文は次のとおりです。
1Messaging.reserveMassEmailCapacity(count);

および

1Messaging.reserveSingleEmailCapacity(count);

ここで、count はメールの送信先アドレスの総数を示します。

次の点に注意してください。
  • Apex トランザクションがコミットされるまでメールは送信されません。
  • sendEmail メソッドをコールしているユーザのメールアドレスは、メールヘッダーの [送信元アドレス] 項目に挿入されます。返信されたメール、不達メールおよび外出中の自動返信メールは、このメソッドをコールしているユーザに送信されます。
  • トランザクションあたり最大 10 個の sendEmail メソッド。Limits メソッドを使用して、トランザクション内の sendEmail メソッドの数を確認します。
  • sendEmail メソッドで送信される単一のメールメッセージは、送信する組織の 1 日の単一メール制限にカウントされます。この制限値に達すると、SingleEmailMessage を使用する sendEmail メソッドへのコールは拒否され、ユーザは SINGLE_EMAIL_LIMIT_EXCEEDED エラーコードを受信します。ただし、Salesforce アプリケーションを通して送られた単一メールは許可されます。
  • sendEmail メソッドで送信される一括メールメッセージは、送信する組織の 1 日の一括メール制限にカウントされます。この制限値に達すると、MassEmailMessage を使用する sendEmail メソッドへのコールは拒否され、ユーザは MASS_MAIL_LIMIT_EXCEEDED エラーコードを受信します。
  • SendEmailResult オブジェクトで返されるすべてのエラーは、メールが送信されなかったことを表します。

Messaging.SingleEmailMessage には setOrgWideEmailAddressId というメソッドがあります。OrgWideEmailAddress オブジェクトのオブジェクト ID を受け取ります。setOrgWideEmailAddressId に有効な ID が渡されると、OrgWideEmailAddress.DisplayName 項目が、ログインユーザの [表示名] ではなく、メールヘッダーに使用されます。ヘッダーの送信メールアドレスも、OrgWideEmailAddress.Address で定義された項目に設定されます。

OrgWideEmailAddress.DisplayName および setSenderDisplayName の両方が定義されると、DUPLICATE_SENDER_DISPLAY_NAME エラーが発生します。

メモ

詳細は、Salesforce オンラインヘルプの「組織の共有アドレス」を参照してください。

1// First, reserve email capacity for the current Apex transaction to ensure
2// that we won't exceed our daily email limits when sending email after
3// the current transaction is committed.
4Messaging.reserveSingleEmailCapacity(2);
5
6// Processes and actions involved in the Apex transaction occur next,
7// which conclude with sending a single email.
8
9// Now create a new single email message object
10// that will send out a single email to the addresses in the To, CC & BCC list.
11Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
12
13// Strings to hold the email addresses to which you are sending the email.
14String[] toAddresses = new String[] {'user@acme.com'}; 
15String[] ccAddresses = new String[] {'smith@gmail.com'};
16  
17
18// Assign the addresses for the To and CC lists to the mail object.
19mail.setToAddresses(toAddresses);
20mail.setCcAddresses(ccAddresses);
21
22// Specify the address used when the recipients reply to the email. 
23mail.setReplyTo('support@acme.com');
24
25// Specify the name used as the display name.
26mail.setSenderDisplayName('Salesforce Support');
27
28// Specify the subject line for your email address.
29mail.setSubject('New Case Created : ' + case.Id);
30
31// Set to True if you want to BCC yourself on the email.
32mail.setBccSender(false);
33
34// Optionally append the salesforce.com email signature to the email.
35// The email address of the user executing the Apex Code will be used.
36mail.setUseSignature(false);
37
38// Specify the text content of the email.
39mail.setPlainTextBody('Your Case: ' + case.Id +' has been created.');
40
41mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created.<p>'+
42     'To view your case <a href=https://na1.salesforce.com/'+case.Id+'>click here.</a>');
43
44// Send the email you have created.
45Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });