gpt4 book ai didi

iphone - 无法使用 MFMailComposeViewController 从应用程序发送电子邮件

转载 作者:行者123 更新时间:2023-12-03 18:56:01 28 4
gpt4 key购买 nike

我在尝试从我的应用程序发送电子邮件时遇到了一些困难。我尝试了 iCodeBlog 中的代码 ( http://icodeblog.com/2009/11/18/iphone-coding-tutorial-in-application-emailing/ )

-(void)sendEmail:(id)sender{    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];    mail.mailComposeDelegate = self;    if ([MFMailComposeViewController canSendMail]) {            //Setting up the Subject, recipients, and message body.        [mail setToRecipients:[NSArray arrayWithObjects:@"myEmail@email.com",nil]];        [mail setSubject:@"Subject of Email"];        [mail setMessageBody:@"Message of email" isHTML:NO];            //Present the mail view controller        [self presentModalViewController:mail animated:YES];    }        //release the mail    [mail release];}    //This is one of the delegate methods that handles success or failure    //and dismisses the mail- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{    [self dismissModalViewControllerAnimated:YES];    if (result == MFMailComposeResultFailed) {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message Failed!" message:@"Your email has failed to send" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];        [alert show];        [alert release];    }}

它说它发送了电子邮件并且没有发生错误,但我从未在收件箱中收到电子邮件。我尝试将它们发送到不同的电子邮件帐户,并尝试从不同的帐户发送它们,没有发生错误,但我从未收到电子邮件。有什么想法吗?

如果它很重要,当我开始输入“收件人:电子邮件”时,我会在调试器控制台上收到此消息

DA|Could not open the lock file at /tmp/DAAccountsLoading.lock. We'll load the accounts anyway, but bad things may happen

=====编辑======

我刚刚意识到所有这些电子邮件都发送到了我在 Mail.app 上的发件箱。当我点击发送时它们不是自动发送吗?如果没有,那么当用户按下 MFMailComposeView 上的“发送”按钮时,我该怎么做才能发送它们?或者也许调用 Mail.app 并发送这些电子邮件。

最佳答案

使用这个代码肯定会起作用:

    -(IBAction)send{

[self callMailComposer];
}

-(void)callMailComposer{

Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
[self displayComposerSheet];
else
[self launchMailAppOnDevice];
}

else
{
[self launchMailAppOnDevice];
}
}


#pragma mark -
#pragma mark Compose Mail
#pragma mark

// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet{

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];


picker.mailComposeDelegate = self;
NSString *tosubject =@"";
[picker setSubject:tosubject];


// Set up recipients
[picker setCcRecipients:nil];
[picker setBccRecipients:nil];

[picker setToRecipients:nil];



[picker setMessageBody:strNewsLink isHTML:NO];

[self presentModalViewController:picker animated:YES];

if(picker) [picker release];
if(picker) picker=nil;

}


// 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
{
NSString* alertMessage;
// message.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
alertMessage = @"Email composition cancelled";
break;
case MFMailComposeResultSaved:
alertMessage = @"Your e-mail has been saved successfully";

break;
case MFMailComposeResultSent:
alertMessage = @"Your email has been sent successfully";

break;
case MFMailComposeResultFailed:
alertMessage = @"Failed to send email";

break;
default:
alertMessage = @"Email Not Sent";

break;
}

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"My app name"
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[self dismissModalViewControllerAnimated:YES];
}


#pragma mark
#pragma mark Workaround
#pragma mark
// Launches the Mail application on the device.

-(void)launchMailAppOnDevice{

NSString *recipients = @"mailto:?cc=&subject=";
NSString *body = @"&body=";
NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

}

关于iphone - 无法使用 MFMailComposeViewController 从应用程序发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4285597/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com