关于xcode:使用iOS向自己发送电子邮件。

Sending eMail to self using iOS.

我已经通过messageUIFramework与MFMailComposeViewController一起使用。我有一种情况,我必须向自己发送电子邮件。如何添加"发件人"地址MFMailComposeViewController委托?有什么建议?


如何在APP中发送电子邮件?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
- (IBAction)sendMail:(id)sender {
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

        mailer.mailComposeDelegate = self;

        [mailer setSubject:@"Message Pro"];

        //Destination adress
        NSArray *toRecipients = [NSArray arrayWithObjects:@"your adress", nil];
        [mailer setToRecipients:toRecipients];

        //Attachement Object
        UIImage *myImage = [UIImage imageNamed:@"image.jpeg"];
        NSData *imageData = UIImagePNGRepresentation(myImage);
        [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"mobiletutsImage"];

        //Message Body
        NSString *emailBody = @"message body";
        [mailer setMessageBody:emailBody isHTML:NO];

        [self presentModalViewController:mailer animated:YES];

        [mailer release];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                        message:@"Your device doesn't support the composer sheet"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }

    // Remove the mail view
    [self dismissModalViewControllerAnimated:YES];
}

别忘了

  • IBAction = sendMail添加按钮
  • 导入MessageUI.framework
  • 在.h文件MFMailComposeViewControllerDelegate中添加委托

  • MFMailComposeViewController为您指定了"发件人"地址(用户在获得撰写邮件视图时可以覆盖成为其已配置邮件帐户中的任何一个)。您,程序员,不必担心获取/设置"发件人"地址。