1

我正在尝试发送包含许多图像的邮件。当将html文本写入源文件(.cs)内的字符串时,一切正常,但是当我尝试读取我已经写入某个外部文件的相同文本时,邮件希望在正文中显示图像,它只是放置边框(而不是图像) 和附上的真实图像。

为了从richtextbox转换为html,我使用带有命令Rtf2Html.exe file.rcf的RTF-Converter

如何从将显示在邮件正文中的外部文件文本中读取,当我在源代码中“手动”编写时,该文本将显示为?

这是我的代码:

    using System;
using System.Collections.Generic;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Threading;
using AE.Net.Mail;
using System.Web;
using System.Net.Mime;
using System.Diagnostics;
using System.Text.RegularExpressions;


public void SendEmail()
{

 System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage
            SmtpClient sc = new SmtpClient();
            try
            {
                m.From = new MailAddress("your.email@gmail.com", "Display your name");
                m.To.Add(new MailAddress("some.email@gmail.com", "Display"));
                m.Subject = "Test subject";
                m.IsBodyHtml = true;
                // m.Body = "Dont need this!";

                FileStream fs = new FileStream("root.jpg",
                                   FileMode.Open, FileAccess.Read);
                System.Net.Mail.Attachment a = new System.Net.Mail.Attachment(fs, "C:\\root.jpg",MediaTypeNames.Application.Octet);

                 m.Attachments.Add(a);
                /*
                 **This working when i put two images inside of code section in string "str" it send
                mail with images inside of body**
                */
                string str = "<html><body><h1><p><a href=\"http://www.google.hr\" target=\"_blank\">www.google.hr</a></p><h1>Picture</h1><img src=\"cid:image1\"><h1>First text</h1><img src=\"cid:image2\"><br/><h1>Second text</h1></body></html>";

                /*
                **If i read from file to string and send mail it will show images as attach ("source.html")
                have same text as in above string "str"**
                */

               //   string str = File.ReadAllText("source.html");     

                AlternateView av =
                             AlternateView.CreateAlternateViewFromString(str,
                             null, MediaTypeNames.Text.Html);
                LinkedResource lr = new LinkedResource("file1.jpg",
                             MediaTypeNames.Image.Jpeg);
                LinkedResource lr2 = new LinkedResource("file2.jpg",
                             MediaTypeNames.Image.Jpeg);

                lr.ContentId = "image1";
                lr2.ContentId = "image2";

                av.LinkedResources.Add(lr);
                av.LinkedResources.Add(lr2);
                m.AlternateViews.Add(av);

                sc.Host = "smtp.gmail.com";
                sc.Port = 587;
                sc.Credentials = new System.Net.NetworkCredential("your.email@gmail.com", "yourpassword1");
                sc.EnableSsl = true;
                sc.Send(m);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
4

0 回答 0