2009-09-29 3 views
5

메일 앱을 시작한 다음 내 앱으로 돌아가서 내 앱에서 이메일을 보내는 방법을 알고 있지만 메일 앱을 열지 않고 이메일을 보낼 수 있기를 바랍니다. 예를 들어 내 앱에서 버튼을 클릭하면 해당 버튼을 클릭하면 이메일이 전송됩니다. 그런 다음 이메일이 전송되었음을 사용자에게 알립니다.iphone 앱에서 이메일을 보내십시오

아무도이 작업을 수행하지 않았습니까?

감사합니다.

사미

+0

받는 사람을 어떻게 선택합니까? 또는 하드 코딩 된 것입니까? – Tim

답변

3

이 작업을 수행하는 가장 좋은 방법은 메일을 발송하지 앱 웹 서버를 가지고있다. 전자 메일의 세부 정보를 전달하고 서버가 사용자 대신 전자 메일을 보내도록하십시오.

+0

웹 서버에 액세스 할 수없는 경우 문제가 발생합니다. 그런 다음 나중에 웹 서버에 다시 시도하기 위해 메시지를 대기열에 넣어야합니다. 하지만 앱이 나중에 실행되지 않을 수 있습니다. 아이폰이 백그라운드 처리를 허용한다면, 화면이 잠겨 있거나 몇 분 동안 사용자가 아무런 반응을 보이지 않는다면 한계가있다. – mahboudz

4

몇 가지 선택 사항이 있습니다. Apple의 MFMailComposeViewController 클래스 (아래 참조)를 사용하면 Mail 앱을 실행하거나 떠나지 않고도 앱에서 메시지를 만들어 iPhone의 Mail로 전달할 수 있습니다. 앱에 SMTP를 구현하여 전자 메일을 직접 보낼 수도 있습니다. 이메일을 웹 서버에 전달하고 웹 서버로 보낼 수도 있습니다. 가장 쉬운 방법은 첫 번째 방법입니다. 단점은 메시지가 전송되었는지 여부를 알지 못하는 것입니다. 네트워크가 작동하는지 여부 및 기타 요인에 따라 달라집니다. 물론 자신의 SMTP 코드를 사용하면 네트워크 또는 서버를 사용할 수없는 경우 모든 대기열을 처리하고 다시 시도해야하므로 해당 작업을 수행하려면 앱이 실행 중이어야합니다. Apple's docs에서

:

MFMailComposeViewController 클래스는 편집 및 이메일 메시지를 전송을 관리하는 표준 인터페이스를 제공합니다. 이보기 컨트롤러를 사용하여 응용 프로그램 내부에 표준 전자 메일보기를 표시하고 제목, 전자 메일받는 사람, 본문 텍스트 및 첨부 파일과 같은 초기 값으로 해당보기의 필드를 채 웁니다. 사용자는 지정한 초기 내용을 편집하고 전자 메일 보내기 또는 작업 취소를 선택할 수 있습니다.

+0

고마워, 아마 메일 응용 프로그램을 시작하지 않고 먼저 MFMailComposeViewController를 사용하여 시도해 볼 것입니다 ... – sami

9

다음은 MFMailComposeViewController를 사용하여 이메일을 보내는 샘플 코드입니다. buildphases에서

-(IBAction)showPicker:(id)sender { 
// This sample can run on devices running iPhone OS 2.0 or later 
// The MFMailComposeViewController class is only available in iPhone OS 3.0 or later. 
// So, we must verify the existence of the above class and provide a workaround for devices running 
// earlier versions of the iPhone OS. 
// We display an email composition interface if MFMailComposeViewController exists and the device can send emails. 
// We launch the Mail application on the device, otherwise. 

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

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

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
picker.mailComposeDelegate = self; 

[picker setSubject:@"Hello from California!"]; 


// Set up recipients 
NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; 
NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; 

[picker setToRecipients:toRecipients]; 
[picker setCcRecipients:ccRecipients]; 
[picker setBccRecipients:bccRecipients]; 

// Attach an image to the email 
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"]; 
NSData *myData = [NSData dataWithContentsOfFile:path]; 
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"]; 

// Fill out the email body text 
NSString *emailBody = @"It is raining in sunny California!"; 
[picker setMessageBody:emailBody isHTML:NO]; 

[self presentModalViewController:picker animated:YES]; 
[picker release]; 
} 


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of   the operation. 
message.hidden = NO; 
// Notifies users about errors associated with the interface 
switch (result) 
{ 
    case MFMailComposeResultCancelled: 
     message.text = @"Result: canceled"; 
     break; 
    case MFMailComposeResultSaved: 
     message.text = @"Result: saved"; 
     break; 
    case MFMailComposeResultSent: 
     message.text = @"Result: sent"; 
     break; 
    case MFMailComposeResultFailed: 
     message.text = @"Result: failed"; 
     break; 
    default: 
     message.text = @"Result: not sent"; 
     break; 
} 
[self dismissModalViewControllerAnimated:YES]; 
} 

-(void)launchMailAppOnDevice { 

// Launches the Mail application on the device. 
NSString *recipients = @"mailto:[email protected][email protected],[email protected]&subject=Hello from California!"; 
NSString *body = @"&body=It is raining in sunny California!"; 

NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body]; 
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]]; 
} 
0

추가 프레임 워크 MessageUI.framework

ViewController.h는 파일

#import <MessageUI/MessageUI.h> 

    @interface ViewController() <MFMailComposeViewControllerDelegate> 

ViewController.m 파일

-(IBAction)emailButtonClicked:(id)sender{ 

     MFMailComposeViewController *mailComposer =[[MFMailComposeViewController alloc] init]; 
     if (mailComposer !=nil) { 
      mailComposer.mailComposeDelegate = self; 
      NSString *emailBody = @"Write the text here........"; 
      [mailComposer setMessageBody:emailBody isHTML:NO]; 
      [self presentModalViewController:mailComposer animated:YES]; 
     } 
     } 

     - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
      [self becomeFirstResponder]; 
      [self dismissModalViewControllerAnimated:YES]; 
     } 
관련 문제