Exemple #1
0
void MultipartMessage::SetBodyPart(int aIdx, const MultipartBodyPart& aPart)
{
    DwBody& body = mMessage->Body();
    int numParts = NumberOfParts();
    DwBodyPart* part = 0;
    // If indexed part exists already, just replace its values
    if (0 <= aIdx && aIdx < numParts) {
        part = body.FirstBodyPart();
        for (int curIdx=0; curIdx < aIdx; ++curIdx) {
            part = part->Next();
        }
    }
    // Otherwise, add as many new parts as necessary.
    else if (numParts <= aIdx) {
        while (numParts <= aIdx) {
            part = DwBodyPart::NewBodyPart(mEmptyString, 0);
            body.AddBodyPart(part);
            ++numParts;
        }
    }
    else /* if (aIdx < 0) */ {
        // error!
        return;
    }

    const DwString& type     = aPart.TypeStr();
    const DwString& subtype  = aPart.SubtypeStr();
    const DwString& cte      = aPart.CteStr();
    const DwString& contDesc = aPart.ContentDescription();
    const DwString& contDisp = aPart.ContentDisposition();
    const DwString& bodyStr  = aPart.Body();

    DwHeaders& headers = part->Headers();
    if (type != "" && subtype != "") {
        headers.ContentType().SetTypeStr(type);
        headers.ContentType().SetSubtypeStr(subtype);
    }
    if (cte != "") {
        headers.Cte().FromString(cte);
    }
    if (contDesc != "") {
        headers.ContentDescription().FromString(contDesc);
    }
    if (contDisp != "") {
        headers.ContentDisposition().FromString(contDisp);
    }
    part->Body().FromString(bodyStr);
}
void MessageWithAttachments::AttachBinaryFile(const char* aFilename,
    int aType, int aSubtype)
{
    // Get the file contents

    DwString str;
    PutFileInString(aFilename, str);
    
    // Encode using base64 encoding

    DwString encStr;
    DwEncodeBase64(str, encStr);
    
    // Create a body part and set the necessary fields

    MultipartBodyPart part;
    part.SetType(aType);
    part.SetSubtype(aSubtype);
    part.SetCte(DwMime::kCteBase64);

    // Set content-disposition to attachment, with filename parameter
    // (see RFC-1806 for information on this *experimental* header field)
    
    DwString contDisp = "attachment; filename=";
    contDisp += '\"';
    contDisp += aFilename;
    contDisp += '\"';
    part.SetContentDisposition(contDisp);

    // Set the encoded file contents as the body of the body part

    part.SetBody(encStr);

    // Make sure this is not the first part, since that is reserved for
    // the text
    
    if (NumberOfParts() == 0) {
        SetBodyPart(1, part);
    }
    else {
        AddBodyPart(part);
    }
}
int MessageWithAttachments::NumberOfAttachments() const
{
    int n = NumberOfParts() - 1;
    return (n >= 0) ? n : 0;
}