Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie: mail script with a code-behind in VB

Status
Not open for further replies.

ncar35b

Technical User
Jan 10, 2003
55
US
Hello. I'm struggling to write a mail script (SMTP) that calls the key information (to, from, subject, body) from a code-behind. I can do this inline, but I'd like to be able to reference the code-behind/class, since I'll use it often. Could someone please point me in the right direction, or supply the code for the two pages (the main page and the code-behind)?

Thanks!
 
I'm not sure I understand correctly what you are trying to do, but it seems to me that you should create a different class that implements the functionallity you want, then instantiate it in your CodeBehind in a protected variable and finally use it in your ASPX page.

Let me know if you need further explanation

Best regards,

Mauricio Peccorini
 
Yes, that's exactly what I'm trying to do. What I'm having trouble with is the code/syntax to do this. Here is my code for the inline version. I can't figure out how to put the mail script in a code-behind. Thanks!!



<%@ Page Language="vb" %>
<%@ import Namespace="System.Web.Mail" %>
<script runat="server">

Sub Page_Load(Sender As Object, E As EventArgs)

if Page.IsPostback then



Dim msg as New MailMessage()

msg.To = "test@test.com"
msg.From = "test2@test.com"
msg.Subject = "testing"
'msg.BodyFormat = MailFormat.Html
msg.BodyFormat = MailFormat.Text
msg.Body = txtName.Text

SmtpMail.SmtpServer = "69.41.226.102"

SmtpMail.Send(msg)
msg = Nothing


end if

End Sub

</script>
<html>
<head>
</head>
<body style="FONT: 10pt verdana">
<form runat="server">
<asp:textbox id="txtName" runat="server"></asp:textbox>
<input type="submit" value="Submit Query" />
<br />
</form>
</body>
</html>
 
You've got this inline one running on Page_Load???

Do this in you aspx page
<asp:button id="cmdSend" runat="server" Text="Send Mail"></asp:button>


In code behind:-

<code>
Private Sub cmdSend_click(Sender As Object, E As EventArgs) Handles cmdSend.Click

Dim msg as New MailMessage()

msg.To = "test@test.com"
msg.From = "test2@test.com"
msg.Subject = "testing"
'msg.BodyFormat = MailFormat.Html
msg.BodyFormat = MailFormat.Text
msg.Body = txtName.Text

SmtpMail.SmtpServer = "69.41.226.102"

SmtpMail.Send(msg)
msg = Nothing

End Sub

</code>
HTH
JB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top