1

我正在尝试使用 iOS 中的 MailCore 框架发送消息。这是我的 View Did Load 方法:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

myMessage = [[CTCoreMessage alloc] init];

// Set To
CTCoreAddress *addrTo = [CTCoreAddress address];
[addrTo setEmail:@"aaa@gmail.com"];
[myMessage setTo:[NSSet setWithObject:addrTo]];

// Set From 
CTCoreAddress *addrFrom = [CTCoreAddress address];
[addrFrom setEmail:@"bbb@gmail.com"];
[addrFrom setName:@"bb"];
[myMessage setFrom:[NSSet setWithObject:addrFrom]];

[myMessage setSubject:@"test"];
[myMessage setHTMLBody:@"Отправил это сообщение через SMTP с TSL (используя MailCore) :)"];

[CTSMTPConnection sendMessage:myMessage server:@"smtp.gmail.com" username:@"bbbb"
                     password:@"ccc" port:587 useTLS:YES useAuth:YES];
[myMessage release];
}

结果我收到了一封电子邮件,由于某种原因,它被缩短了:

Отправил это сообщение через SMTP с T

可能是什么原因。如您所见,我使用端口 587 和 useTLS = YES 的 gmail.com。

PS 当我使用 mail.ru 而不是 gmail.com 时,它是一样的。看来,不是服务器问题。

4

4 回答 4

2

这看起来非常像编码问题。如果您首先对非 ascii 字符进行 HTML 转义怎么办?

澄清一下,似乎有人在询问 HTMLBody 的长度(以字符为单位),然后假设这是要发送的正确 UTF-8 字节数。

于 2012-06-25T00:42:07.017 回答
1

这已在最新版本的 MailCore 中得到修复。这是提交https://github.com/mronge/MailCore/commit/1d833fb5996e2350aeaba9a1f04acb33b7cae84c

于 2012-07-04T03:00:20.970 回答
0

这是 libEtPan Dinh Viet Hoa 的作者提供的解决方案:

问题在于这种方法(CTMIME_HtmlPart.m):

- (struct mailmime *)buildMIMEStruct {
    struct mailmime_fields *mime_fields;
    struct mailmime *mime_sub;
    struct mailmime_content *content;
    struct mailmime_parameter *param;
    int r;

    /* text/html part */
    //TODO this needs to be changed, something other than 8BIT should be used
    mime_fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_8BIT);
    assert(mime_fields != NULL);

    content = mailmime_content_new_with_str("text/html");
    assert(content != NULL);

    param = mailmime_parameter_new(strdup("charset"), strdup(DEST_CHARSET));
    assert(param != NULL);

    r = clist_append(content->ct_parameters, param);
    assert(r >= 0);

    mime_sub = mailmime_new_empty(content, mime_fields);
    assert(mime_sub != NULL);
    NSString *str = [self content];
    //TODO is strdup necessary?
    r = mailmime_set_body_text(mime_sub, strdup([str cStringUsingEncoding:NSUTF8StringEncoding]), [str length]);
    assert(r == MAILIMF_NO_ERROR);
    return mime_sub;
}

您可以看到您将 [str cStringUsingEncoding:NSUTF8StringEncoding] 作为参数传递。而不是传递该数据的大小,而是传递[str length],这是该字符串的字符数。在这里你应该做什么:

NSData * data = [str dataUsingEncoding:NSUTF8String];
r = mailmime_set_body_text(mime_sub, strdup([data bytes]), [data length]);
于 2012-06-27T08:15:24.840 回答
0

最后,经过几个小时的尝试,我解决了这个问题。

libEtPan 中的这个方法应该这样改变:

    int mailmime_set_body_text(struct mailmime * build_info,
               char * data_str, size_t length)
{
  int encoding;
  struct mailmime_data * data;

  encoding = mailmime_transfer_encoding_get(build_info->mm_mime_fields);

  // Added by Dennis !!!Here Russian message was cut off!!!
    // The reason is that size_t length is a count of chars but used for data length (which is twice as count of chars in UTF-8)
    // That's why I take the lenght of actual data_str, not length
    if (encoding == 2) {
        length = strlen(data_str);
    }

  data = mailmime_data_new(MAILMIME_DATA_TEXT, encoding,
               0, data_str, length, NULL);
  if (data == NULL)
    return MAILIMF_ERROR_MEMORY;

  build_info->mm_data.mm_single = data;

  return MAILIMF_NO_ERROR;
}
于 2012-06-26T10:21:30.337 回答