在 Microsoft Word 2007 中,可以将文档作为电子邮件正文发送;文字、图片和格式。有没有办法在 C# 中使用 Word 的“向收件人发送邮件”选项?如果可能的话,我将如何去做呢?提前致谢!
问问题
4221 次
1 回答
0
您需要编写一个 Microsoft Word 插件来执行此操作。有关此检查 Microsoft 站点的基本信息:使用 Visual Studio 的 Word 解决方案
您可以执行以下操作来解决您的问题。该代码将在您保存文档时发送邮件。使用一个单词 addin,您可以进行很多活动并做自己的事情。或者,如果您想通过单击按钮发送邮件,您可以添加自己的按钮。
void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.DocumentBeforeSave += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(DocumentSave);
}
void DocumentSave(Word.Document doc, ref bool test, ref bool test2)
{
if(doc is Word.Document)
{
SendMailWithAttachment(doc);
}
}
public void SendMailWithAttachment(Word.Document doc)
{
SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential = new NetworkCredential(MailConst.Username, MailConst.Password);
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(MailConst.Username);
// setup up the host, increase the timeout to 5 minutes
smtpClient.Host = MailConst.SmtpServer;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
smtpClient.Timeout = (60 * 5 * 1000);
message.From = fromAddress;
message.Subject = subject;
message.IsBodyHtml = false;
message.Body = body;
message.To.Add(recipient);
var attachmentFilename = doc.FullName
if (attachmentFilename != null)
{
Attachment attachment = new Attachment(attachmentFilename, MediaTypeNames.Application.Octet);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(attachmentFilename);
disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
disposition.FileName = Path.GetFileName(attachmentFilename);
disposition.Size = new FileInfo(attachmentFilename).Length;
disposition.DispositionType = DispositionTypeNames.Attachment;
message.Attachments.Add(attachment);
}
smtpClient.Send(message);
}
于 2013-06-21T18:24:50.260 回答