Reputation: 2960
I am writing a project in asp.net C# using Visual Studio 2010. I want to write function, which opens outlook window to send email when user clicks a button.
I tried this:
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 );
But compiler says there is no namespace Office inside namespace Microsoft. Actually Microsoft Office including Outlook fully installed in my computer.
Should I include Office library to Visual Studio? How the problem can be solved?
Upvotes: 1
Views: 17216
Reputation: 1804
Rather you try like this,add using Microsoft.Office.Interop.Outlook; reference
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();
Upvotes: 1
Reputation: 119
this use outlook to send email with recipient, subject, and body preloaded.
<A HREF="mailto:[email protected]?subject=this is the subject&body=Hi, This is the message body">send outlook email</A>
Upvotes: 3
Reputation: 12731
If you use Microsoft.Office.Interop.Outlook
, Outlook must be installed on the server (and runs on the server, not on user computer).
Have you tried using SmtpClient?
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
using (m)
{
//sender is set in web.config: <smtp from="my alias <[email protected]>">
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*/ }
}
Upvotes: 1