Solved

Create email C# with Asp.Net

Posted on 2014-12-16
6
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 75

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 36

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 75

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trigger C# code inside the SQL Server 6 57
Need help for captcha 2 41
What!!!??? 5 60
Textbox autocomplete using jquery in asp.net 13 48
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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