如何从应用程序内发送电子邮件

时间:2012-10-27 20:55:26

标签: ios email

我正在尝试发送包含来自文本字段和图片的数据的电子邮件,但它不起作用,请提供建议。这是我的代码:

     - (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];
    }

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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

答案 2 :(得分:0)

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