I want to send a Mail
via Outlook
and C#
but I have a problem with the placement of my Attachments
. I have the following code:
if (strBody.StartsWith(@"{\rtf"))
{
mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatRichText;
mailItem.RTFBody = Encoding.UTF8.GetBytes(strBody);
mailItem.Attachments.Add(strAttachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, int.MaxValue, null);
}
else
{
mailItem.Body = strBody;
mailItem.Attachments.Add(strAttachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, 1, null);
}
My strBody has the following Value:
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Arial;}} {\colortbl ;\red255\green0\blue128;\red0\green128\blue255;} \viewkind4\uc1\pard\fs20 Sehr geehrte \cf1 Damen\cf0 und \cf2 Herren\cf0 ,\par \par hier ihre AB\fs20\par }
But my Mail
looks like this:
Now my Question is,
Attachments
be displayed as an extra Row like when the Mail is not RTF formatted?Attachments
to be displayed at the End?Well you did everything right. Every value > 1 will place the attachment at the end of your mail. After "hier ihre AB" it is placed. Looks stupid but well... As a little workaround, I used it like that too, place some new lines. As much as it takes to place the Attachment under your last sentence.
Or you write the Mail as a HTML Type. Less problems.
EDIT:
As you can see, the file is placed at the end of the mail.
EDIT II:
Here is a example for a method to send your E-Mail as HTML with the attachment in the attachment row:
static void Main(string[] args)
{
Outlook.Application tmpOutlookApp = new Outlook.Application();
Outlook.MailItem tmpMessage = (Outlook.MailItem)tmpOutlookApp.CreateItem(Outlook.OlItemType.olMailItem);
tmpMessage.HTMLBody = "Test";
String sDisplayName = "Test";
int iPosition = (int)tmpMessage.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment oAttach = tmpMessage.Attachments.Add(@"C:\Test.txt", iAttachType, iPosition, sDisplayName);
tmpMessage.Subject = "Your Subject will go here.";
Outlook.Recipients oRecips = (Outlook.Recipients)tmpMessage.Recipients;
Outlook.Recipient tmpRecipient = (Outlook.Recipient)oRecips.Add("EMail");
tmpRecipient.Resolve();
tmpMessage.Send();
}