我正在使用Visual Studio 2010在asp.net C#中编写一个项目。 我想编写功能,打开Outlook窗口,在用户点击按钮时发送电子邮件。
我试过了:
using Outlook = Microsoft.Office.Interop.Outlook;
Outlook.Application oApp = new Outlook.Application ();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem ( Outlook.OlItemType.olMailItem );
oMailItem.To = address;
// body, bcc etc...
oMailItem.Display ( true );
但编译器说在Microsoft名称空间内没有名称空间Office。 实际上Microsoft Office包括Outlook已完全安装在我的计算机中。
我应该将Office库包含到Visual Studio中吗? 如何解决问题?
答案 0 :(得分:1)
如果您使用Microsoft.Office.Interop.Outlook
,则必须在服务器上安装Outlook(并在服务器上运行,而不是在用户计算机上运行)。
您是否尝试过使用SmtpClient?
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
using (m)
{
//sender is set in web.config: <smtp from="my alias <mymail@mysite.com>">
m.To.Add(to);
if (!string.IsNullOrEmpty(cc))
m.CC.Add(cc);
m.Subject = subject;
m.Body = body;
m.IsBodyHtml = isBodyHtml;
if (!string.IsNullOrEmpty(attachmentName))
m.Attachments.Add(new System.Net.Mail.Attachment(attachmentFile, attachmentName));
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
try
{ client.Send(m); }
catch (System.Net.Mail.SmtpException) {/*errors can happen*/ }
}
答案 1 :(得分:1)
这使用outlook来发送带有收件人,主题和正文预装的电子邮件。
<A HREF="mailto:recipient@domain.com?subject=this is the subject&body=Hi, This is the message body">send outlook email</A>
答案 2 :(得分:1)
相反,您尝试这样,使用Microsoft.Office.Interop.Outlook添加;参考
Application app = new Application();
NameSpace ns = app.GetNamespace("mapi");
ns.Logon("Email-Id", "Password", false, true);
MailItem message = (MailItem)app.CreateItem(OlItemType.olMailItem);
message.To = "To-Email_ID";
message.Subject = "A simple test message";
message.Body = "This is a test. It should work";
message.Attachments.Add(@"File_Path", Type.Missing, Type.Missing, Type.Missing);
message.Send();
ns.Logoff();