Solved

Error using example code to email from my C# code

Posted on 2015-01-22
7
193 Views
Last Modified: 2015-01-23
Hi, I'm using VS2012.
I used this example, https://msdn.microsoft.com/en-us/library/ms268749%28v=vs.110%29.aspx 
just the code for CreateEmailItem()
I'm getting error in VS at
MailItem eMail = (MailItem)(this.Application.CreateItem(OlItemType.olMailItem));

Also, where do I set the "Send From" email?  This will be called from my web application.  Would Office/outlook need to be installed on the web server?  

Thank you.
email.txt
0
Comment
Question by:lapucca
[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
  • 3
  • 3
7 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40565499
I have always used the SMTPClient class to send email as it does not require Outlook installed locally

Here is the MSDN page which includes an example
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40565510
Here is an example I have used before. Note there is no error handling in this code

using System.Net.Mail;

      public void SendEmail(string subject, string body) {
                    
		MailMessage message = new MailMessage();
		message.To.Add("me@home.com");
		message.Subject = subject;
		message.Body = body;

		new SmtpClient().Send(message);
		message.Dispose();
      }

Open in new window


The MailMessage class has quite few option including a From field
https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage(v=vs.110).aspx
0
 

Author Comment

by:lapucca
ID: 40565530
Thank you, yes, that's what I used before.  
Question about if Office need to be installed on web server for this to work or not?
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40565558
Your first example, yes. The one I posted no.

Note in my example I used SmtpClient() as I had set up my Smtp connection settings in the config file eg
https://msdn.microsoft.com/en-us/library/ms164240(v=vs.110).aspx

You will need to know the smtp server name in your environment and set this when creating the SmtpClient object or in your config settings.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40565584
Yes your sample requires Office to be installed which is really not a good thing especially on a server.

Have a look at http://emoreau.com/Entries/Articles/2007/09/Using-SystemNetMail.aspx
0
 

Author Comment

by:lapucca
ID: 40566745
Eric,
The only difference I see is the host parameter is set for the smtpClient before sending the email.  So setting the Host would then not require the web server to have to install office?  Thank you.

            SmtpClient smpt= new SmtpClient();
            smpt.Host = "";

            smpt.Send(message);
0
 

Author Closing Comment

by:lapucca
ID: 40566752
Oh, just saw that Michael answered my question already before Eric.  Thank you both.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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