3

我正在开发一个应用程序,我在没有 IOS 邮件 API 的情况下发送邮件(邮件是具有 SES 亚马逊服务的发件人,无需用户交互和附加文件......)。在我的 API 中,我必须使用所有 MIME 协议发送编码邮件(这不是问题)。我用 charset ascii 和我在 base64 中附加的 pdf 发送很好的邮件编码正文:

   CFUUIDRef    uuidRef   = CFUUIDCreate(kCFAllocatorDefault);
NSString    *uuid     = (__bridge_transfer  NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
CFRelease(uuidRef);

//Encapsulation
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableString *rawMime = [[NSMutableString alloc] init];
[rawMime appendFormat:@"To: %@\n", _to];
[rawMime appendFormat:@"From: \"%@\" <boutique@tactill.com>\n", [defaults objectForKey:@"companyName"]];
[rawMime appendFormat:@"Reply-To: %@\n", [defaults objectForKey:@"toEmailForStats"]];
[rawMime appendFormat:@"BCC: %@\n", [defaults objectForKey:@"toEmailForStats"]];
[rawMime appendFormat:@"Subject: %@\n", _subject];
[rawMime appendString:@"Date: Thu, 05 Jan 95 10:53:24 -0500\n"];
[rawMime appendFormat:@"Message-ID: <%@@%@>\n", [(NSString *)uuid stringByReplacingOccurrencesOfString:@"-" withString:@""], @"IETF.CNR I.Reston.VA.US"];
[rawMime appendString:@"Mime-Version: 1.0\n"];
[rawMime appendString:@"Content-type: Multipart/Mixed; boundary=\"NextPart\"\n"];
[rawMime appendString:@"\n"];
[rawMime appendString:@"--NextPart\n"];

//Body
[rawMime appendString:@"Content-type: text/plain; charset=\"us-ascii\"\n"];
[rawMime appendString:@"\n"];
[rawMime appendString:_body];
[rawMime appendString:@"\n"];
[rawMime appendString:@"--NextPart\n"];

//Attach pdf
[rawMime appendString:@"Content-Type: application/pdf; "];
[rawMime appendFormat:@"name=\"%@.pdf\";\n", _pdfName];
[rawMime appendFormat:@"Content-Transfer-Encoding: base64\r\n\r\n"];
[rawMime appendFormat:@"%@\n",_pdfAttached];
[rawMime appendString:@"\n"];

//If necessary attach csv
if (_csvAttached!=nil) {
    [rawMime appendString:@"--NextPart\n"];
    [rawMime appendString:@"Content-Type: text/plain; "];
    [rawMime appendFormat:@"name=\"%@.txt\";\n", _csvName];
    [rawMime appendFormat:@"Content-Transfer-Encoding: base64\r\n\r\n"];
    [rawMime appendFormat:@"%@\n",_csvAttached];
    [rawMime appendString:@"\n"];
    [rawMime appendString:@"--NextPart\n"];
}


NSData        *rawMessageData = [rawMime dataUsingEncoding:NSUTF8StringEncoding];

当我用 gmail 或邮件应用程序(OSX 或 IOS)打开邮件时,一切都很完美,但是当我用 Windows 邮件打开它时,特殊字符被更改为垃圾邮件。我已经阅读过它,并且我看到 Windows 邮件读取 iso-8859-1(NSISOLatin1StringEncoding)。所以我在那个帖子的帮助下试过:[http://stackoverflow.com/questions/4553388/how-to-convert-utf8-encoding-to-iso-8859-1-encoding][1] 做那个代码:

   //add body
char converted[([_body length] + 1)];
[_body getCString:converted maxLength:([_body length] + 1) encoding: NSISOLatin1StringEncoding];   
[rawMime appendString:@"Content-type: text/plain; charset=iso-8859-1\n"];
[rawMime appendFormat:@"Content-Transfer-Encoding: quoted-printable\r\n\r\n"];
[rawMime appendFormat:@"%s", converted];
[rawMime appendString:@"\n"];
[rawMime appendString:@"--NextPart\n"];

现在所有的邮件客户端都向我展示了这个:Merci pour votre achat。¿ bientâ™t!和 NSLog 一样,当我只打印正文时我做错了什么?我怎样才能很好地编码邮件的正文?

4

1 回答 1

3

这是一个回答这个问题的工作代码,它添加了一个pdf,偶尔也添加了一个csv

-(NSData*)formatedMail{


    CFUUIDRef   uuidRef   = CFUUIDCreate(kCFAllocatorDefault);
    NSString    *uuid     = (__bridge_transfer  NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
    CFRelease(uuidRef);

    NSDate* today = [[NSDate alloc] init];
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

    NSString* date =[dateFormatter stringFromDate:today];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableString *rawMime = [[NSMutableString alloc] init];
    [rawMime appendFormat:@"To: %@\n", email_to];
    [rawMime appendFormat:@"From: \"name_from" <e-mail_from>\n", [defaults objectForKey:@"companyName"]];
    [rawMime appendFormat:@"Reply-To: %@\n", [defaults objectForKey:@"toEmailForStats"]];
    [rawMime appendFormat:@"BCC: %@\n", [defaults objectForKey:@"toEmailForStats"]];
    [rawMime appendFormat:@"Subject: %@\n", _subject];
    [rawMime appendFormat:@"Date: %@\n", date];
    [rawMime appendFormat:@"Message-ID: <%@@%@>\n", [(NSString *)uuid stringByReplacingOccurrencesOfString:@"-" withString:@""], @"IETF.CNR I.Reston.VA.US"];
    [rawMime appendString:@"Mime-Version: 1.0\n"];
    [rawMime appendString:@"Content-type: Multipart/Mixed; boundary=\"NextPart\"\n"];
    [rawMime appendString:@"\n"];
    [rawMime appendString:@"--NextPart\n"];

    //Here's come the body part
    [rawMime appendString:@"Content-type: text/plain; charset=\"UTF-8\"\n"];
    [rawMime appendString:@"\n"];
    [rawMime appendString:_body];
    [rawMime appendString:@"\n"];
    [rawMime appendString:@"--NextPart\n"];

    //Attach pdf
    [rawMime appendString:@"Content-Type: application/pdf; "];
    [rawMime appendFormat:@"name=\"%@.pdf\";\n", _pdfName];
    [rawMime appendFormat:@"Content-Transfer-Encoding: base64\r\n\r\n"];
    [rawMime appendFormat:@"%@\n",_pdfAttached];
    [rawMime appendString:@"\n"];

    //If necessary attach csv
    if (_csvAttached!=nil) {
        [rawMime appendString:@"--NextPart\n"];
        [rawMime appendString:@"Content-Type: application/octet-stream; "];
        [rawMime appendFormat:@"name=\"%@\";\n", _csvName];
        [rawMime appendFormat:@"Content-Transfer-Encoding: base64\r\n\r\n"];
        [rawMime appendFormat:@"%@\n",_csvAttached];
        [rawMime appendString:@"\n"];
        [rawMime appendString:@"--NextPart\n"];
    }


    NSData        *rawMessageData = [rawMime dataUsingEncoding:NSUTF8StringEncoding];

    return rawMessageData;
}

_pdfAttached 是 NSString 代表以 base64 编码的 PDF 我希望它有所帮助,很抱歉之前没有发布解决方案

这是不发送但创建邮件正文的代码。在这里,您可以执行使用 SESSendRawEmailRequest 无法执行的操作,例如在 to 和 from 中添加“显示名称”。我建议您使用这两个部分来定义发件人,您会看到它提供了只有 SESSendRawMessage 无法提供的可能性。最后说,如果你不想有例外,你不应该忘记在 SESSEndRawEmailRequest 中放置收件地址和发件人地址。

于 2012-05-23T08:19:09.517 回答