2011-02-02 35 views

回答

0

真的没有其他人建议的COM互操作的理由。看看.NET System.Net.Mail命名空间。

相关类是:

有发送XLS attachement我的一个完整的C#示例n Attachment类文档。

// Specify the file to be attached and sent. 
// This example assumes that a file named Data.xls exists in the current working directory. 
string file = "data.xls"; 

// Create a message and set up the recipients. 
MailMessage message = new MailMessage(
    "[email protected]", 
    "[email protected]", 
    "Quarterly data report.", 
    "See the attached spreadsheet."); 

// Create the file attachment for this e-mail message. 
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); 

// Add time stamp information for the file. 
ContentDisposition disposition = data.ContentDisposition; 
disposition.CreationDate = System.IO.File.GetCreationTime(file); 
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); 
disposition.ReadDate = System.IO.File.GetLastAccessTime(file); 

// Add the file attachment to this e-mail message. 
message.Attachments.Add(data); 

//Send the message. 
SmtpClient client = new SmtpClient(server); 

// Add credentials if the SMTP server requires them. 
client.Credentials = CredentialCache.DefaultNetworkCredentials; 
client.Send(message); 
1

如果你真的想用Outlook发送你的电子邮件,你可以简单地使用COM对象,例如,像这样:

dynamic app = Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application")); 
dynamic email = app.CreateItem(0); 
email.Subject = "Subject"; 
email.Body = "Text"; 
email.To = "[email protected]"; 
email.Save(); 
email.Attachments.Add(@"E:\MyFile.txt"); 

email.Display(true); //use this to display the Outlook-window 
email.Send(); //use this to send the email directly 
+0

非常感谢您的回复,它对我有很大的帮助。 – Harikasai 2011-02-04 04:24:20

1

Microsoft.Office.Interop.Outlook.Application olkApp =新Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.MailItem olkMail =(MailItem)olkApp.CreateItem(OlItemType.olMailItem);
olkMail.Subject =“测试邮件”; olkMail.To =“[email protected]”; olkMail.Body =“Hi”; olkMail.Attachments.Add(“D:\ Data \ report.xls”,Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue,1,“Report”); olkMail.Save(); //将邮件保存在未发送的草稿中 //olkMail.Send();//发送邮件 MessageBox.Show(“Mail sent”);

相关问题