Monday, March 26, 2012

Strange Error

Hi everyone!
now, i have a small problem. It is not easy to understand with me.
I want to send an email to my email with an attachment. This is my code:
<%@dotnet.itags.org. Page Language="vb" CodeBehind="MailAttachment.aspx.vb" AutoEventWireup="false" Inherits="WebApplication2.MailAttachment" %>
<% @dotnet.itags.org.Import Namespace="System.Web.Mail" %>
<%
dim msgMail as new MailMessage

msgMail.To = "thedoem111@dotnet.itags.org.yahoo.com"
msgMail.From = "tuthanvn84@dotnet.itags.org.yahoo.com"
msgMail.Subject = "Attachment Test"

msgMail.BodyFormat = MailFormat.Text
msgMail.Body = "Check out the attachment!"
msgMail.Attachments.Add(new MailAttachment(Server.MapPath("MyExcel.xls")))
try
SmtpMail.Send(msgMail)

Response.Write("Email was queued to disk")
catch ex as exception
Response.Write(Ex.message)

end try

%>
But I want to use codebehind. They are occur an error at this line:
msgMail.Attachments.Add(new MailAttachment(Server.MapPath("MyExcel.xls")))

I don't understand what really happen. Can anyone tell me what happen?
Thank for any suggesstion!hi,
i do not know what the error u are getting. my guess would be that it is an attachment size error. for this u may want to check the size of the attachments set on ur SMTP server, that u can find on ur IIS server.
if its an other error please post the error .

- gkopparam
hi gkopparam!
the first, It is code, not in runtime.
the second, I had tested. The first code is successful. Then i copy and paste in codebehind file. It occur an error at that line.
1stly, what is the exact error you are getting. A screen shot of the stack trace would be usefull.

Also, what is the string value of:

Server.MapPath("MyExcel.xls")))

Woka
Hi Wokawidget!
I'm sorry if my description is not clearly for you.

So i have two screenshoots here. Can you tell me what my error is?

http://i117.photobucket.com/albums/o50/lekhuyen/FirstCode.jpg

and

http://i117.photobucket.com/albums/o50/lekhuyen/SecondCode.jpg

Best regards!
If you hover of the the squiggley line then .NET will tell you the error, which is Server.MapPath does not exist.

The server object is taken from the page object's base. So if you put that code into an aspx page and not a standard class it will work.

However, I would prefer to keep functions like this in classes, which is good coding, but u will need to replace the Server.MapPath bit with a param that gets passed in, ie

Public Sub SendMail(ByVal recipients As String, ByVal attachmentPath As String)

msg.Attachments.Add(attachmnetPath)

End Sub

Then from the web page it can be called like so:

MyClass.SendMail("Woof@.Woka.com", Server.MapPath("MyFile.woof"))

Woka
Also, little tip with forum posting. Try to take out the email addresses in your screenshot. This forum is thankfully filled with trustworthy folk but the last thing you want is your email adress advertised publicly like this :thumb:
Yea, that's a damn good pt. Woka@.Woof.com was a made up address for this very reason. Spiders and bots trawl these pages and strip out any email addresses it finds so that they can be used for spamming.

Woka
Unfortunately I can't take all the credit as it's mentioned in the "Before you post..." announcement by Brad :D

0 comments:

Post a Comment