4

The following is my code to send an attachment in a mail. This works fine. I am able to send mails but I don't always receive the mails.

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

// Set the subject of email
[picker setSubject:@"My data file"];

// Add email addresses

[picker setToRecipients:[NSArray arrayWithObjects:emailId, nil]];


// Fill out the email body text
NSString *emailBody = @"Hello, \n Please find the data from the iOS app in the attachments.\n\n Thank you.\nMy Team.";

// This is not an HTML formatted email
[picker setMessageBody:emailBody isHTML:NO];

// Create NSData object from file
NSData *exportFileData = [NSData dataWithContentsOfFile:filePath];

// Attach image data to the email
[picker addAttachmentData:exportFileData mimeType:@"text/csv" fileName:  [self.CSVNameTextField text]];


// Show email view

if ([MFMailComposeViewController canSendMail]) {

    [self presentModalViewController:picker animated:YES];
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Nik
  • 2,913
  • 7
  • 40
  • 66
  • Are you testing in Simulator ? – βhargavḯ Mar 27 '13 at 15:48
  • 4
    Side note - why do you create the mail compose controller and then check if mail can be sent? You should only create the controller and set it up if you can send mail. – rmaddy Mar 27 '13 at 15:57
  • 1
    You say this code only works sometimes. Then you say you can't send mail from the app. Which is it? Can you sometimes send mail from the app or never from the app? – rmaddy Mar 27 '13 at 15:58
  • I am able to send the mail all the time but I dont receive the mails. I receive mails sometimes. – Nik Mar 27 '13 at 16:30
  • I have changed the contents of my question to be more clear what is happening – Nik Mar 27 '13 at 16:32
  • @Raj Where are you sending the emails to? Are you able to successfully receive other emails with attachments at that address? – John Topley Mar 27 '13 at 16:36
  • @JohnTopley Yes I am able to get the mails with attachments. I also tried sending it without attachment.. But it does not work as I mentioned. – Nik Mar 27 '13 at 16:40
  • @Raj Have you tried sending them to another email address? – John Topley Mar 27 '13 at 16:43
  • @JohnTopley yes... no success – Nik Mar 27 '13 at 16:47
  • If you send the email to a given address, do you always get the email for that address or do you only get it sometimes for that email address? Check your Junk or Spam folders. There is nothing wrong with your code unless the attachment is too large. Do you ever send an attachment that is more than 5MB, 10MB, or 15MB? – rmaddy Mar 27 '13 at 23:34
  • @rmaddy I see that I get emails only sometimes no matter if I am attaching something to it or not. this happens regardless the attachment. – Nik Mar 28 '13 at 09:36
  • @Raj That's not what I asked. If you send 10 emails to the same user, does that user always get all 10? Or does a given user only get some of them? – rmaddy Mar 28 '13 at 15:21
  • @rmaddy the user will get only some of them and not all. – Nik Mar 28 '13 at 15:33

3 Answers3

6

After you send the Mail with your App go to the Mail-Software on your iPhone, you will most likely find the mail in the Outbox.

Cause the MFMailComposeViewController will just forward the Mail to the Mail-Software and it doesn't care what happens next to the message. So it's up to your Mail-Software how and when the Outbox will be updated.

brianng
  • 5,790
  • 1
  • 32
  • 23
xapslock
  • 1,119
  • 8
  • 21
2

I had the same problem, I found that it would go through the mail box and say it had sent and then nothing would come through.

About 20-30 minutes later the first one came through and then gradually the rest I sent came through too.

I don't know why it takes so long, if I find out I will edit this answer, but definitely wait up to an hour before assuming your code is broken.

Hope this helps someone who, like me, might be trawling their code over and over

sam_smith
  • 6,023
  • 3
  • 43
  • 60
  • Did you ever find a more lasting solution? I am also experiencing this issue. Email gets queued, but (sometimes) not sent. – elsurudo Aug 09 '16 at 16:01
1

In my case i had to manually open up the iphone mail app, then mails were sent and received immediately.

M.Yessir
  • 202
  • 3
  • 11