Saturday, March 24, 2012

SMTPClient email problem

Can anyone help me out?

I am trying to send an email in my asp.net 2.0 application using the code below :

Dim

mailAsNew MailMessage()'set the addresses
mail.From =New MailAddress(strApplicantEmail)
mail.To.Add(info@dotnet.itags.org.domain.co.uk)
'set the content
mail.Subject ="Random Text :- " & strJobRef
mail.Body = strApplicantName & strApplicantContactNo

Dim strLongFilePathAsString = FileUpload1.PostedFile.FileNamemail.Attachments.Add(New Attachment(strLongFilePath))'send the message
Dim MailObjAsNew System.Net.Mail.SmtpClient
Dim credAsNew Net.NetworkCredential("username","password")

MailObj.Credentials = cred
MailObj.Host =

"mail.domain.co.uk"

MailObj.Send(mail)

The code works perfectly in my IDE, but when I run this from the server I get the following error message :-

Could not find a part of the path 'C:\Documents and Settings\User\My Documents\Random Document.doc'.

Can anyone help me out?

Thanks

The obvious answer is that when you run it from your IDE the file is on your local hard disc, when the server runs it it doesn't exist inC:\Documents and Settings\User\My Documents\. coz c:\ is a reference to the server hard disc.

As stepping through your code, i realized that you arent upload the file selected by the user to your web server and then send the email. it was working on your machine, because your computer serves as a webserver and webclient in the developement phase, one you have uploaded your application into another domain, you have to have that file on the webserver.

in your case before you attach the file, use

FileUpload1.PostedFile.SaveAs(Server.MapPath("~") + "/" + strLongFilePath) // this will upload the file into your application root folder

then you could add the attachment file to your mail .

mail.Attachments.Add(new Attachments(Server.MapPath("~") + "/" + strLongFilePath)

HC


Thanks for the reply.

The lineFileUpload1.PostedFile.SaveAs(Server.MapPath("~") + "/" + strLongFilePath)

is now giving me the errorThe given path's format is not supported.

Any ideas?


you have to use just the file name of the uploaded file and not all file path.

Dim fileName as string

fileName = System.IO.Path.GetFileName(strLongFilePath)

FileUpload1.PostedFile.SaveAs(Server.MapPath("~") + "/" +fileName)

HC


Thanks very much, that has solved it!

I've marked your post as the answer.


Another problem I'm afraid. The above code, which works really well in IE6 & IE7 does not work correctly in Firefox. In Firefox, the email is sent but the attachment is not included. Anyone know why this may be the case.

Thanks

Mike..


Its ok folks, I've double checked my code and there was a problem with the logic. Issue resolved now.

0 comments:

Post a Comment