0

我正在尝试发送包含文本字段和图像数据的电子邮件,但它不起作用,请告知。这是我的代码:

     - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1)
{

    Cocktails*c=[[Cocktails alloc]init];

    _arrTextField=[NSArray  arrayWithObjects:_txtName,_txtIngredients,_txtPreper,_txtServe,_txtFrom ,nil];

    NSLog(@"send email");

    if ([MFMailComposeViewController canSendMail])
    {
    NSMutableArray*recipients=[[NSMutableArray alloc]init];

    [recipients addObject:@"maya1580@gmail.com"];
    MFMailComposeViewController *controller= [[MFMailComposeViewController alloc]init];
    controller.mailComposeDelegate= self;
    [controller addAttachmentData:_imgDrink mimeType:@"image/png" fileName:@"Myimage"];
    [controller setSubject:@"my cocktail"];
    [controller setMessageBody: _arrTextField isHTML:NO];
    [controller setToRecipients:recipients];
    }
    else
    {
        UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Your devise is not set up for Email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];

        [alert show];
       //  [alert release];
    }
4

3 回答 3

0

你不能自动发送电子邮件,因为苹果不会让你在没有用户知识的情况下做这种事情。您必须出示 presentModalView!

于 2012-10-27T21:37:55.467 回答
0

您永远不会提供邮件控制器。它需要像任何其他模态视图控制器一样呈现。

于 2012-10-27T21:04:59.627 回答
0

这应该会有所帮助:

-(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)sendCode:(id)sender {
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

        mailer.mailComposeDelegate = self;

        [mailer setSubject:@"Your subject"];

        NSArray *toRecipients = [NSArray arrayWithObjects:@"maya1580@gmail.com", nil];
        [mailer setToRecipients:toRecipients];

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

e"];


        NSString *emailBody =
        @"your body";


        [mailer setMessageBody:emailBody isHTML:NO];


        [mailer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentModalViewController:mailer animated:YES];

    }
    else {
        {
            UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Error"
                                                             message:@"Your device doesnt support that action."
                                                            delegate:nil
                                                   cancelButtonTitle:@"Okay"
                                                   otherButtonTitles:nil];

            [alert2 show];



        }
    }
}

并在 .h 文件中添加:

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface Controller : UIViewController <MFMailComposeViewControllerDelegate> {


}

-(IBAction)send:(id)sender;

@end
于 2012-10-27T21:07:39.033 回答