summaryrefslogtreecommitdiff log msg author committer range
path: root/kmime/Mainpage.dox
blob: 7f47e488025872b5ae7235a02f487005abf37128 (plain)
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201  /** \mainpage The KMime Library \section introduction Introduction KMime is a library for handling mail messages and newsgroup articles. Both mail messages and newsgroup articles are based on the same standard called MIME, which stands for Multipurpose Internet Mail Extensions. In this document, the term \c message is used to refer to both mail messages and newsgroup articles. KMime deals solely with the in-memory representation of messages, topics such a transport or storage of messages are handled by other libraries, for example by the mailtransport library or by the KIMAP library. Similary, this library does not deal with displaying messages or advanced composing, for those there are the messageviewer and the messagecomposer components in the KDEPIM module. KMime's main function is to parse, modify and assemble messages in-memory. In a \ref string-broken-down "later section", parsing and assembling is actually explained. KMime provides high-level classes that make these tasks easy. MIME is defined by various RFCs, see the \ref rfcs "RFC section" for a list of them. \section structure Structure of this document This document will first give an \ref mime-intro "introduction to the MIME specification", as it is essential to understand the basics of the structure of MIME messages for using this library. The introduction here is aimed at users of the library, it gives a broad overview with examples and omits some details. Developers who wish to modifiy KMime should read the \ref rfcs "corresponding RFCs" as well, but this is not necessary for library users. After the introduction to the MIME format, the two ways of representing a message in memory are discussed, the \ref string-broken-down "string representation and the broken down representation". This is followed by a section giving an \ref classes-overview "overview of the most important KMime classes". The last sections give a list of \ref rfcs "relevant RFCs" and provide links for \ref links "further reading". \section mime-intro Structure of MIME messages \subsection history A brief history of the MIME standard The MIME standard is quite new (1993), email and usenet existed way before the MIME standard came into existence. Because of this, the MIME standard has to keep backwards compatibility. The email standard before MIME lacked many capabilities like encodings other than ASCII or attachments. These and other things were later added by MIME. The standard for messages before MIME is defined in RFC 5322. In RFC 2045 to RFC 2049, several backward-compatible extensions to the basic message format are defined, adding support for attachments, different encodings and many others. Actually, there is an even older standard, defined in RFC 733 (Standard for the format of ARPA network text messages, introduced in 1977). This standard is now obsoleted by RFC 5322, but backwards compatibilty is in some cases supported, as there are still messages in this format around. Since pre-MIME messages had no way to handle attachments, attachments were sometimes added to the message text in an uuencoded form. Although this is also obsolete, reading uuencoded attachments is still supported by KMime. After MIME was introduced, people realized that there is no way to have the filename of attachments encoded in anything different than ASCII. Thus, RFC 2231 was introduced to allow abitrary encodings for parameter values, such as the attachment filename. \subsection examples MIME by examples In the following sections, MIME message examples are shown, examined and explained, starting with a simple message and proceeding to more interesting examples. You can get additional examples by simply viewing the source of your own messages in your mail client, or by having a look at the examples in the \ref rfcs "various RFCs". \subsubsection simple-mail A simple message \verbatim Subject: First Mail From: John Doe Date: Sun, 21 Feb 2010 19:16:11 +0100 MIME-Version: 1.0 Hello World! \endverbatim The above example features a very simple message. The two main parts of this message are the \b header and the \b body, which are seperated by an empty line. The body contains the actual message content, and the header contains metadata about the message itself. The header consists of several header fields, each of them in their own line. Header fields are made up from the header field name, followed by a colon, followed by the header field body. The \b MIME-Version header field is mandatory for MIME messages. \b Subject, \b From and \b Date are important header fields, they are usually displayed in the message list of a mail client. The \c Subject header field can be anything, it does not have a special structure. It is a so-called \b unstructured header field. In contrast, the \c From and the \c Date header fields have to follow a special structure, they must be formed in a way that machines can parse. They are \b structured header fields. For example, a mail client needs to understand the \c Date header field so that it can sort the messages by date in the message list. The exact details of how the header field bodies of structured header fields should be formed are specified in an RFC. In this example, the \c From header contains a single email address. More precisly, a single email address is called a \b mailbox, which is made up of the display name (John Doe) and the address specification (john.doe@domain.com), which is enclosed in angle brackets. The \c addr-spec consists of the user name, the local part, and the \b domain name. Many header fields can contain multiple email addresses, for example the \c To field for messages with multiple recipients can have a comma-seperated list of mailboxes. A list of mailboxes, together with a display name for the list, forms a \b group, and multiple groups can form an address list. This is however rarely used, you'll most often see a simple list of plain mailboxes. There are many more possible header fields than shown in this example, and the header can even contain abitrary header fields, which usually are prefixed with \c X-, like \c X-Face. \subsubsection encodings Encodings and charsets \verbatim From: John Doe Date: Mon, 22 Feb 2010 00:42:45 +0100 MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Gr=FCezi Welt! \endverbatim The above shows a message that is using a different \b charset than the standard \b US-ASCII charset. The message body contains the string "Grüezi Welt!", which is \b encoded in a special way. The \b content-type of this message is \b text/plain, which means that the message is simple text. Later, other content types will be introduced, such as \b text/html. If there is no \c Content-Type header field, it is assumed that the content-type is \c text/plain. Before MIME was introduced, all messages were limited to the US-ASCII charset. Only the lower 127 values of the bytes were allowed to be used, the so-called \b 7-bit range. Writing a message in another charset or using letters from the upper 127 byte values was not allowed. \par Charset Encoding When talking about charsets, it is important to understand how strings of text are converted to byte arrays, and the other way around. A message is nothing else than a big array of bytes. The bytes that form the body of the message somehow need to be interpreted as a text string. Interpreting a byte array as a text string is called \b decoding the text. Converting a text string to a byte array is called \b encoding the text. A \b codec (coder-decoder) is a utility that can encode and decode text. In Qt, the class for text strings is QString, and the class for byte arrays is QByteArray. The base class of all codecs is QTextCodec. With the US-ASCII charset, encoding and decoding text is easy, one just has to look at an ASCII table to be able to convert text strings to byte arrays and byte arrays to text strings. For example, the letter 'A' is represented by a single byte with the value of 65. When encountering a byte with the value 84, we can look that up in the table and see that it represents the letter 'T'. With the US-ASCII charset, each letter is represented by exactly one byte, which is very convenient. Even better, all letters commonly used in English text have byte values below 127, so the 7-bit limit of messages is no problem for text encoded with the US-ASCII charset. Another example: The string "Hello World!" is represented by the following byte array:
48 65 6C 6C 6F 20 57 6F 72 6C 64
Note that the byte values are written in hexadecimal form here, not in decimal as earlier. Now, what if we want to write a message that contains German umlauts or Chinese letters? Those are not in the ASCII table, therefore a different charset has to be used. There is a wealth of charsets to chose from. Not all charsets can handle all letters, for example the ISO-8859-1 charset can handle German umlauts, but can not handle Chinese or Arabic letters. The Unicode standard is an attempt to introduce charsets that can handle all known letters in the world, in all languages. Unicode actually has several charsets, for example UTF-8 and UTF-16. In an ideal world, everyone would be using Unicode charsets, but for historic and legacy reasons, other charsets are still much in use. Charsets other than US-ASCII don't generally have as nice properties: A single letter can be represented by multiple bytes, and generally the byte values are not in the 7-bit range. Pay attention to the UTF-8 charset: At first glance, it looks exactly like the US-ASCII charset, common latin letters like A - Z are encoded with the same byte values as with US-ASCII. However, letters other than A - Z are suddenly encoded with two or even more bytes. In general, one letter can be encoded in an abitrary number of bytes, depending on the charset. One can \b not rely on the 1 letter == 1 byte assumption. Now, what should be done when the text string "Grüezi Welt!" should be sent in the body of a message? The first step is to chose a charset that can represent all letters. This already excludes US-ASCII. Once a charset is chosen, the text string is encoded into a byte array. "Grüezi Welt!" encoded with the ISO-8859-1 charset produces the following byte array:
47 72 FC 65 7A 69 20 57 65 6C 74 21
The letter 'ü' here is encoded using a single byte with the value FC. The same string encoded with UTF-8 looks slightly different:
47 72 C3 BC 65 7A 69 20 57 65 6C 74 21
Text after image --Boundary-01=_Nf9jLZ6aPhm3WrN-- --Boundary-02=_Nf9jLpJ2aGp5RQK Content-Type: image/png; name="test.png" Content-Transfer-Encoding: base64 Content-Id: <547730348@KDE> iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAIAAAAiOjnJAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAg [SNIP] AABJRU5ErkJggg== --Boundary-02=_Nf9jLpJ2aGp5RQK-- \endverbatim The first thing you'll notice in this example probably is that it has a \b multipart/related node with the following structure: \verbatim multipart/related |- multipart/alternative | |- text/plain | \- text/html \- image/png \endverbatim When the HTML part has inline image, the HTML part and its image part both have to be children of a multipart/related container, like in this example. In this case, the \c img tag has the source \c cid:547730348@KDE, which is a placeholder that refers to the Content-Id header of another part. The image part contains exactly that value in its \c Content-Id header, and therefore a message viewer application can connect both. The plain text part can not have inline images, therefore its text might seem a bit confusing. HTML messages with inline images can of course also have attachments, in which the message structure becomes a mix of multipart/related, multipart/alternative and multipart/mixed. The following example shows the structure of a message with two inline images and one \c .tar.gz attachment: \verbatim multipart/mixed |- multipart/related | |- multipart/alternative | | |- text/plain | | \- text/html | |- image/png | \- image/png \- application/x-compressed-tar \endverbatim The structure of MIME messages can get arbitrarily complex, the above is just one relativley simply example. The nesting of multipart nodes can get much deeper, there is no restriction on nesting levels. \subsubsection encapsulated Encapsulated messages Encapsulated messages are messages which are attachments to another message. The most common example is a forwareded mail, like in this example: \verbatim From: Frank To: Bob Subject: Fwd: Blub MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_sX+jLVPkV1bLFdZ" --Boundary-00=_sX+jLVPkV1bLFdZ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hi Bob, hereby I forward you an interesting message from Greg. --Boundary-00=_sX+jLVPkV1bLFdZ Content-Type: message/rfc822; name="forwarded message" Content-Transfer-Encoding: 7bit Content-Description: Forwarded Message Content-Disposition: inline From: Greg To: Frank Subject: Blub MIME-Version: 1.0 Content-Type: Text/Plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Bla Bla Bla --Boundary-00=_sX+jLVPkV1bLFdZ-- \endverbatim \verbatim multipart/mixed |- text/plain \- message/rfc822 \- text/plain \endverbatim The attached message is treated like any other attachment, and therefore the top-level content type is multipart/mixed. The most interesting part is the \c message/rfc822 MIME part. As usual, it has some MIME headers, like \c Content-Type or \c Content-Disposition, followed by the MIME body. The MIME body in this case is the attached message. Since it is a message, it consists of a header and a body itself. Therefore, the \c message/rfc822 MIME part appears to have two headers; in reality, it is the normal MIME header and the message header of the encapsulated message. The message header and the message body are both in the MIME body of the \c message/rfc822 MIME part. \subsubsection crypto Signed and Encryped Messages MIME messages can be cryptographically signed and/or encrypted. The format for those messages is defined in RFC 1847, which specifies two new multipart subtypes, \b multipart/signed and \b multipart/encrypted. The crypto format of these new security multiparts is defined in additional RFCs; the most common formats are OpenPGP and S/MIME. Both formats use the principle of public-key cryptography. OpenPGP uses \b keys, and S/MIME uses \b certificates. For easier text flow, only the term \c key will be used for both keys and certificates in the text below. Security multiparts only sign or encrypt a specifc MIME part. The consequence is that the message headers can not be signed or encrypted. Also this means that it is possible to sign or encrypt only some of the MIME parts of a message, while leaving other MIME parts unsigned or unencrypted. Furthermore, it is possible to sign or encrypt different MIME parts with different crypto formats. As you can see, security multiparts are very flexible. Security multiparts are not supported by KMime. However, it is possible for applications to use KMime when providing support for crypto messages. For example, the messageviewer component in KDEPIM supports signed and encrypted MIME parts, and the messagecomposer library can create such messages. Signed MIME parts are signed with the private key of the sender, everybody who has the public key of the sender can verifiy the signature. Encrypted MIME parts are encrypted with the public key of the receiver, and only the receiver, who is the sole person possessing the private key, can decrypt it. Sending an encrypted message to multiple recipients therefore means that the message has to be sent multiple times, once for each receiver, as each message needs to be encrypted with a different key. \par Signed MIME parts A multipart/signed MIME part has exactly two children: The first child is the content that is signed, and the second child is the signature. \verbatim From: Thomas McGuire Subject: My Subject Date: Mon, 15 Mar 2010 12:20:16 +0100 MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart2567247.O5e8xBmMpa"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit --nextPart2567247.O5e8xBmMpa Content-Type: Text/Plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Simple message --nextPart2567247.O5e8xBmMpa Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (GNU/Linux) iEYEABECAAYFAkueF/UACgkQKglv3sO8a1MdTACgnBEP6ZUal931Vwu7PyiXT1bn Zr0Anj4bAI9JhHEDiwA/iwrWGfSC+Nlz =d2ol -----END PGP SIGNATURE----- --nextPart2567247.O5e8xBmMpa-- \endverbatim \verbatim multipart/signed |- text/plain \- application/pgp-signature \endverbatim The example here uses the OpenPGP format to sign a simply plain text message. Here, the text/plain MIME part is signed, and the application/pgp-signature MIME part contains the signature data, which in this case is ASCII-armored. As said above, it is possible to sign only some MIME parts. A message which has a image/jpeg attachment that is signed, but a main text part is not signed, has the following MIME structure: \verbatim multipart/mixed |- text/plain \- multipart/signed |- image/jpeg \- application/pgp-signature \endverbatim It is possible to sign multipart parts as well. Consider the above example that has a plain text part and an image attachment. Those two parts can be signed together, with the following structure: \verbatim multipart/signed |- multipart/mixed | |- text/plain | \- image/jpeg \- application/pgp-signature \endverbatim Signed messages in the S/MIME format use a different content type for the signature data, like here: \verbatim multipart/signed |- text/plain \- application/x-pkcs7-signature \endverbatim \par Encrypted MIME parts Multipart/encrypted MIME parts also have exactly two children: The first child contains metadata about the encrypted data, such as a version number. The second child then contains the actual encrypted data. \verbatim From: someone@domain.com To: Thomas McGuire Subject: Encrypted message Date: Mon, 15 Mar 2010 12:50:16 +0100 MIME-Version: 1.0 Content-Type: multipart/encrypted; boundary="nextPart2726747.j47xUGTWKg"; protocol="application/pgp-encrypted" Content-Transfer-Encoding: 7bit --nextPart2726747.j47xUGTWKg Content-Type: application/pgp-encrypted Content-Disposition: attachment Version: 1 --nextPart2726747.j47xUGTWKg Content-Type: application/octet-stream Content-Disposition: inline; filename="msg.asc" -----BEGIN PGP MESSAGE----- Version: GnuPG v2.0.14 (GNU/Linux) hQIOA8p5rdC5CBNfEAf+NZVzVq48C1r5opOOiWV96+FUzIWuMQ6u8fzFgI7YVyCn [SNIP] =reNr --nextPart2726747.j47xUGTWKg-- -----END PGP MESSAGE----- \endverbatim \verbatim multipart/encrypted |- application/pgp-encrypted \- application/octet-stream \endverbatim The encrypted data is contained in the \c application/octet-stream MIME part. Without decrypting the data, it is unknown what the original content type of the encrypted MIME data is! The encrypted data could be a simple text/plain MIME part, an image attachment, or a multipart part. The encrypted data contains both the MIME header and the MIME body of the original MIME part, as the header is needed to know the content type of the data. The data could as well by of content type multipart/signed, in which case the message would be both signed and encrypted. \par Inline cryto formats Although using the security multiparts \c multipart/signed and \c multipart/encrypted is the recommended standard, there are other possibilities to sign or encrypt a message. The most common methods are Inline OpenPGP and S/MIME Opaque. For inline OpenPGP messages, the crypto data is contained inlined in the actual MIME part. For example, a message with a signed text/plain part might look like this: \verbatim From: someone@domain.com To: someoneelse@domain.com Subject: Inline OpenPGP test MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Inline OpenPGP signed example. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (GNU/Linux) iEYEARECAAYFAkueJ2EACgkQKglv3sO8a1MS3QCfcsYnJG7uYQxzxz6J5cPF7lHz WIoAn3PjVPlWibu02dfdFObwd2eJ1jAW =p3uO -----END PGP SIGNATURE----- \endverbatim Encrypted inline OpenPGP works in a similar way. Opaque S/MIME messages are also similar: For signed MIME parts, both the signature and the signed data are contained in a single MIME part with a content type of \c application/pkcs7-mime. As security multiparts are preferred over inline OpenPGP and over opaque S/MIME, I won't go into more detail here. \subsubsection misc Miscellaneous Points about Messages \par Line Breaks Each line in a MIME message has to end with a \b CRLF, which is a carriage return followed by a newline, which is the escape sequence\\r\\n. CR and LF may not appear in other places in a MIME message. Special care needs to be taken with encoded linebreaks in binary data, and with distinguishing soft and hard line breaks when converting between different content transfer encodings. For more details, have a look at the RFCs. While the official format is to have a CRLF at the end of each line, KMime only expects a single LF for its in-memory storage. Therefore, when loading a message from disk or from a server into KMime, the CRLFs need to be converted to LFs first, for example with KMime::CRLFtoLF(). The opposite needs to be done when storing a KMime message somewhere. Lines should not be longer than 78 characters and may not be longer than 998 characters. \par Header Folding and CFWS Header fields can span multiple lines, which was already shown in some of the examples above where the parameters of the header field value were in the next line. The header field is said to be \b folded in this case. In general, header fields can be folded whenever whitespace (\b WS) occurs. Header field values can contain \b comments; these comments are semantically invisible and have no meaning. Comments are surrouned by parentheses. \verbatim Date: Thu, 13 Feb 1969 23:32 -0330 (Newfoundland Time) \endverbatim This example shows a folded header that also has a comment (Newfoundland Time). The date header is a structured header field, and therefore it has to obey to a defined syntax; however, adding comments and whitespace is allowed almost anywhere, and they are ignored when parsing the message. Comments and whitespace where folding is allowed is sometimes referred to as \b CFWS. Any occurence of CFWS is semantically regarded as a single space. \section string-broken-down The two in-memory representations of messages There are two representations of messages in memory. The first is called string representation and the other one is called broken-down representation. String representation is somehow misnamed, a better term would be byte array representation. The string representation is just a big array of bytes in memory, and those bytes make up the encoded mail. The string representation is what is stored on disk or what is received from an IMAP server, for example. With the broken-down representation, the mail is broken down into smaller structures. For example, instead of having a single byte array for all headers, the broken-down structure has a list of individual headers, and each header in that list is again broken down into a structure. While the string representation is just an array of 7 bit characters that might be encoded, the broken-down representations contain the decoded text strings. As an example, conside the byte array \verbatim "Hugo Maier" \endverbatim Although this is just a bunch of 7 bit characters, a human immediatley recognizes the broken-down structure and sees that the display name is "Hugo Maier" and that the localpart of the email address is "hugo.maier". To illustrate, the broken-down structure could be stored in a structure like this: \verbatim struct Mailbox { QString displayName; QByteArray addressSpec; }; \endverbatim The address spec actually could be broken down further into a localpart and a domain. The process of converting the string representation to a broken-down representation is called \b parsing, and the reverse is called \b assembling. Parsing a message is necessary when wanting to access or modify the broken-down structure. For example, when sending a mail, the address spec of a mailbox needs to be passed to the SMTP server, which means that the recipient headers need to be parsed in order to access that information. Another example is the message list in an mail application, where the broken-down structure of a mail is needed to display information like subject, sender and date in the list. On the other hand, assembling a message is for example done in the composer of a mail application, where the mail information is available in a broken-down form in the composer window, and is then assembled into a final MIME message that is then sent with SMTP. Parsing is often quite tricky, you should always use the methods from KMime instead of writing parsing routines yourself. Even the simple mailbox example above is in pratice difficult to parse, as many things like comments and escaped characters need to be taken into consideration. The same is true for assembling: In the above case, one could be tempted to assemble the mailbox by simply writting code like this: \verbatim QByteArray stringRepresentation = '"' + displayName + "\" <" + addressSpec + ">"; \endverbatim However, just like with parsing, you shouldn't be doing assembling yourself. In the above case, for example, the display name might contain non-ASCII characters, and RFC2047 encoding would need to be applied. So use KMime for assembling in all cases. When parsing a message and assembling it afterwards, the result might not be the same as the original byte array. For example, comments in header fields are ignored during parsing and not stored in the broken-down structure, therefore the assembled message will also not contain comments. Messages in memory are usually stored in a broken-down structure so that it is easy to to access and manipulate the message. On disk and on servers, messages are stored in string representation. \section classes-overview Overview of KMime classes KMime has basically two sets of classes: Classes for headers and classes for MIME parts. A MIME part is represented by \c KMime::Content. A Content can be parsed from a string representation and also be assembled from the broken-down representation again. If parsed, it has a list of sub-contents (in case of multipart contents) and a list of headers. If the Content is not parsed, it stores the headers and the body in a byte array, which can be accessed with head() and body(). There is also a class \c KMime::Message, which basically is a thin wrapper around Content for the top-level MIME part. Message also contains convenience methods to access the message headers. For headers, there is a class hierachy, with \c KMime::Headers::Base as the base class, and \c KMime::Headers::Generics::Structured and \c KMime::Headers::Generics::Unstructured in the next levels. Unstructured is for headers that don't have a defined structure, like Subject, whereas Structured headers have a specific structure, like Date. The header classes have methods to parse headers, like from7BitString(), and to assemble them, like as7BitString(). Once a header is parsed, the classes provide access to the broken-down structures, for example the Date header has a method dateTime(). The parsing in from7BitString() is usually handled by a protected parse() function, which in turn call parsing functions for different types, like parseAddressList() or parseAddrSpec() from the \c KMime::HeaderParsing namespace. When modifing messages, the message is first parsed into a broken-down representation. This broken-down representation can then be accessed and modified with the appropriate functions. After changing the broken-down structure, it needs to be assembled again to get the modified string representation. KMime also comes with some codes for handling base64 and quoted-printable encoding, with \c KMime::Codec as the base class. \section rfcs RFCs \li RFC 5322: Internet Message Format \li RFC 5536: Netnews Article Format \li RFC 2045: Multipurpose Internet Mail Extensions (MIME), Part 1: Format of Internet Message Bodies \li RFC 2046: Multipurpose Internet Mail Extensions (MIME), Part 2: Media Types \li RFC 2047: Multipurpose Internet Mail Extensions (MIME), Part 3: Message Header Extensions for Non-ASCII Text \li RFC 2048: Multipurpose Internet Mail Extensions (MIME), Part 4: Registration Procedures \li RFC 2049: Multipurpose Internet Mail Extensions (MIME), Part 5: Conformance Criteria and Examples \li RFC 2231: MIME Parameter Value and Encoded Word Extensions: Character Sets, Languages, and Continuations \li RFC 2183: Communicating Presentation Information in Internet Message: The Content-Disposition Header Field \li RFC 2557: MIME Encapsulation of Aggregate Documents, such as HTML (MHTML) \li RFC 1847: Security Multiparts for MIME: Multipart/Signed and Multipart/Encrypted \li RFC 3851: S/MIME Version 3 Message Specification \li RFC 3156: MIME Security with OpenPGP \li RFC 2298: An Extensible Message Format for Message Disposition Notifications \li RFC 2646: The Text/Plain Format Parameter (not supported by KMime) \section links Further Reading \li Wikipedia article on MIME\n \li The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) \li A tutorial on character code issues \li Online Base64 encoder and decoder \li Online quoted-printable encoder \li Online quoted-printable decoder \li Online charset converter \li Wikipedia article on public-key cryptography \authors The major authors of this library are: \li Christian Gebauer \li Volker Krause \ \li Marc Mutz \ \li Christian Thurner \ \li Tom Albers \ \li Thomas McGuire \ This document was written by:\n \li Thomas McGuire \ \maintainers \li Volker Krause \ \li Marc Mutz \ \licenses \lgpl */ // DOXYGEN_PROJECTNAME=KMIME Library