1
votes

Right I'm trying to send an Email form though lotus notes, it has an attachment and the body needs to be in HTML.

I've got some code that from all I've read should allow me to do this however it doesn't. Without the HTML body the attachment will send, when I impliment a HTML body the Email still sends but the attachment dissapears

   try
            {
                Session.Initialize("1234567890");
                Session.ConvertMime = false;
                MailServer = Session.GetEnvironmentString("MailServer", true);
                MailFile = Session.GetEnvironmentString("Mailfile", true);
                MailDb = Session.GetDatabase(MailServer.ToString(), MailFile.ToString(), false);
                MailDoc = MailDb.CreateDocument();
                MailDoc.ReplaceItemValue("Form", "Memo");
                MailDoc.ReplaceItemValue("SendTo", "XXXXXX");
                MailDoc.ReplaceItemValue("subject", "Test test");
                MailDoc.AppendItemValue("Principal", "HIHIHi");
                object obAttachment;
                Mime = MailDoc.CreateMIMEEntity("Body");
                HtmlBody = Session.CreateStream();
                HtmlBody.Open("C:\\Users\\Documents\\310143-001_1125_20181016.pdf", "");
                Mime.SetContentFromBytes(HtmlBody, "Application/pdf", Domino.MIME_ENCODING.ENC_IDENTITY_BINARY);
                HtmlBody.Close();
                HtmlBody.Truncate();
        HtmlBody = Session.CreateStream();
                HtmlBody.WriteText(mailBoby, EOL_TYPE.EOL_CR);
                Mime.SetContentFromText(HtmlBody, "text/html;charset=UTF-8", Domino.MIME_ENCODING.ENC_IDENTITY_BINARY);
                Object obj = MailDoc.GetItemValue("SendTo");
                MailDoc.SaveMessageOnSend = true;
                MailDoc.Send(false, ref obj);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                rt = null;
                Session = null;
                MailDoc = null;
                MailDb = null;

            }
2

2 Answers

1
votes

You'll have to create a multi-part MIME message for both HTML and file attachments.

Session.Initialize("1234567890");
Session.ConvertMime = false;
MailServer = Session.GetEnvironmentString("MailServer", true);
MailFile = Session.GetEnvironmentString("Mailfile", true);
MailDb = Session.GetDatabase(MailServer.ToString(), MailFile.ToString(), false);
MailDoc = MailDb.CreateDocument();
MailDoc.ReplaceItemValue("Form", "Memo");
MailDoc.ReplaceItemValue("SendTo", "XXXXXX");
MailDoc.ReplaceItemValue("subject", "Test test");
MailDoc.AppendItemValue("Principal", "HIHIHi");
object obAttachment;
AttachmentPath = "C:\\Users\\Documents\\";
AttachmentFile = "310143-001_1125_20181016.pdf";
mailBody = "<html><head></head><body>Hello There.</body></html>";

Mime = MailDoc.CreateMIMEEntity("Body");

MimeHeader = Mime.CreateHeader("MIME-Version");
MimeHeader.SetHeaderVal("1.0");

MimeHeader= Mime.CreateHeader("Content-Type");
MimeHeader.SetHeaderValAndParams( "multipart/alternative;boundary=\"=NextPart_=\"");

MimeChild = Mime.CreateChildEntity();
HtmlBody = Session.CreateStream();

HtmlBody.WriteText(mailBody, Stream.EOL_CR);
MimeChild.SetContentFromText(HtmlBody, "text/html;charset=\"iso-8859-1\"", Domino.MIME_ENCODING.ENC_NONE);

MimeChild = Mime.CreateChildEntity();
HtmlBody = Session.CreateStream();
HtmlBody.Open(AttachmentPath + AttachmentFile, "");   

MimeHeader = MimeChild.CreateHeader("Content-Disposition");
MimeHeader.SetHeaderVal("attachment; filename=\""+AttachmentFile+"\"");

MimeChild.SetContentFromBytes(HtmlBody, "application/octet-stream; name=\""+AttachmentFile+"\"", Domino.MIME_ENCODING.ENC_IDENTITY_BINARY); 

MailDoc.CloseMIMEEntities(true);

Session.ConvertMime = true;

Object obj = MailDoc.GetItemValue("SendTo");
MailDoc.SaveMessageOnSend = true;
MailDoc.Send(false, ref obj);

This should work for most any file type, but you can specify application/pdf for the Content-Disposition for PDF files.

0
votes

You're calling the SetContentFromBytes and SetContentFromText methods on your Mime object twice. On the same Mime object. The second call overwrites the first.

You need to create a tree of MIME entities using the CreateChildEntity method, setting the content type for the parent entity as 'multipart' and setting one of the child entities to your text and one of the entities from your attachment.