Solved

Error using example code to email from my C# code

Posted on 2015-01-22
7
177 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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