2

我使用以下代码发送邮件:

- (IBAction)sendMailPressed:(id)sender
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {
        // We must always check whether the current device is configured for sending emails
        if ([mailClass canSendMail])
        {
            [self displayComposerSheet];
        }
        else
        {
            [self launchMailAppOnDevice];
        }
    }
    else
    {
        [self launchMailAppOnDevice];
    }
}

// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;


    [picker setSubject:self.strMailSubject];

    // Attach pdf to the email
    NSURL *urlToLoad = [[NSBundle mainBundle] URLForResource:self.strSorce withExtension:self.strExtention];
    NSData *myData = [NSData dataWithContentsOfURL:urlToLoad];
    [picker addAttachmentData:myData mimeType:@"application/pdf" fileName:[NSString stringWithFormat:@"%@.%@", self.strSorce, self.strExtention]];

    //  [self presentModalViewController:picker animated:YES];
    [[Singleton sharedInstance] pushModalViewController:picker whereCurrentController:self animated:YES];
    [picker release];
}


// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            break;
        case MFMailComposeResultSaved:
            break;
        case MFMailComposeResultSent:

            break;
        case MFMailComposeResultFailed:
            break;
        default:
            break;
    }

    [[Singleton sharedInstance] popModalViewControllerAnimated:YES];
}


// Launches the Mail application on the device.
-(void)launchMailAppOnDevice
{
    NSString *recipients = [NSString stringWithFormat:@"mailto:&subject=%@", self.strMailSubject];

    NSString *email = [NSString stringWithFormat:@"%@", recipients];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}

我得到了这个观点: 在此处输入图像描述

但我需要这个视图(请只注意来自字符串,而不是图像)。

我不知道如何让 From 字符串出现

在此处输入图像描述

4

5 回答 5

2

您可以setCcRecipients:在代码中使用它。

于 2013-01-04T11:16:25.877 回答
2

displayComposerSheet 只需在创建后在您的方法中添加这一行,MFMailComposeViewController还只需设置NSArray收件人名称...

 NSString *strFullName = [NSString stringWithFormat:@"From : %@",yourSenderEmailAddress];/// here write your email address which dynamic get from your string 
 NSArray *arrRecipients = [[NSArray alloc]initWithObjects:strFullName, nil];
[picker setCcRecipients:arrRecipients];

我也用一个例子更新了代码......

维沙尔的回答也是正确的,我只是完成它..

于 2013-01-04T11:33:19.723 回答
0

如果添加了多个邮件帐户,发件人字段将可见,以便从多个帐户中选择发件人。

于 2014-03-12T12:08:21.697 回答
0

您可以在 iOS 11 及更高版本中使用“setPreferredSendingEmailAddress”。

于 2018-08-16T12:21:03.850 回答
0

可能出于安全原因,无法以编程方式设置 MFMailComposeViewController 的 From: 字段。设备的所有者可以控制在设备上注册了哪些帐户,因此允许“发件人:”字段包含哪些内容。

发件人:字段仅显示设备上是否配置了多个电子邮件帐户;然后,该字段允许用户选择从哪个帐户发送。如果设备上只有一个帐户,则不会显示(它“不言而喻”正在使用哪个帐户)。

于 2015-08-22T21:56:33.540 回答