Link to home
Start Free TrialLog in
Avatar of José Perez
José PerezFlag for Chile

asked on

Create email C# with Asp.Net

Hi,
I am using Visual Studio 2013 for Web to develop an Asp.Net page with C#.
Now, I am in the moment to create a web form where the user clicks a button and an outlook message is displayed with certain information he currently have in the page, then the user should click on 'Send'.

First of all, the code displays an error in the 'Outlook.MailItem mailItem = (Outlook.MailItem)' suggesting there is no 'using Microsoft.Office.Interop.Outlook'

The code I am trying to make work is the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Outlook;

namespace WebApplication7
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            CreateMailItem()
        }

        private void CreateMailItem()
        {
            Outlook.MailItem mailItem = (Outlook.MailItem)
                this.Application.CreateItem(Outlook.OlItemType.olMailItem);
            mailItem.Subject = "This is the subject";
            mailItem.To = "someone@example.com";
            mailItem.Body = "This is the message.";
            mailItem.Importance = Outlook.OlImportance.olImportanceLow;
            mailItem.Display(false);
        }


    }
}

Open in new window

Avatar of LordWabbit
LordWabbit

This is server side code, it would execute on the server hosting the website and not the current users PC.  Since outlook 2000 service pack 2 creating an email is restricted, you might want to look into outlook redemption as a workaround.
Avatar of kaufmed
You could use a mailto link in your page; however, keep in mind that you are very limited in what you can populate via the mailto link. A mailto link will typically result in the user's default email application being opened. If you go this route, then because a mailto is itself a link, you will have to URL-encode the data to ensure special characters--like newlines, spaces, tabs, etc.--are handled correctly.
Avatar of José Perez

ASKER

mmm interesting, do you have an example of how to generate emails with C# or asp.net code?
I found sample code for mailto but the bad part is that it depends on the mail client the user have. Also, it is not very recommended, so none of the solutions provided work for us.

I was recommended to use an asp-c# contact form... does anyone of you have a sample page to do it? (VS2012-VS2013)

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In a web environment, MailMessage and SmtpClient will not let you show the mail on the user's screen, nor will it provide them with a send button, nor will it go through the user's Outlook. You could code custom pages/views that accomplish the first two, but not the last one. AFAIK, the mailto link is the only way to get all 3, but it is limited. MailMessage and SmtpClient run on the server, not the client.