How to send mail from a Windows Application??

I am attempting to write an application which will send a mail message with an attachment.

Here's code:

using System;
using System.Drawing;
using System.Data;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Web;
using System.Web.Services;
using System.Web.Mail;

string filename = "c:\\stuff.xls"
MailMessage send =
new MailMessage();
send.To = "validmail@mymailserver.com";
send.From = "validmail@mymailserver.com";
send.Subject = "[MessageTitle]";
send.Priority = MailPriority.High;
send.Body = "Please see attached";
MailAttachment MyAttachment =
new MailAttachment(filename);
send.Attachments.Add(MyAttachment);
SmtpMail.SmtpServer = "10.10.10.1";//the IP address of out Exchange Server
SmtpMail.Send(send);


Here's the error I get when I attempt to run it:

System.Web.HttpException: Could not access 'CDO.Message' object. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x800C000D): The specified protocol is unknown.

--- End of inner exception stack trace ---at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Type type, Object obj, String methodName, Object[] args)
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)
--- End of inner exception stack trace ---
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)
at System.Web.Mail.CdoSysHelper.Send(MailMessage message)
at System.Web.Mail.SmtpMail.Send(MailMessage message)


I've read a *lot* of web articles and tried many of them.  I've tried running this with the SMTP server set as "localhost" and ensured that IIS is set to forward, but nothing seems to work.  It *seems* to work ok (though the message is never delivered) if I take the attachment off, which makes me think that it's not something to do with the smtp server, but I'm open to suggestions.

Thanks

Kevon




Answer this question

How to send mail from a Windows Application??

  • David Xiong

    If anti-virus is enabled on your system.....then it would block the mail from going....try disabling and then sending the mail.



  • FrankVS

    Sorry, is there a forum for .net 2002/3

  • Martyburns

    Your problem is not with the mail server that you're trying to send to, but rather with CDO installed on your local box. Check out this FAQ and see if it helps. It sounds like your problem could be in how CDO is using impersonated credentials to access files on the local box.

    http://www.systemwebmail.com/faq/4.2.8.aspx

    BTW - Microsoft has deprecated System.Web.Mail in favour of the new System.Net.Mail that ships with .NET Framework 2.0.

  • david cantrill

    Unfortunately, this did not help.  I am developing on my local machine, which I have admin rights to on all folders... the article you posted said to ensure that you had rights... or at least that's how I read it.  Am I missing something there

    Kevon

  • shal69503

    Yes, I've looked at the IIS settings for mail forwarding and everything *looks* correct....

    Do we know when .NET 2 is to be released publicly   This actual application is not a high priority.

    Kevon

  • nfaustino

    These forums are targeted at .NET Framework 2.0 and Visual Studio 2005 (cf http://forums.microsoft.com/msdn/ShowPost.aspx PostID=1190), although most posters will respond to queries about .NET Framework 1.X and VS.NET 2002/3.

  • dys

    Few other things to look at... It definitely sounds like an issue with CDO. Have you verified that you have the latest version of CDO installed on your system Have you tried to run your code on a clean machine to ensure it's not something with your particular configuration



  • Geeter

    Perhaps I missed something here...

    The machine that I am running this on is a brand new installation of XP Pro, with VS.NET 2003.  How do I check what version of CDO I have   What is CDO

  • Silvina

    Here is the low-down on CDO (Collaboration Data Objects):

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/exchanchor/htms/msexchsvr_cdo_top.asp

    cdosys.dll (CDO v2) originally shipped with Windows 2000 Server and is now part of Windows 2000, Windows XP, and Windows Server 2003. You can find it in c:\Windows\System32\cdosys.dll. The file version on my Windows XP Pro SP1 is v6.0.6015.0 and on my Windows XP Pro SP2 is 6.2.2.0.

    The System.Web.Mail namespace wraps around functionality provided by cdosys.dll.

    Does this shed any light on your problem



  • RedDawg

    Unfortunately Framework 2.0 is not production material yet :(
  • 13Cats

    The official release date for VS 2005, .NET Framework 2.0, and SQL Server 2005 is November 7, 2005.

  • mgsk

    Have you tried the other suggestions listed in the FAQ that are not permission related For instance, your exception message also states "The specified protocol is unknown", which relates to:

    http://www.systemwebmail.com/faq/4.3.6.aspx



  • Geoff Stockham

    No, problem. MSDN Forums seem to be as good a place as any to get .NET questions answered. I was more commenting to DragonSpeed why I was referring to .NET Framework 2.0 specific features. You might want to note which version you're developing with so that responses can be more targeted.

  • David Smith from MSU!

    Thanks, yes...  I found that I have 6.2.0002.0...  but since that appears to be the most current version, how does this impact my issue

    Kevon

  • How to send mail from a Windows Application??