Solved

Create email C# with Asp.Net

Posted on 2014-12-16
6
282 Views
Last Modified: 2014-12-26
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

0
Comment
Question by:José Perez
6 Comments
 
LVL 11

Expert Comment

by:LordWabbit
ID: 40504603
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.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40505250
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.
0
 
LVL 2

Author Comment

by:José Perez
ID: 40505512
mmm interesting, do you have an example of how to generate emails with C# or asp.net code?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:José Perez
ID: 40506055
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.
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40510360
Please check this link:
http://www.4guysfromrolla.com/articles/072606-1.aspx
It is a bit dated, but MailMessage and SmtpClient  are the standard way to send emails in .NET. The info is very comprehensive and it provides a sample email form page.
Notice that you can put your SMPT server details in your configuration file as described on the link.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40510456
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.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now