Written in the form of user controls. sendmail.ascx: <% @ Control Language = "vb" AutoEventWireup = "false" Codebehind = "SendMail2.ascx.vb" Inherits = "LYEduInfo.SendMail2" TargetSchema = "http://schemas.microsoft.com/intellisense/ie5 "%>
Email:
your mailbox:
* Mail address format error.
Your Name:
Suggestions or opinions:
* The required item cannot be empty.
annex:
Sendmail.ascx.vb: imports system.web.mail imports system.io public class sendmail2 inherits system.Web.ui.userControl #Region "Web Form Designer Generated Code" This call is required for web form designer of.
Private Sub InitializeComponent () End Sub Protected WithEvents txtMailAdd As System.Web.UI.WebControls.TextBox Protected WithEvents txtContext As System.Web.UI.WebControls.TextBox Protected WithEvents labMailBox As System.Web.UI.WebControls.Label Protected WithEvents txtName As System.Web.UI.WebControls.TextBox Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents Label3 As System.Web.UI.WebControls.Label Protected WithEvents Label4 As System.Web.UI.WebControls.Label Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile Protected WithEvents btnSend As System.Web.UI.WebControls.Button Protected WithEvents labMess As System.Web.UI.WebControls.Label Protected WithEvents labAddFile As System.Web.UI.WebControls.Label Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator Protected WithEvents RegularExpressionValidator1 As System.Web.UI.WebCo ntrols.regularExpressionValidator 'Note: The following placeholders declare are required for the web form designer. 'Don't delete or move it. Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method is called the Web Form Designer required' Do not use the code editor to modify it.
InitializeComponent () End Sub #End Region 'Public MailBoxName As String' mailbox name Public MailAddress As String 'e-mail address Public MailUserMail As String' If authentication is required, which is the agent of the sender to verify the mailbox Public MailUserName As String = "" ' If you need to verify that this is the username Public MailUserPass As String 'If you need to verify that this is the password Public MailSMTPServer As String' Outgoing Mail server Public NeedAddFile As Boolean = False 'is allowed to submit attachments Public AttachFileExtNames As String = ", txt, doc, XLS, JPG, GIF, BMP, "'Attachment Type Limit Public AttachFileSize AS String =" 500 "' Attachment Size Public UseJmail As Boolean = True 'Does Send (4.3 or more) Private Sub Page_Load (Byval Sender Asystem. Object, byval e as system.eventargs) Handles mybase.load 'Place the initialization page here's user code me.LabMESS.TEXT = "" if not page.text = mailboxname & "if not needaddfile Then Me.labAddFile.Visible = False Me.File1.Visible = False End If End If End Sub Private Sub btnSend_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click Try If Trim (Me.txtContext .Text & "") = "" "" "" Ption ("must fill in the content.
") End if if me.txtmailadd.text =" "" The me.txtmailadd.text = mailusermail endiffmail43 () else cdosendmail () end if me.LabMES.Text = "Mail Send success, thank you! "Catch exception me.LabMESS.TEXT =" error: "& ex.Message End Try End Sub Private Function CDOSENDMAIL () AS Boolean '' uses CDO Send Mail Try Dim OMSG AS New CDO.MESSAGE '' Reconference Mailbox omsg.to = mailaddress Dim strsub as string if me.txtname.text & "=" "" "=" "" = "Else strsub = me.txtname.text end if '' sender mailbox and friendly name omsg.from = Me.txtMailadd.Text & "(" & strsub & ")" '' Mail Topic strsub = "from [" & STRSUB & "] Submitted from [& MailboxName &"] "OMSG.Subject = strsub '" mail SUMMARY oMsg.TextBody = Me.txtContext.Text & "" '' add an attachment, if Dim strAddFileName As String strAddFileName = UpAddFile () If strAddFileName <> "" Then oMsg.AddAttachment (strAddFileName) End If '' authentication Dim iConfg As CDO.IConfiguration = oMsg.Configuration Dim oFields As ADODB.Fields = iConfg.Fields oFields ( "http://schemas.microsoft.com/cdo/configuration/sendusing") .Value = 2 If MailUserName <> "" Then oFields ( "http://schemas.microsoft.com/cdo/configuration/sendusername"").value = mailusername ofields (" http://schemas.microsoft.com/cdo/configuration/ sendpassword "). Value = MailUserPass oFields (" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate ") .Value = 1 End If oFields (" http://schemas.microsoft.com/cdo/configuration/ Languagecode "). Value =
& H804 'language code of deields ("http://schemas.microsoft.com/cdo/configuration/smtpserver) .Value = IIF (Mailsmtpserver =", "127.0.0.1", mailsmtpserver) Ofields.Update () Settings charset oMsg.TextBodyPart.Charset = "gb2312" oMsg.Send () oMsg = Nothing Return True Catch ex As Exception Throw New Exception ( "failed to send mail using CDO:" & ex.Message) Return False End Try End Function Private Function Jmailsendmail43 () AS Boolean '' uses JMail4.3 components Send Mail Try Dim Jmail1 As Object Jmail1 = Server.createObject ("Jmail.Message") 'Establishing an object Jmail1.silent = true' shielded exception error, returning false True two-value Jmail1.logging = true 'Enables mail log jmail1.charset = "GB2312" message text code to national standard jmail1.contenttype = "text / html" message format is HTML format jmail1.addrecipient (MailadDress)' e-mail address of the recipient If MailUserName <> "" Then jmail1.From = Me.txtMailAdd.Text 'sender E-MAIL address jmail1.MailServerUserName = MailUserName' login required mail server user name jmail1.MailServerPassword = MailUserPass 'The password required for login mail server Else Jmail1.From = Me.txtmailadd.Text end if jmail1.subject = "From [& MailboxName &"] "& me.txtname.text &" mail " 'Me.txtName.Text & title message jmail1.FromName =" "jmail1.Body = Me.txtContext.Text' message content 'add an attachment Dim strAddFileName As String strAddFileName = UpAddFile () If strAddFileName <>" "Then Jmail1.addattachment (straddfilename) end if 'jmail1.prority = 1' email emergency, 1 is the fastest, 5 is the slowest, 3 is the default value jmail1.send (IIF (Mailsmtpserver = "," 127.0.0.1 " Mailsmtpserver) 'Execute Mail Send (via mail server address)
jmail1.Close () 'Close Object Return True Catch ex As Exception Throw New Exception ( "E-mail using JMail Failed:" & ex.Message) Return False End Try End Function Private Function UpAddFile () As String' 'uploading files to UpFiles / MAILADDFILE directory, return file path name 'If there is no file upload return empty string if me.file1.postedfile.contentLength> 0 Then' 'generated file name Dim Street, StrfileName, StrPathall AS String' extension, processing extension restriction ' Configure in the Web.config section, such as: '
'StrExt = Path.GetExtension (File1.PostedFile.FileName & "") Dim strSetExt As String If AttachFileExtNames & "" <> "" Then strSetExt = AttachFileExtNames & "" Else strSetExt = GetConfigValue ( "MailAttachFileExtNames") End If If InStr ( StRSETEXT, "," & Strext.trim (".") & ",", "," "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "File") Throw myex end if '' handling file size limit 'in Web.config
Configuration in the section, such as: 'unit is k'