Mac development support multi mailbox sending mail


April 2019


26 time


I'm doing Mac development and I need to handle a mail feedback problem within the App function. We now only support a built-in Mail mailbox feedback system, but sometimes the user default mailbox is not Mail mailbox, it will lead to users unable to send mail.

How do we get the default mailbox users to send mail?

(void)sendEmail:(NSString *)subject messageContent:(NSString *)body fileAttachment:(NSArray *)filePathArr fromWho:(NSString *)emailFrom toWho:(NSString *)emailTo{
/ create a Scripting Bridge object for talking to the Mail application */
MailApplication *mail = (MailApplication *)[[SBApplication alloc]initWithBundleIdentifier: @""];
/ set ourself as the delegate to receive any errors */
[mail activate];
/ create a new outgoing message object */
MailOutgoingMessage *emailMessage = [[[mail classForScriptingClass:@"outgoing message"] alloc] initWithProperties:
                                     [NSDictionary dictionaryWithObjectsAndKeys:
                                      subject, @"subject",
                                      body, @"content",
/ Handle a nil value gracefully. */
/ add the object to the mail app  */
[[mail outgoingMessages] addObject: emailMessage];
/ set the sender, show the message */
emailMessage.sender = emailFrom;
emailMessage.visible = YES;
/ create a new recipient and add it to the recipients list */
MailToRecipient *theRecipient = [[[mail classForScriptingClass:@"to recipient"] alloc] initWithProperties:
                                 [NSDictionary dictionaryWithObjectsAndKeys:
                                  emailTo, @"address",
/ Handle a nil value gracefully. */
[emailMessage.toRecipients addObject: theRecipient];
/ add an attachment, if one was specified */
for(NSString *filePath in filePathArr){
    NSString *attachmentFilePath = filePath;
    if ( [attachmentFilePath length] > 0 ) {
        MailAttachment *theAttachment;
        / In Snow Leopard, the fileName property requires an NSString representing the path to the
         * attachment.  In Lion, the property has been changed to require an NSURL.   */
        SInt32 osxMinorVersion;
        Gestalt(gestaltSystemVersionMinor, &osxMinorVersion);
        / create an attachment object */
        if(osxMinorVersion >= 7)
            theAttachment = [[[mail classForScriptingClass:@"attachment"] alloc] initWithProperties:
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSURL fileURLWithPath:filePath], @"fileName",
        / The string we read from the text field is a URL so we must create an NSURL instance with it
         * and retrieve the old style file path from the NSURL instance. */
            theAttachment = [[[mail classForScriptingClass:@"attachment"] alloc] initWithProperties:
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              [[NSURL URLWithString:attachmentFilePath] path], @"fileName",
        / Handle a nil value gracefully. */
        / add it to the list of attachments */
        [[emailMessage.content paragraphs] addObject: theAttachment];
  / send the message */

0 answers