2009-10-01 82 views
0

在我的應用程序中,我必須發送反饋給客戶的電子郵件。在iPhone中發送電子郵件

這裏是我的代碼,

-(void) send:(id) sender { 
    [self sendEmailTo:[txtTo text] withSubject:[txtSubject text] withBody:[txtBody text]]; 
} 

-(void) sendEmailTo:(NSString *)to withSubject:(NSString *) subject withBody:(NSString*)body { 
    NSString *mailString = [NSString stringWithFormat:@"mailto:?to=%@&subject=%@body=%@", 
    [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
    [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
    [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 

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

但是,這是行不通的。它需要任何類型的SMTP設置或其他? 我嘗試過蘋果的MailComposer示例,但它也不起作用。

請幫幫我。

回答

5

我已經發布這個here,但不會再次點擊的緣故這裏是:


您需要將MessageUI.framework包括到你的項目,你的頭文件裏面你需要設置代理:

#import <MessageUI/MessageUI.h> 
@interface RootViewController : UIViewController <MFMailComposeViewControllerDelegate> { 
    MFMailComposeViewController *email; 
} 

@property (nonatomic, retain) MFMailComposeViewController *email; 

一旦你這樣做,你有你的執行文件中的一些委託方法你需要包括(你應該檢查的結果,但我想,以維持儘可能少的代碼):

@synthesize email; 

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { 
    [email dismissModalViewControllerAnimated:YES]; 
} 

不管你想用這個,你需要初始化和設置它是這樣的:

email = [[MFMailComposeViewController alloc] init]; 
email.mailComposeDelegate = self; 

// Subject 
[email setSubject:@"Testing"]; 

// Optional Attachments 
NSData *artwork = UIImagePNGRepresentation([UIImage imageNamed:@"albumart.png"]); 
[email addAttachmentData:artwork mimeType:@"image/png" fileName:@"albumart.png"]; 

// Body 
[email setMessageBody:@"This is the body"]; 

// Present it 
[self presentModalViewController:email animated:YES]; 
0

嘗試使用不同的格式:

NSString *mailString = [NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@", 
    [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
    [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
    [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];