发送内联 MHTML

发布于 2024-07-21 02:04:49 字数 229 浏览 16 评论 0原文

我想知道是否可以通过.NET 2.0 MailMessage 对象 用于发送即时创建的内联 MHTML 文件。

内联我的意思是:它应该以用户打开电子邮件后可以看到的方式发送,而无需打开/下载附件。

I was wondering if it is possible through the .NET 2.0 MailMessage object to send an inline MHTML file that is created on the fly.

By inline I mean: It should be sent in a way that the user can see it, once he opens the email, without having to open/download the attachment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海不蓝 2024-07-28 02:04:49

这有点棘手,但是是的,你可以做到。 事实上,MailMessage 类只不过是系统 CDO.Message 类之上的一个包装器,它可以做到这一点。
您也可以使用 AlternateView 功能,它更简单:

MailMessage mailMessage = new MailMessage("[email protected]"
    ,"[email protected]"
    ,"test"
    ,"");
string ContentId = "wecandoit.jpg";
mailMessage.Body = "<img src=\"cid:" + ContentId + "\"/>";
AlternateView av = AlternateView.CreateAlternateViewFromString(mailMessage.Body
    ,null
    ,MediaTypeNames.Text.Html);
LinkedResource lr = new LinkedResource(@"d:\Personal\My Pictures\wecandoit.jpg");
lr.ContentId = ContentId;
lr.ContentType.Name = ContentId;
lr.ContentType.MediaType = "image/jpeg";
av.LinkedResources.Add(lr);
mailMessage.AlternateViews.Add(av);
SmtpClient cl = new SmtpClient();
cl.PickupDirectoryLocation = @"c:\test";
cl.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
cl.Send(mailMessage);

It's a bit tricky, but yes, you can do it. In fact MailMessage class is nothing more than a wrapper above the system's CDO.Message class which can do the trick.
Also you can use AlternateView functionality, it's more simple:

MailMessage mailMessage = new MailMessage("[email protected]"
    ,"[email protected]"
    ,"test"
    ,"");
string ContentId = "wecandoit.jpg";
mailMessage.Body = "<img src=\"cid:" + ContentId + "\"/>";
AlternateView av = AlternateView.CreateAlternateViewFromString(mailMessage.Body
    ,null
    ,MediaTypeNames.Text.Html);
LinkedResource lr = new LinkedResource(@"d:\Personal\My Pictures\wecandoit.jpg");
lr.ContentId = ContentId;
lr.ContentType.Name = ContentId;
lr.ContentType.MediaType = "image/jpeg";
av.LinkedResources.Add(lr);
mailMessage.AlternateViews.Add(av);
SmtpClient cl = new SmtpClient();
cl.PickupDirectoryLocation = @"c:\test";
cl.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
cl.Send(mailMessage);
攒眉千度 2024-07-28 02:04:49

(jdecuyper——感谢您的插件,因为我写了 aspNetEmail)。

您可以使用 aspNetEmail 来完成此操作。 您可以使用 MHT 替换电子邮件的全部内容。

您无法使用 System.Net.Mail 执行此操作,但如果您想走商业路线,请向我发送一封电子邮件,地址为 [电子邮件受保护],我将向您展示如何完成此操作。

如果您想走开源路线,您可以修改 codeproject 上的一些 SMTP 代码来实现此目的。 基本上,您可以将内容注入 SMTP 进程的 DATA 命令中。

需要注意的一件事是:如果您的 MHT 文档嵌入了脚本、flash、activeX 对象或任何可能被邮件客户端阻止的内容,则它的呈现方式可能与您在浏览器中看到的内容不同。

(jdecuyper -- thanks for the plug, as I wrote aspNetEmail).

You can do this with aspNetEmail. You can replace the entire contents of the email message with your MHT.

You can't do this with System.Net.Mail, but if you want to go the commerical route, pop me an email at [email protected] and I'll show you how this can be done.

If you want to go an open source route, there is probably some SMTP code on codeproject that you could modify to do this. Basically, you would inject your contents into the DATA command of the SMTP process.

One thing to note: If your MHT document has embedded scripts, flash, activeX objects or anything that may be blocked by the mail client, it probably won't render the same as what you are seeing in the browser.

_畞蕅 2024-07-28 02:04:49

您是否想在 html 电子邮件中添加一些图像?

为此,您需要将图像嵌入到电子邮件中。 我找到了一个教程来用几行代码完成它。 您还可以购买 aspnetemail 程序集。 发送带有嵌入图像的电子邮件总是对我有很大帮助,如果出现任何问题,他们也有一个优秀的支持团队。

请记住,嵌入图像会使您的电子邮件变得更重,但更好:)

Are you trying to add some images to an html email?

To accomplish this you will need to embed the images inside your email. I found a tutorial to accomplish it in a few lines of code. You can also buy the aspnetemail assembly. It has always helped me a lot to send emails with embedded images, they also have an excellent support team if anything goes wrong.

Keep in mind that embedding images makes your email heavier, but nicer :)

浮生未歇 2024-07-28 02:04:49

可以通过 CDO.Message (需要添加到项目引用 COM 库“Microsoft CDO for Windows 2000 Library”):

protected bool SendEmail(string emailFrom, string emailTo, string subject, string MHTmessage)
{
    string smtpAddress = "smtp.email.com";

    try
    {
      CDO.Message oMessage = new CDO.Message();

      // set message
      ADODB.Stream oStream = new ADODB.Stream();
      oStream.Charset = "ascii";
      oStream.Open();
      oStream.WriteText(MHTmessage);
      oMessage.DataSource.OpenObject(oStream, "_Stream");

      // set configuration
      ADODB.Fields oFields = oMessage.Configuration.Fields;
      oFields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = CDO.CdoSendUsing.cdoSendUsingPort;
      oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = smtpAddress;
      oFields.Update();

      // set other values
      oMessage.MimeFormatted = true;
      oMessage.Subject = subject;
      oMessage.Sender = emailFrom;
      oMessage.To = emailTo;
      oMessage.Send();
    }
    catch (Exception ex)
    {
      // something wrong
    }
}

It is possible via CDO.Message (it is necessary add to project references COM library "Microsoft CDO for Windows 2000 Library"):

protected bool SendEmail(string emailFrom, string emailTo, string subject, string MHTmessage)
{
    string smtpAddress = "smtp.email.com";

    try
    {
      CDO.Message oMessage = new CDO.Message();

      // set message
      ADODB.Stream oStream = new ADODB.Stream();
      oStream.Charset = "ascii";
      oStream.Open();
      oStream.WriteText(MHTmessage);
      oMessage.DataSource.OpenObject(oStream, "_Stream");

      // set configuration
      ADODB.Fields oFields = oMessage.Configuration.Fields;
      oFields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = CDO.CdoSendUsing.cdoSendUsingPort;
      oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = smtpAddress;
      oFields.Update();

      // set other values
      oMessage.MimeFormatted = true;
      oMessage.Subject = subject;
      oMessage.Sender = emailFrom;
      oMessage.To = emailTo;
      oMessage.Send();
    }
    catch (Exception ex)
    {
      // something wrong
    }
}
命硬 2024-07-28 02:04:49

可以通过 CDO.Message (需要添加到项目引用 COM 库“Microsoft CDO for Windows 2000 Library”):

protected bool SendEmail(string emailFrom, string emailTo, string subject, string MHTmessage)
{
    string smtpAddress = "smtp.email.com";

    try
    {
      CDO.Message oMessage = new CDO.Message();

      // set message
      ADODB.Stream oStream = new ADODB.Stream();
      oStream.Charset = "ascii";
      oStream.Open();
      oStream.WriteText(MHTmessage);
      oMessage.DataSource.OpenObject(oStream, "_Stream");

      // set configuration
      ADODB.Fields oFields = oMessage.Configuration.Fields;
      oFields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = CDO.CdoSendUsing.cdoSendUsingPort;
      oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = smtpAddress;
      oFields.Update();

      // set other values
      oMessage.MimeFormatted = true;
      oMessage.Subject = subject;
      oMessage.Sender = emailFrom;
      oMessage.To = emailTo;
      oMessage.Send();
    }
    catch (Exception ex)
    {
      // something wrong
    }
}

It is possible via CDO.Message (it is necessary add to project references COM library "Microsoft CDO for Windows 2000 Library"):

protected bool SendEmail(string emailFrom, string emailTo, string subject, string MHTmessage)
{
    string smtpAddress = "smtp.email.com";

    try
    {
      CDO.Message oMessage = new CDO.Message();

      // set message
      ADODB.Stream oStream = new ADODB.Stream();
      oStream.Charset = "ascii";
      oStream.Open();
      oStream.WriteText(MHTmessage);
      oMessage.DataSource.OpenObject(oStream, "_Stream");

      // set configuration
      ADODB.Fields oFields = oMessage.Configuration.Fields;
      oFields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = CDO.CdoSendUsing.cdoSendUsingPort;
      oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = smtpAddress;
      oFields.Update();

      // set other values
      oMessage.MimeFormatted = true;
      oMessage.Subject = subject;
      oMessage.Sender = emailFrom;
      oMessage.To = emailTo;
      oMessage.Send();
    }
    catch (Exception ex)
    {
      // something wrong
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文