0 votes

Hi,

I'm using vb.net and trying to read a previously saved .eml file from disk and load into the SMTP routine.
Then I am trying to change the to/from and edit some of the HTML body, and even add attachments.

The code I am using is :
dim filename = "c:\mail\outbox\email1.eml"
Dim builder As New MailBuilder()
builder = New MailBuilder().CreateFromEmlFile(filename)

I seem to get an error:
Exception thrown: 'System.InvalidCastException' in testEmailService.exe

Can you help please?
Basically, once this works, I will adjust builder.To etc etc

many thanks

Andy.

by (650 points)

1 Answer

0 votes

This line is incorrect:

builder = New MailBuilder().CreateFromEmlFile(filename)

CreateFromEmlFile creates IMail instance:

Dim email as IMail 
email = New MailBuilder().CreateFromEmlFile(filename)

If you want to further modify the message you should create new pre-filled builder:

Dim builder2 as MailBuilder
builder2 = email.ToBuilder()
builder2.Text = "different text"
builder2.AddAttachment(....)

// and finally create new, modified email message:

Dim email2 as IMail 
email2 = builder2.Create()

or you can to some extent modify the message itself :

email.To.Add(new MailBox("test@example.com"));
by (301k points)
edited by
thanks for the super quick support :-)

I made the change, but this has caused compilation errors now

in particular, when trying to add the attachments with this line:
Dim att As MimeData = builder.AddAttachment(fileDetails)
[builder.AddAttachment is the problem]
and
Dim email As IMail = builder.Create()
[builder.Create is the problem]
See below for full code:

builder = New MailBuilder().CreateFromEmlFile(filename)
builder.From.Add(New MailBox(from, "CPS Grounds"))
If emailto.Count > 0 Then
    For Each toaddress In emailto
        If toaddress <> "" Then
              builder.[To].Add(New MailBox(toaddress))
        End If
    Next
End If
builder.Subject = subject
For Each attachment In attachmentList
    '
    ' get attachment details
    '
    fn = attachment
    '
    Dim fileDetails = fn
    Dim realFileName = fn.Replace(fname, "")
    Dim att As MimeData = builder.AddAttachment(fileDetails)
    att.FileName = realFileName
    '
Next
'
' create the whole email
'
Dim email As IMail = builder.Create()
'
' send the email and close the smtp routine
'
smtp.SendMessage(email)
Please see modified answer.
Scratch the below, seems if I change this then it works :-)
Dim builder2 as mailbuilder
instead of
Dim builder2 as imail

This doesn't seem to work...
Is it possible to do what I need to do?
Basically, I need to load in a pre-saved .eml file, and use it as the basis for a new email.
The .eml file would be created by a user in outlook and then is  saved by an IMAP routine as a .eml file.

The routine above then uses the .eml file and sends to multiple recipients, added by the routine here.

So, I'm trying to load the .eml into a builder so I can adjust parts of the email.

even when I copy your code as per below, I get the same error.
basically "addattachment is not a member of imail" and "CCreate is not a member of imail"

Dim email as IMail
email = New MailBuilder().CreateFromEmlFile(filename)
Dim builder2 as Imail
builder2 = email.ToBuilder()
builder2.AddAttachment(....)

Dim email2 as IMail
email2 = builder2.Create()
My bad - sorry.

This line:
Dim builder2 as IMail
Should be:
Dim builder2 as MailBuilder

Corrected that in my answer already.
...