What is wrong here?

G

Guest

Hi, friends,

I have a function which send email with attachments as the follows:

public int SendEmailWithAttachment(string fromEmailAddress,
string toEmailAddress, string subject, string message, string attachment)
{
System.Web.Mail.MailMessage mm = new System.Web.Mail.MailMessage();
mm.From = fromEmailAddress;
mm.To = toEmailAddress;
mm.Subject = subject;
mm.Body = message;
mm.BodyFormat = System.Web.Mail.MailFormat.Text;
mm.Priority = System.Web.Mail.MailPriority.Normal;
mm.BodyEncoding = System.Text.Encoding.Default;
mm.UrlContentBase = "";
mm.UrlContentLocation = "";

if (attachment.Trim() != "")
{
char[] delim = new char[] {','};
foreach (string sSubstr in attachment.Split(delim))
{
System.Web.Mail.MailAttachment ma = new
System.Web.Mail.MailAttachment(sSubstr);
mm.Attachments.Add(ma);
}
}

System.Web.Mail.SmtpMail.SmtpServer = ""; //default
System.Web.Mail.SmtpMail.Send(mm);
return 1;
}

It works fine if the if (attachment.Trim() != "") {...} block is NOT
executed. However, if this block IS executed (i.e., having attachment), I got
an error saying:
Could not access 'CDO.Message' object.

But, I really could not find what was wrong. Any ideas, suggestions, etc.?
Thanks a lot.
 
G

Guest

Hi Andrew,

I do not see any problem with you code. Except one point I noticed, where
when you are sending the path of the attachements, make sure you take care of
"\".

If the path is "C:\Temp\Test.txt", then it should be passed as
"C:\\Temp\\Test.txt".

Thanks & Regards,
Sangam
 
J

Jon Skeet [C# MVP]

Sangam said:
I do not see any problem with you code. Except one point I noticed, where
when you are sending the path of the attachements, make sure you take care of
"\".

If the path is "C:\Temp\Test.txt", then it should be passed as
"C:\\Temp\\Test.txt".

Note that that's only the case if the string is a literal in the C#
code, which there's no indication of here. It's not like you need to
need to pass a string which *really* contains double backslashes. (In
fact, I'd go with the verbatim string literal syntax here of
@"C:\Temp\Test.txt".)
 
C

Cor Ligthert

Andrew,

AFAIK, is there just no default SMTP webserver, try it first with an IP (or
name) from an existing one.

Cor
 

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