在我的申请中,我必须向客户的电子邮件发送反馈。
这是我的代码,
-(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设置或其他? 我尝试了Apple的MailComposer示例,但它也不起作用。
请帮帮我。
答案 0 :(得分: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];
答案 1 :(得分:0)
尝试使用不同的格式:
NSString *mailString = [NSString stringWithFormat:@"mailto:%@?subject=%@&body=%@",
[to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];