Solved

Error using example code to email from my C# code

Posted on 2015-01-22
7
191 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

765 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