Apple が提供するデフォルトの SBSendEmail の例のコードを使用して電子メールを送信しています。私の場合の唯一の違いは、受信者が事前にわからないことと、ユーザーがメール ウィンドウの [宛先] フィールドに受信者を入力することを期待していることです。これが私のコードです:
MailApplication *mail = [SBApplication
applicationWithBundleIdentifier:@"com.apple.Mail"];
/* create a new outgoing message object */
MailOutgoingMessage *emailMessage =
[[[mail classForScriptingClass:@"outgoing message"] alloc]
initWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
@"this is my subject", @"subject",
@"this is my content", @"content",
nil]];
/* add the object to the mail app */
[[mail outgoingMessages] addObject: emailMessage];
/* set the sender, show the message */
// emailMessage.sender = [self.fromField stringValue];
emailMessage.visible = YES;
/* create a new recipient and add it to the recipients list */
// MailToRecipient *theRecipient =
// [[[mail classForScriptingClass:@"to recipient"] alloc]
// initWithProperties:
// [NSDictionary dictionaryWithObjectsAndKeys:
// @"recipientEmailHere@example.com", @"address",
// nil]];
// [emailMessage.toRecipients addObject: theRecipient];
/* add an attachment, if one was specified */
NSString *attachmentFilePath = "<my provided file path>";
if ( [attachmentFilePath length] > 0 ) {
/* create an attachment object */
MailAttachment *theAttachment = [[[mail
classForScriptingClass:@"attachment"] alloc]
initWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
attachmentFilePath, @"fileName",
nil]];
/* add it to the list of attachments */
[[emailMessage.content attachments] addObject: theAttachment];
}
/* send the message */
[emailMessage send];
受信者を指定していないため、メール アプリケーションは「エラー、受信者が指定されていません」というアラートを開きます。このアラートには [メッセージの編集] ボタンが 1 つしかありませんが、ユーザーはこのボタンを使用して受信者を追加できます。このアラートが開かない可能性はありますか?