我正在使用Microsoft.Office.Interop.Outlook.Application
生成电子邮件并在用户发送之前将其显示在屏幕上。该应用程序是一个winform
应用程序,在C#
.NET Framework 3.5 SP1
Microsoft Outlook 2003
编码,public static void GenerateEmail(string emailTo, string ccTo, string subject, string body)
{
var objOutlook = new Application();
var mailItem = (MailItem)(objOutlook.CreateItem(OlItemType.olMailItem));
mailItem.To = emailTo;
mailItem.CC = ccTo;
mailItem.Subject = subject;
mailItem.HTMLBody = body;
mailItem.Display(mailItem);
}
。我使用以下代码:
body
我的问题是:
如何在生成的电子邮件的{{1}}中插入/添加使用该应用程序的用户的默认签名? 任何帮助表示赞赏。
答案 0 :(得分:45)
有一种非常简单的方法没有被提及。见下面的修改:
public static void GenerateEmail(string emailTo, string ccTo, string subject, string body)
{
var objOutlook = new Application();
var mailItem = (MailItem)(objOutlook.CreateItem(OlItemType.olMailItem));
mailItem.To = emailTo;
mailItem.CC = ccTo;
mailItem.Subject = subject;
mailItem.Display(mailItem);
mailItem.HTMLBody = body + mailItem.HTMLBody;
}
通过在显示邮件项目后编辑HTMLBody,您允许Outlook执行添加默认签名的工作,然后进行复制,编辑和追加。
答案 1 :(得分:18)
请看下面的链接。它解释了在文件系统中可以找到签名的位置以及如何正确读取签名。
http://social.msdn.microsoft.com/Forums/en/vsto/thread/86ce09e2-9526-4b53-b5bb-968c2b8ba6d6
该主题仅提及Window XP和Windows Vista签名位置。我已确认Windows 7上的Outlook签名与Vista位于同一位置。我还确认了Outlook 2003,2007和2010的签名位置相同。
如果您选择这条路线,这是一个代码示例。取自this site.
private string ReadSignature()
{
string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Signatures";
string signature = string.Empty;
DirectoryInfo diInfo = new DirectoryInfo(appDataDir);
if(diInfo.Exists)
{
FileInfo[] fiSignature = diInfo.GetFiles("*.htm");
if (fiSignature.Length > 0)
{
StreamReader sr = new StreamReader(fiSignature[0].FullName, Encoding.Default);
signature = sr.ReadToEnd();
if (!string.IsNullOrEmpty(signature))
{
string fileName = fiSignature[0].Name.Replace(fiSignature[0].Extension, string.Empty);
signature = signature.Replace(fileName + "_files/", appDataDir + "/" + fileName + "_files/");
}
}
}
return signature;
}
编辑:{2013年的See here to find the name of the default signature或@ japel在2010年此主题中的答案。
答案 2 :(得分:14)
我遇到了完全相同的问题,但只能使用Interop解决它,从而获得默认签名。
诀窍是调用GetInspector,它会将HTMLBody属性神奇地设置为签名。只需阅读GetInspector属性即可。我使用Windows 7 / Outlook 2007进行了测试。
为解决方案致信this blog post。
答案 3 :(得分:4)
由于某些原因,根据安装的语言,库会有所不同。 此外,签名可以保存徽标图像,我不知道为什么,但它是由2个不同大小的文件制作的。
private string ReadSignature()
{
string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Signatures";
string signature = string.Empty;
DirectoryInfo diInfo = new DirectoryInfo(appDataDir);
if (diInfo.Exists)
{
FileInfo[] fiSignature = diInfo.GetFiles("*.htm");
if (fiSignature.Length > 0)
{
StreamReader sr = new StreamReader(fiSignature[0].FullName, Encoding.Default);
signature = sr.ReadToEnd();
if (!string.IsNullOrEmpty(signature))
{
string fileName = fiSignature[0].Name.Replace(fiSignature[0].Extension, string.Empty);
signature = signature.Replace(fileName + "_files/", appDataDir + "/" + fileName + "_files/");
}
}
}
else
{
appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Signaturer";
signature = string.Empty;
diInfo = new DirectoryInfo(appDataDir);
if (diInfo.Exists)
{
FileInfo[] fiSignature = diInfo.GetFiles("*.htm");
if (fiSignature.Length > 0)
{
StreamReader sr = new StreamReader(fiSignature[0].FullName, Encoding.Default);
signature = sr.ReadToEnd();
if (!string.IsNullOrEmpty(signature))
{
string fileName = fiSignature[0].Name.Replace(fiSignature[0].Extension, string.Empty);
signature = signature.Replace(fileName + "_files/", appDataDir + "/" + fileName + "_files/");
}
}
}
}
if (signature.Contains("img"))
{
int position = signature.LastIndexOf("img");
int position1 = signature.IndexOf("src", position);
position1 = position1 + 5;
position = signature.IndexOf("\"", position1);
billede1 = appDataDir.ToString() + "\\" + signature.Substring(position1, position - position1);
position = billede1.IndexOf("/");
billede1 = billede1.Remove(position, 1);
billede1 = billede1.Insert(position, "\\");
billede1 = System.Web.HttpUtility.UrlDecode(billede1);
position = signature.LastIndexOf("imagedata");
position1 = signature.IndexOf("src", position);
position1 = position1 + 5;
position = signature.IndexOf("\"", position1);
billede2 = appDataDir.ToString() + "\\" + signature.Substring(position1, position - position1);
position = billede2.IndexOf("/");
billede2 = billede2.Remove(position, 1);
billede2 = billede2.Insert(position, "\\");
billede2 = System.Web.HttpUtility.UrlDecode(billede2);
}
return signature;
}
获取并插入签名: 全局变量:
string billede1 = string.Empty; // holding image1
string billede2 = string.Empty; // holding image2
string signature = ReadSignature();
if (signature.Contains("img"))
{
int position = signature.LastIndexOf("img");
int position1 = signature.IndexOf("src", position);
position1 = position1 + 5;
position = signature.IndexOf("\"", position1);
//CONTENT-ID
const string SchemaPR_ATTACH_CONTENT_ID = @"http://schemas.microsoft.com/mapi/proptag/0x3712001E";
string contentID = Guid.NewGuid().ToString();
//Attach image
mailItem.Attachments.Add(@billede1, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, mailItem.Body.Length, Type.Missing);
mailItem.Attachments[mailItem.Attachments.Count].PropertyAccessor.SetProperty(SchemaPR_ATTACH_CONTENT_ID, contentID);
//Create and add banner
string banner = string.Format(@"cid:{0}", contentID);
signature = signature.Remove(position1, position - position1);
signature = signature.Insert(position1, banner);
position = signature.LastIndexOf("imagedata");
position1 = signature.IndexOf("src", position);
position1 = position1 + 5;
position = signature.IndexOf("\"", position1);
//CONTENT-ID
// const string SchemaPR_ATTACH_CONTENT_ID = @"http://schemas.microsoft.com/mapi/proptag/0x3712001E";
contentID = Guid.NewGuid().ToString();
//Attach image
mailItem.Attachments.Add(@billede2, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, mailItem.Body.Length, Type.Missing);
mailItem.Attachments[mailItem.Attachments.Count].PropertyAccessor.SetProperty(SchemaPR_ATTACH_CONTENT_ID, contentID);
//Create and add banner
banner = string.Format(@"cid:{0}", contentID);
signature = signature.Remove(position1, position - position1);
signature = signature.Insert(position1, banner);
}
mailItem.HTMLBody = mailItem.Body + signature;
字符串处理可以更聪明,但这个工作,并给了我我的sature 上帝运气。
答案 4 :(得分:3)
我通过主要是'偷偷摸摸'来克服这个问题。如果,当您通过 Ctrl + N 在Outlook中创建新电子邮件时,它会插入默认签名,我将该空白电子邮件(带签名)存储在临时字符串中,然后追加该字符串到另一个包含其中内容的字符串。
以下是一些演示它的代码:
string s = "";
Outlook.Application olApp = new Outlook.Application();
Outlook.MailItem mail = olApp.CreateItem(Outlook.OlItemType.olMailItem);
mail.To = "Hi@World.com";
mail.Subject = "Example email";
s = mainContentAsHTMLString + mail.HTMLBody;
mail.Display();
mail.HTMLBody = s;
答案 5 :(得分:3)
我发现了一种非常简单的方法来附加默认的Outlook签名(包括图像)。诀窍是在调用GetInspector之后检索正文消息并将消息连接到该消息。
Imports Microsoft.Office.Interop
Dim fdMail As Outlook.MAPIFolder
Dim oMsg As Outlook._MailItem
oMsg = fdMail.Items.Add(Outlook.OlItemType.olMailItem)
Dim olAccounts As Outlook.Accounts
oMsg.SendUsingAccount = olAccounts.Item(1)
oMsg.Subject = "XXX"
oMsg.To = "xxx@xxx.com"
Dim myInspector As Outlook.Inspector = oMsg.GetInspector
Dim text As String
text = "mail text" & oMsg.HTMLBody
oMsg.HTMLBody = text
oMsg.Send()
答案 6 :(得分:2)
我已经处理了几个小时了。最后,我偶然发现了一个非常有趣的Microsoft支持案例。
真正的问题埋在其他地方: Microsoft Outlook使用Microsoft Word作为编辑器。如果在发送项目时通过Word HTML模块验证了HTML源,则可能会丢失格式。
要解决此问题,只需加载Word.Interopt并使用Word作为编辑器。
由www.DeepL.com/Translator
翻译 public static void SendMail(string subject, string message, List<string> attachments, string recipients)
{
try
{
Outlook.Application application = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
Word.Document worDocument = mailItem.GetInspector.WordEditor as Word.Document;
Word.Range wordRange = worDocument.Range(0, 0);
wordRange.Text = message;
foreach (string attachment in attachments ?? Enumerable.Empty<string>())
{
string displayName = GetFileName(attachment);
int position = (int)mailItem.Body.Length + 1;
int attachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment attachmentItem = mailItem.Attachments.Add
(attachment, attachType, position, displayName);
}
mailItem.Subject = subject;
Outlook.Recipients recipientsItems = (Outlook.Recipients)mailItem.Recipients;
Outlook.Recipient recipientsItem = (Outlook.Recipient)recipientsItems.Add(recipients);
recipientsItem.Resolve();
mailItem.Display();
recipientsItem = null;
recipientsItems = null;
mailItem = null;
application = null;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
private static string GetFileName(string fullpath)
{
string fileName = Path.GetFileNameWithoutExtension(fullpath);
return fileName;
}
玩得开心。托马斯
答案 7 :(得分:0)
要获取/设置用户的默认签名,您可以使用Windows注册表。 Outlook 2010的示例: HKEY_CURRENT_USER \软件\微软\办公室\ 14.0 \ COMMON \ MailSettings 名称:NewSignature 数据类型:字符串 值:(签名文件的名称没有结尾)
答案 8 :(得分:0)
我有一个针对此问题的变体解决方案值得分享,并且完整的目的是从任何Outlook帐户发送电子邮件,并将电子邮件文本与您可以选择的任何签名合并。
假设:
您添加了对HtmlAgilityPack的引用,该引用用于格式化HTML内容。
由于我无法找到(通过注册表除外)获取电子邮件帐户签名,因此将其作为文本值传递,可以在程序中设置为参数,也可以查看Outlook帐户的注册表设置
来自帐户是系统上的有效电子邮件帐户 签名文件由Outlook使用html格式创建。
我相信对此有很多可能的改进。
/// <summary>
/// Sends an email from the specified account merging the signature with the text array
/// </summary>
/// <param name="to">email to address</param>
/// <param name="subject">subect line</param>
/// <param name="body">email details</param>
/// <returns>false if account does not exist or there is another exception</returns>
public static Boolean SendEmailFromAccount(string from, string to, string subject, List<string> text, string SignatureName)
{
// Retrieve the account that has the specific SMTP address.
Outlook.Application application = new Outlook.Application();
Outlook.Account account = GetAccountForEmailAddress(application, from);
// check account
if (account == null)
{
return false;
}
// Create a new MailItem and set the To, Subject, and Body properties.
Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
// Use this account to send the e-mail.
newMail.SendUsingAccount = account;
newMail.To = to;
newMail.Subject = subject;
string Signature = ReadSignature(SignatureName);
newMail.HTMLBody = CreateHTMLBody(Signature, text);
((Outlook._MailItem)newMail).Send();
return true;
}
private static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress)
{
// Loop over the Accounts collection of the current Outlook session.
Outlook.Accounts accounts = application.Session.Accounts;
foreach (Outlook.Account account in accounts)
{
// When the e-mail address matches, return the account.
if (account.SmtpAddress == smtpAddress)
{
return account;
}
}
throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
}
/// <summary>
/// Return an email signature based on the template name i.e. signature.htm
/// </summary>
/// <param name="SignatureName">Name of the file to return without the path</param>
/// <returns>an HTML formatted email signature or a blank string</returns>
public static string ReadSignature(string SignatureName)
{
string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Signatures";
string signature = string.Empty;
DirectoryInfo diInfo = new DirectoryInfo(appDataDir);
if
(diInfo.Exists)
{
FileInfo[] fiSignature = diInfo.GetFiles("*.htm");
foreach (FileInfo fi in fiSignature)
{
if (fi.Name.ToUpper() == SignatureName.ToUpper())
{
StreamReader sr = new StreamReader(fi.FullName, Encoding.Default);
signature = sr.ReadToEnd();
if (!string.IsNullOrEmpty(signature))
{
// this merges the information in the signature files together as one string
// with the correct relative paths
string fileName = fi.Name.Replace(fi.Extension, string.Empty);
signature = signature.Replace(fileName + "_files/", appDataDir + "/" + fileName + "_files/");
}
return signature;
}
}
}
return signature;
}
/// <summary>
/// Merges an email signature with an array of plain text
/// </summary>
/// <param name="signature">string with the HTML email signature</param>
/// <param name="text">array of text items as the content of the email</param>
/// <returns>an HTML email body</returns>
public static string CreateHTMLBody(string signature, List<string> text)
{
try
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
HtmlAgilityPack.HtmlNode node;
HtmlAgilityPack.HtmlNode txtnode;
// if the signature is empty then create a new string with the text
if (signature.Length == 0)
{
node = HtmlAgilityPack.HtmlNode.CreateNode("<html><head></head><body></body></html>");
doc.DocumentNode.AppendChild(node);
// select the <body>
node = doc.DocumentNode.SelectSingleNode("/html/body");
// loop through the text lines and insert them
for (int i = 0; i < text.Count; i++)
{
node.AppendChild(HtmlAgilityPack.HtmlNode.CreateNode("<p>" + text[i] + "</p>"));
}
// return the full document
signature = doc.DocumentNode.OuterHtml;
return signature;
}
// load the signature string as HTML doc
doc.LoadHtml(signature);
// get the root node and insert the text paragraphs before the signature in the document
node = doc.DocumentNode;
node = node.FirstChild;
foreach (HtmlAgilityPack.HtmlNode cn in node.ChildNodes)
{
if (cn.Name == "body")
{
foreach (HtmlAgilityPack.HtmlNode cn2 in cn.ChildNodes)
{
if (cn2.Name == "div")
{
// loop through the text lines backwards as we are inserting them at the top
for (int i = text.Count -1; i >= 0; i--)
{
if (text[i].Length == 0)
{
txtnode = HtmlAgilityPack.HtmlNode.CreateNode("<p class=\"MsoNormal\"><o:p> </o:p></p>");
}
else
{
txtnode = HtmlAgilityPack.HtmlNode.CreateNode("<p class=\"MsoNormal\">" + text[i] + "<o:p></o:p></p>");
}
cn2.InsertBefore(txtnode, cn2.FirstChild);
}
// return the full document
signature = doc.DocumentNode.OuterHtml;
}
}
}
}
return signature;
}
catch (Exception)
{
return "";
}
}
答案 9 :(得分:0)
在使用 Inspectors_NewInspector 事件实现拦截器时,使用 GetInspector 会导致异常。您还会发现,在引发 Inspectors_NewInspector 事件时,尚未将签名添加到MailItem。
如果您使用自己的代码(例如您自己的按钮)触发 Item.Send(),则“签名”和“内容结尾”都将具有Anchor标签<a>
”。请注意,如果您自己处理Outlook功能区[发送]按钮单击事件(对于加载项很常见),则这些标记将从HTMLBody中删除(从Word HTML到HTML的转换)。
我的解决方案是处理Item.Open事件,以便在创建/引发Interceptor / Inspectors_NewInspector时,可以将Id属性添加到包含的<p>
标记中,以便以后在发送时使用。即使发送后,该属性仍保留在HTML中。
这确保了每当调用Send时,我都可以在代码中检测到“签名”或“内容结尾”段落。
答案 10 :(得分:0)
那些年来一直在寻找答案的人。
在Outlook 2016中, mailItem.HTMLBody 已包含您的默认签名/页脚。
就我而言,我回复了某人。 如果您想在之前添加一条消息,请如下所示。很简单。
MailItem itemObj = item as MailItem; //itemObj is the email I am replying to
var itemReply = itemObj.Reply();
itemReply.HTMLBody = "Your message" + itemReply.HTMLBody; //here happens the magic, your footer is already there in HTMLBody by default, just don't you delete it :)
itemReply.Send();