Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error using example code to email from my C# code

Posted on 2015-01-22
7
Medium Priority
?
218 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:Michael Fowler
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:Michael Fowler
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 23

Accepted Solution

by:
Michael Fowler earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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