Thursday, March 22, 2012

Why these code cant work

Dim mailMessage As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage
mailMessage.From = "kandy@dotnet.itags.org.js1011.com"
mailMessage.To = "kandy@dotnet.itags.org.js1011.com"
mailMessage.Subject = "Email Subject"
mailMessage.BodyFormat = System.Web.Mail.MailFormat.Html

' TODO: Set the mailMessage.Body property
'mailMessage.Body = "test"

System.Web.Mail.SmtpMail.SmtpServer = "localhost"
System.Web.Mail.SmtpMail.Send(mailMessage)

Looks OK but you could add


Try

System.Web.Mail.SmtpMail.Send(mailMessage)

ErrorLabel.Text = "Message Sent"

Catch ex As Exception

ErrorLabel.Text = ex.Message

End Try

to try and display the error

Simon
Thank you !!

error message is : Can't find CDO.Message object

What's wrong?
help me~~~
Your code is okay, but either you haven't set up the SMTP mail server correctly, or you haven't set one up at all.

Have a look at the replies tothis same question on Extreme .NET Forums. You will see that the talk is about setting up an SMTP mail server, not about the code.

Also, it's probably a bad idea to give a variable the same name as its own type. You hadDim mailMessage As System.Web.Mail.MailMessage. It makes it much harder to read your code then, as we (and you yourself later on) cannot tell whether you are referring to a variable or a type.

For what it's worth, here is my edited version of your code:

<%@. Page Language="VB" %>
<%@. import Namespace="System.Data" %>
<script runat="server"
Sub btnSend_Click ( ByVal sender As Object, ByVal eArgs As EventArgs )

Dim thisMailMessage As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage
thisMailMessage.From = "kandy@.js1011.com"
thisMailMessage.To = "kandy@.js1011.com"
thisMailMessage.Subject = "Email Subject"
thisMailMessage.BodyFormat = System.Web.Mail.MailFormat.Html
thisMailMessage.Body = "test"

' We can comment out the next line to see if there is a default SMTP server
System.Web.Mail.SmtpMail.SmtpServer = "localhost"

Try
System.Web.Mail.SmtpMail.Send(thisMailMessage)
lblMessage.text = "Message sent"
Catch e As Exception
lblMessage.text = "Message failed: " & e.Message()
End Try

End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:Button id="btnSend" text="Send" onclick="btnSend_Click" runat="server" />
<br />
<asp:Label id="lblMessage" runat="server" />
</form>
</body>
</html>


thank you
now I know the reason

do not use the smtp server

0 comments:

Post a Comment