我做了一个示例ipad邮件编写器应用程序将图像发送到另一个地址。所以我写了以下代码:
#import <messageUI/MFMailComposeViewController.h>
//to compose mail
-(IBAction)composeMail{
if([self validateImageView]){
[self sendSelectedImage];
}
else{
[self showAlert];
}
}
//to validate image view
-(BOOL)validateImageView{
if(selectedImageView.image){
return YES;
}
else{
return NO;
}
}
//to send selected image
-(void)sendSelectedImage{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
@try {
picker.mailComposeDelegate = self;
[picker setSubject:@"Hello from Triassic!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"shamsudheen@triassicsolutions.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"shamsudheen@triassicsolutions.com", @"shamsudheen@triassicsolutions.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"shamsudheen@triassicsolutions.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Attach an image to the email
NSData *myData = UIImagePNGRepresentation(selectedImageView.image);
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"];
// Fill out the email body text
NSString *emailBody = @"It is raining in Trivandrum!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
@catch (NSException * ex) {
NSLog([NSString stringWithFormat:@"%@",ex]);
}
@finally {
[picker release];
}
}
//to show a alert box
-(void)showAlert{
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:@"Please select a image from PhotoAlbums!" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Continue", nil];
[alertView show];
[alertView release];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation==UIInterfaceOrientationLandscapeRight || interfaceOrientation==UIInterfaceOrientationLandscapeLeft);
}
#pragma mark -
#pragma mark Dismiss Mail/SMS view controller
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the
// message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
@try {
feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
feedbackMsg.text = @"Mail sending canceled";
break;
case MFMailComposeResultSaved:
feedbackMsg.text = @"Mail saved";
break;
case MFMailComposeResultSent:
feedbackMsg.text = @"Mail sent";
break;
case MFMailComposeResultFailed:
feedbackMsg.text = @"Mail sending failed";
break;
default:
feedbackMsg.text = @"Mail not sent";
break;
}
}
@catch (NSException * ex) {
NSLog([NSString stringWithFormat:@"%@",ex]);
}
@finally {
[self dismissModalViewControllerAnimated:YES];
}
}
因此,当撰写按钮单击时,它将显示一个弹出窗口,其中包含输入的邮件地址和所有详细信息。 它显示结果发送过程成功完成。但我没有收到任何邮件到shamsudheen@triassicsolutions.com。可能我知道我做了什么错误。我通过进入弹出窗口通过这个发送邮件到另一封电子邮件地址section.i认为compose方法在弹出加载时有效。然后我如何发送邮件到在显示的弹出窗口输入的地址。工作不正常..可能我知道我做错了什么
答案 0 :(得分:2)
在模拟器中,您无法发送邮件,因为如果您想首先向其他人发送邮件,则必须在设备的帐户设置中设置邮件详细信息(您必须登录到您的帐户)。但是模拟器中不存在该功能。这就是您无法从模拟器发送电子邮件的原因。