None of this is my code. I've tried several versions I've found. I just can't make it work.
Where does this go? <% @Import Namespace="System.Web.Mail" %>
Or is it this? Imports System.Net.Mail
This code was from a guy yesterday on the Web Forms board.
http://forums.asp.net/t/971802.aspx
Calling the function from code
MailHelper.SendMailMessage("[email protected]", "[email protected]", "[email protected]",
"[email protected]", "Sample Subject", "Sample body of text for mail message")
MailHelper.vb
Imports System.Net.Mail
Public Class MailHelper
''' <summary>
''' Sends an mail message
''' </summary>
''' <param name="from">Sender address</param>
''' <param name="recepient">Recepient address</param>
''' <param name="bcc">Bcc recepient</param>
''' <param name="cc">Cc recepient</param>
''' <param name="subject">Subject of mail message</param>
''' <param name="body">Body of mail message</param>
Public Shared Sub SendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As
String, ByVal subject As String, ByVal body As String)
' Instantiate a new instance of MailMessage
Dim mMailMessage As New MailMessage()
' Set the sender address of the mail message
mMailMessage.From = New MailAddress(from)
' Set the recepient address of the mail message
mMailMessage.To.Add(New MailAddress(recepient))
' Check if the bcc value is nothing or an empty string
If Not bcc Is Nothing And bcc <> String.Empty Then
' Set the Bcc address of the mail message
mMailMessage.Bcc.Add(New MailAddress(bcc))
End If
' Check if the cc value is nothing or an empty value
If Not cc Is Nothing And cc <> String.Empty Then
' Set the CC address of the mail message
mMailMessage.CC.Add(New MailAddress(cc))
End If
' Set the subject of the mail message
mMailMessage.Subject = subject
' Set the body of the mail message
mMailMessage.Body = body
' Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = True
' Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal
' Instantiate a new instance of SmtpClient
Dim mSmtpClient As New SmtpClient()
' Send the mail message
mSmtpClient.Send(mMailMessage)
End Sub
End Class
Web.config
<?
xml version="1.0"?>
<configuration>
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="smtp.yourdomain.com" port="25" userName="yourUserName" password="yourPassword"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
...and this one is from 4GuysFromRolla.com
Source Code for Creating an ASP.NET Visitor Feedback Page
<% @Import Namespace="System.Web.Mail" %>
<script language="vb" runat="server">
Sub btnSendFeedback_Click(sender as Object, e as EventArgs)
'Create an instance of the MailMessage class
Dim objMM as New MailMessage()
'Set the properties - send the email to the person who filled out the
'feedback form.
objMM.To = "[email protected]"
objMM.From = txtEmail.Text
'If you want to CC this email to someone else, uncomment the line below
'objMM.Cc = "[email protected]"
'If you want to BCC this email to someone else, uncomment the line below
'objMM.Bcc = "[email protected]"
'Send the email in text format
objMM.BodyFormat = MailFormat.Text
'(to send HTML format, change MailFormat.Text to MailFormat.Html)
'Set the priority - options are High, Low, and Normal
objMM.Priority = MailPriority.Normal
'Set the subject
objMM.Subject = "4GuysFromRolla.com - Feedback"
'Set the body
objMM.Body = "At " + DateTime.Now + " feedback was sent from an ASP.NET " & _
"Web page. Below you will find the feedback message " & _
"send by " & txtName.Text & "." & vbCrLf & vbCrLf & _
"---------------------------------------" & vbCrLf & vbCrLf & _
txtMessage.Text & vbCrLf
'Specify to use the default Smtp Server
SmtpMail.SmtpServer = ""
'Now, to send the message, use the Send method of the SmtpMail class
SmtpMail.Send(objMM)
panelSendEmail.Visible = false
panelMailSent.Visible = true
End Sub
</script>
<html>
<body>
<asp:panel id="panelSendEmail" runat="server">
<form runat="server">
<h2>We are interested in your feedback! Please enter the following
requested information below to send us your comments.</h2>
<b>Your Name:</b>
<asp:textbox id="txtName" runat="server" />
<br>
<b>Your Email Address:</b>
<asp:textbox id="txtEmail" runat="server" />
<p>
<b>Your Message:</b><br>
<asp:textbox id="txtMessage" TextMode="MultiLine"
Columns="40" Rows="10" runat="server" />
<p>
<asp:button runat="server" id="btnSendFeedback" Text="Send Feedback!"
OnClick="btnSendFeedback_Click" />
</form>
</asp:panel>
<asp:panel id="panelMailSent" runat="server" Visible="False">
An email has been sent to the email address you specified. Thanks!
</asp:panel>
</body>
</html>