Mail merge images not being displayed

2 posts, 0 answers
  1. fred williams
    fred williams avatar
    55 posts
    Member since:
    Feb 2010

    Posted 27 Jun 2012 Link to this post

    Hello,
    I've created a mail merge application as per he example on your website.

    The Word document is loaded in, and the docprovider is set. I then insert images fine using insert>picture and they show up in the document. Everything appears fine form within the program. 


    When I send the emails however, the pictures don't show up. From within Outlook I get an error that reads:

     "the linked image cannot be displayed. The file may have been moved, renamed, or deleted. Verify that the link points to the correct file and location."

     I've attached a screenshot of exactly how I'm inserting the image into the document. Below is the code I use when I send the emails. Also noteworthy, I had to change the formatprovider from docx to rtf before sending the emails. If I use the docxformatprovider, the text in the email shows up with a bunch of symbols, improperly formatted. 

    Here's DocOpened event I use when opening and loading the word doc:
    try
    {
        OpenFileDialog fDialog = (OpenFileDialog)sender;
         
        switch (System.IO.Path.GetExtension(fDialog.FileName))
        {
            case ".docx":
                docProvider = new DocxFormatProvider();
                break;
            case ".rtf":
                docProvider = new RtfFormatProvider();
                break;
            case ".txt":
                docProvider = new TxtFormatProvider();
                break;
            case ".html":
                docProvider = new HtmlFormatProvider();
                break;
            default:
                break;
        }
         
        using (FileStream fStream = new FileStream(fDialog.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        {
            rDoc = docProvider.Import(fStream);
        }
       
        rDoc.LayoutMode = DocumentLayoutMode.Paged;
        emailForm1.Document = rDoc;
    }

     ...and then when sending the emails I use this. 
    protected void SubmitEmail(object sender, RoutedEventArgs e)
    {
        MailMessage message = new MailMessage();
        byte[] emailBytes = null;
        string bodyText = string.Empty;
         
        SmtpClient mailServer = new SmtpClient("mail.pentegra.com");
        mailServer.UseDefaultCredentials = true;
         
        this.emailForm1.Document.MailMergeDataSource.ItemsSource = mergeFields;
        //here's where I change the formatprovider from docx to rtf. if I use docx, the email text comes out as a bunch of   symbols
     
        docProvider = new RtfFormatProvider();  
     
        //this is just a datatable with a list of emails that gets iterated through to send the emails. 
        for (int i = 0; i < _dtMerge.Rows.Count; i++)
        {
            message = new MailMessage();
            foreach (string s in lstAttachments)
            {
                message.Attachments.Add(new Attachment(s));
            }
            message.From = _fromAddress;
            emailForm1.Document.MailMergeDataSource.MoveToIndex(i);
            emailBytes = docProvider.Export(emailForm1.Document.MailMergeCurrentRecord());
     
            //I convert the byte array into the email body test here.
            StringBuilder sb = new StringBuilder();
            using (StreamReader sReader = new StreamReader(mStream))
             {
                        while (!(sReader.EndOfStream))
                        {
                            sb.Append(sReader.ReadLine() + Environment.NewLine);
                        }
              }
             bodyText = sb.ToString();
            message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(bodyText, null, "text/html"));
             
            message.To.Clear();
            message.To.Add(new MailAddress("x@xx.com"));
     
           mailServer.Send(message);
     
             
    }



    Everything shows up fine in the emails except the image. 
  2. Martin Ivanov
    Admin
    Martin Ivanov avatar
    104 posts

    Posted 02 Jul 2012 Link to this post

    Hello Fred,

    Your code is mostly connected to the way you send the document. Furthermore, there are some inconsistencies and missing lines, such as the fact that you are using RtfFormatProvide to export the document and specifying "text/html" as the MIME type. When it comes to obtaining a string with the RTF content of the document, you can just use the overloaded method of RtfFormatProvider which returns a string, instead of exporting it to a byte array, writing the byte array to a stream and getting the string through a StringBuilder.

    In case you would like to insert the picture as a merge field, i.e. to have different pictures in the mail merged document depending on the current record, you should use a field as shown in the snippet below:

    MergeField mf = new MergeField();
     mf.PropertyPath = "RecipientPhoto";
     IncludePictureField picField = new IncludePictureField();
     picField.SetPropertyValue(IncludePictureField.ImageUriProperty, mf);
     this.editor.InsertField(picField);

    I am also sending you a solution so you can check how the mail merge data source is organized. Note that the nested fields in mail merge are introduced in the last version of the controls. 


    Regards,
    Martin
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top