Link to home
Start Free TrialLog in
Avatar of m_evergreen
m_evergreen

asked on

trouble with sending email attachment from windows application

Error:
An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll

Additional information: Could not access 'CDO.Message' object.

innerexception is "Exception has been thrown by the target of an invocation."

innerexception.innerexception is "The specified protocol is unknown."

I experimented, removing the attachment references and the error goes away, however this doesn't get the attachment sent.  My code for sending the mail with attachment looks like this:

        Dim attachment As New System.web.mail.MailAttachment(filename)
        Dim Message As New System.Web.Mail.MailMessage
        Message.To = " <m_evergreen@yahoo.com>"
        Message.From = "Order System <m_evergreen@yahoo.com>"
        Message.Body = "Attachment:  text file"
        Message.Subject = "Your Request"
        Message.Attachments.Add(attachment)
        System.Web.Mail.SmtpMail.SmtpServer = "mail.company.local"
        System.Web.Mail.SmtpMail.Send(Message)

I'm beginning to suspect that the problem may stem from this actually being a windows application instead of a web application.  I imported System.Web references which includes System.Web.Mail of course.  Is there something else I need in references?

Let me know if more code is needed to troubleshoot.  Thank you.
Avatar of EBatista
EBatista

looks like if the system cant reach the SmtpServer, try placing its IP address instead.

I that dont help look at this comment i have found somewhere:
"...whenever you get the message "Could not access 'CDO.Message' object." message. the true problem is hidden in another message that was produced at a lower level in the application.
To get to this message you have to trap the exception using the following exception description and then print out the complete exception to find the buried problem..."

Try
    SmtpMail.Send(oMsg)
Catch ehttp As System.Web.HttpException              
    MessageBox.Show(ehttp.ToString)
Avatar of m_evergreen

ASKER

Since it works fine without the attachment I don't believe the SmtpServer is the cause of the problem.  There seems to be something about the attachment which is causing the problem.

The final error, or innerexception, is "The specified protocol is unknown."
if the problem is only the attachment, then you should check that the correct right are setted to the folder containing that files.
ASKER CERTIFIED SOLUTION
Avatar of EBatista
EBatista

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, I've found a fix for the problem. I started by adding c:/ to
my file name, giving me something like this:

filename = "c:/" & tbl.Rows(A)("CustomerID") & tbl.Rows(A)("FilterID") &
".txt"

This worked on my machine but I was concerned that it might not work on
the server. After giving it some thought I changed the code again
adding to the top:

Imports System.io

and then later in the code:

direct = Directory.GetCurrentDirectory()
filename = direct & tbl.Rows(A)("CustomerID") & tbl.Rows(A)("FilterID")
& ".txt"

I hope this will help anyone in a similar situation.
glad to see that you find it, the part 2 from my post above point you to do the same as you did.
the point is that one can only create a MailAttachment by specifying an absolute path.

for anybody else that read this thread, for an asp .net app must be used the absolute path too:

//ASP.NET app, absolute path created with Server.MapPath property
MailAttachment a = new MailAttachment( Server.MapPath("test.txt"));
m.Attachments.Add( a );

//console and winform app
MailAttachment a = new MailAttachment( "c:\\test.txt" );
m.Attachments.Add( a );