Solved

Error using example code to email from my C# code

Posted on 2015-01-22
7
173 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
  • 3
  • 3
7 Comments
 
LVL 23

Expert Comment

by:Michael74
Comment Utility
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
Comment Utility
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
Comment Utility
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
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 23

Accepted Solution

by:
Michael74 earned 500 total points
Comment Utility
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 69

Expert Comment

by:Éric Moreau
Comment Utility
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
Comment Utility
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
Comment Utility
Oh, just saw that Michael answered my question already before Eric.  Thank you both.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 video discusses moving either the default database or any database to a new volume.
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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now