+1 vote

Hi ,
i'm trying to save a mail on disk. Import the file later and, if nedeed, change the subject or an address (or add). But Import looks not to be working

I've these 2 functions to saveToFile and readFromFile

I created a messsage , saved this as file, read it from file added a CC addresss and saved it with a new name... they are not te same!
What am i doing wrong?

thanks.

private IMail _email = null;
private MailBuilder _message = null;

public Boolean SaveMailMessage(string tcFullFileName)
{
    try
    {
        tcFullFileName = Path.ChangeExtension(tcFullFileName, "eml");
        if (File.Exists(tcFullFileName))
        {
            File.Delete(tcFullFileName);
        }
        if (_email == null) {
            _email = _message.Create();
        }
        byte[] byteArr = _email.Render(AddressHeaderRenderMode.Full);
        File.WriteAllBytes(tcFullFileName, byteArr);                
        return File.Exists(tcFullFileName);
    }
    catch (Exception ex)
    {
        AddErrorInfo(ex.Message, "SaveMailMessage");
        return false;
    }           
}

public Boolean LoadFromFile(string tcFullFileName)
{
    tcFullFileName = Path.ChangeExtension(tcFullFileName, "eml");
    _message = new MailBuilder();
    _message.CreateFromEmlFile(tcFullFileName);
    return true;
}
by

1 Answer

+1 vote

Your code looks incorrect: MailBuilder.CreateFromEmlFile returns an IMail instance. It doesn't fill any of the MailBuilder properties.

Your code should look more or less like this:

private IMail _email = null;
private MailBuilder _builder = null;

public void SaveMailMessage(string fileName)
{
    if (File.Exists(fileName))
        File.Delete(fileName);

    // I assume _builder is instantiated and filled with data elsewhere
    if (_email == null) 
        _email = _builder.Create();

    byte[] eml = _email.Render(AddressHeaderRenderMode.Full);
    File.WriteAllBytes(fileName, eml);              
    // -or-
    //_email.Save(fileName)
}

public void LoadFromFile(string fileName)
{
    _email = new MailBuilder().CreateFromEmlFile(fileName);
}
by (300k points)
Thanks, reloading works now.

We hoped to use the the MailBuilder class for import existing eml data as well, so we could change/add/delete some properties before the real sendings would take place.

... For your knowledge :
in our situation a user generates a XML as script for an email, the system will autom. create an email based on the script and save this emldat into our database. When the email needs to be send is will be read from the database (the eml data) and then the system will check if the mail needs to be redirect to an other emailaddress. Or when a specific subject is given change the subject of change the reply-address or even add an extra attachment.

I will first focus on the send normal and build a rebuilder when/where needed.
i found this ...

_message = _importedEmail.ToBuilder();


:)
Exactly!  :)
...