System.Web.Mail to send out email from desktop app

D

David Cho

I am having trouble using System.Web.Mail to send emails from a desktop
application. The following is my code.

MailMessage _mail = new MailMessage();

_mail.From = "(e-mail address removed)";
_mail.To = "(e-mail address removed)";
_mail.Subject = "howdy";
_mail.Body = "how's your dad?";
_mail.BodyFormat = MailFormat.Text;

// set the default smtp server & send it
SmtpMail.SmtpServer = "mail.smtp.com";
SmtpMail.Send(_mail);

On the SAME MACHINE, running this as a web application works, but
running it as a console application results in

"An unhandled exception of type 'System.Web.HttpException' occurred in
system.web.dll Additional information: Could not access 'CDO.Message'
object."

Strangely, this code used to work, but it does not work anymore. Why
would running it as a web application (on the same machine) work? Is
System.Web.Mail for ASP.NET only? What is the alternative for a desktop
application? I really want to avoid having to use Outlook. Really do.
 
L

Landi

It could be a few things. Are you using these exact lines in your code?
_mail.From = "(e-mail address removed)";
_mail.To = "(e-mail address removed)";
Use a real email address instead.

Second, it could be that the executable doesn't have enough rights and your
ASPNET user does.
SmtpMail.SmtpServer = "mail.smtp.com";
Try using an IP address instead of naming it. Also, try doing
SmtpMail.SmtpServer.Insert(0,"mail.smtp.com")
 
D

David Cho

I solved the problem.

Warning: It is very convoluted.

First,

//not good (but good in ASP.NET)
SmtpMail.SmtpServer = "mail.myserver.com";

//good (don't ask me why)
SmtpMail.SmtpServer.Insert(0, "mail.myserver.com");

Issues with attachments

//Not good (note the relative path)
mail.Attachments.Add(new MailAttachment(@"deathtomicrosoft.txt"));

//good (note the absolute path)
mail.Attachments.Add(new MailAttachment(@"C:\Documents and
Settings\David.SHADOWFAX\My Documents\Visual Studio
Projects\MyProject\bin\Debug\Temp"));

The attachment thing is interesting. When you specify the relative
path, the code does not crash. When you give it a bad file name, it
crashes. So using relative paths SEEMS okay, until you do
SmtpMail.Send.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top