使用C#在Outlook消息中嵌入图像的代码 [英] Code to embed an image in outlook message using C#

查看:569
本文介绍了使用C#在Outlook消息中嵌入图像的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我试图在独立应用程序中发送Outlook电子邮件。截至目前,它在发送文本方面工作得很好。我想在这个..pls帮助中插入一个图像

Hi guys,

Am trying to send an outlook email in a standalone application. As of now it is working fine in sending the text. I want to insert an image into this..pls help

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using Outlook = Microsoft.Office.Interop.Outlook;

public void sendEMailThroughOUTLOOK()
{
    try
    {
        // Create the Outlook application.
        Outlook.Application oApp = new Outlook.Application();
        // Create a new mail item.
        Outlook.MailItem oMsg =       (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
        // Set HTMLBody. 
        
        //add the body of the email
        oMsg.HTMLBody = "Hello, This is test mail!!";
        //Add an attachment.
        String sDisplayName = "MyAttachment";
        int iPosition = (int)oMsg.Body.Length + 1;
        int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
        //now attached the file
        Outlook.Attachment oAttach = oMsg.Attachments.Add(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg",iAttachType,null, sDisplayName);
        //Subject line
        oMsg.Subject = "Your Subject will go here.";
       
        // Add a recipient.
        Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
        // Change the recipient in the next line if necessary.
        Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("sam@abconsultants.com");
        oRecip.Resolve();
        // Send.
        oMsg.Send();
        
        // Clean up.
        oRecip = null;
        oRecips = null;
        oMsg = null;
        oApp = null;
    }
    //end of try block
    catch (Exception ex)
    {
    }
    //end of catch
}
//end of Email Method





提前感谢



thanks in advance

推荐答案

尝试这个



Try this

string imageContentid = "someimage.jpg";

   attachment.PropertyAccessor.SetProperty(
     "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    , imageContentid 
    );

   oMsg.HTMLBody = String.Format(
     "<body><img src=\"cid:{0}\"></body>"
    , imageContentid 
    );


This is a vb code snippet by Dmitry Streblechenko (MVP).It worked fine for me.
Set objOutlook = CreateObject("Outlook.Application")
      Set Ns = objOutlook.GetNamespace("MAPI")
      Ns.Logon
      Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
      Set objOutlookRecip = objOutlookMsg.Recipients.Add("test@dimastr.com")
      objOutlookRecip.Type = olTo
      objOutlookMsg.Subject = "test"
      ' add graphic as attachment to Outlook message
      Set colAttach = objOutlookMsg.Attachments
      Set l_Attach = colAttach.Add("z:\Temp\8\1.jpg ")
      l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpeg"  'Change From 0x370eE001E
      l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x3712001F", "myident" 'Changed from 0x3712001E
      objOutlookMsg.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", True
       'Set body format to HTML
       objOutlookMsg.BodyFormat = olFormatHTML
       msgHTMLBody = "<html>" & _
                  "<head>" & _
                  "</head>" & _
                  "<body>" & _
                  "        <img align="baseline" border="1" hspace="0" src="cid:myident" width="" 600="" hold=" />                  "></img></body></html>"
         objOutlookMsg.HTMLBody = msgHTMLBody
      objOutlookMsg.Save
      objOutlookMsg.Send

Dmitry Streblechenko (MVP)
http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been


这篇关于使用C#在Outlook消息中嵌入图像的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆