以用户控件形式写的。
sendmail.ascx:
sendmail.ascx.vb:
Imports System.Web.Mail
Imports System.IO
Public Class SendMail2
Inherits System.Web.UI.UserControl
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
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.WebControls.RegularExpressionValidator
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
'
Public MailBoxName As String '邮箱名字
Public MailAddress As String '邮箱地址
Public MailUserMail As String '如果需要验证,这是代理的发件人的验证邮箱
Public MailUserName As String = "" '如果需要验证,这是用户名
Public MailUserPass As String '如果需要验证,这是密码
Public MailSMTPServer As String '邮件发送服务器
Public NeedAddFile As Boolean = False '是否允许提交附件
Public AttachFileExtNames As String = ",txt,doc,xls,jpg,gif,bmp," '附件类型限制
Public AttachFileSize As String = "500" '附件大小限制
Public UseJMail As Boolean = True '是否使用JMail组件发送(4.3以上)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Me.labMess.Text = ""
If Not Page.IsPostBack Then
Me.labMailBox.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 & "") = "" Then
Throw New Exception("必须填写内容。")
End If
If Me.txtMailAdd.Text = "" Then
Me.txtMailAdd.Text = MailUserMail
End If
If UseJMail Then
JMailSendMail43()
Else
CDOSendMail()
End If
Me.labMess.Text = "邮件发送成功,谢谢!"
Catch ex As Exception
Me.labMess.Text = "错误:" & ex.Message
End Try
End Sub
Private Function CDOSendMail() As Boolean
'
'使用CDO发送邮件
Try
Dim oMsg As New CDO.Message
'
'收件人邮箱
oMsg.To = MailAddress
Dim strSub As String
If Me.txtName.Text & "" = "" Then
strSub = "网友"
Else
strSub = Me.txtName.Text
End If
'
'发件人邮箱及友好名称
oMsg.From = Me.txtMailAdd.Text & "(" & strSub & ")"
'
'邮件主题
strSub = "来自[" & strSub & "]从[" & MailBoxName & "]提交的邮件"
oMsg.Subject = strSub
'
'邮件内容
oMsg.TextBody = Me.txtContext.Text & ""
'
'增加附件,如果有
Dim strAddFileName As String
strAddFileName = UpAddFile()
If strAddFileName <> "" Then
oMsg.AddAttachment(strAddFileName)
End If
'
'认证
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 '语言代码
oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = IIf(MailSMTPServer = "", "127.0.0.1", MailSMTPServer)
oFields.Update()
'
'设置字符集
oMsg.TextBodyPart.Charset = "gb2312"
oMsg.Send()
oMsg = Nothing
Return True
Catch ex As Exception
Throw New Exception("使用CDO发送邮件失败:" & ex.Message)
Return False
End Try
End Function
Private Function JMailSendMail43() As Boolean
'
'使用JMail4.3组件发送邮件
Try
Dim jmail1 As Object
jmail1 = Server.CreateObject("JMAIL.Message") '建立发送邮件的对象
jmail1.silent = True '屏蔽例外错误,返回FALSE跟TRUE两值j
jmail1.logging = True '启用邮件日志
jmail1.Charset = "GB2312" '邮件的文字编码为国标
jmail1.ContentType = "text/html" '邮件的格式为HTML格式
jmail1.AddRecipient(MailAddress) '邮件收件人的地址
If MailUserName <> "" Then
jmail1.From = Me.txtMailAdd.Text '发件人的E-MAIL地址
jmail1.MailServerUserName = MailUserName '登录邮件服务器所需的用户名
jmail1.MailServerPassword = MailUserPass '登录邮件服务器所需的密码
Else
jmail1.From = Me.txtMailAdd.Text
End If
jmail1.Subject = "来自 [" & MailBoxName & "]" & Me.txtName.Text & " 的邮件" '邮件的标题
jmail1.FromName = Me.txtName.Text & ""
jmail1.Body = Me.txtContext.Text '邮件的内容
'
'增加附件
Dim strAddFileName As String
strAddFileName = UpAddFile()
If strAddFileName <> "" Then
jmail1.AddAttachment(strAddFileName)
End If
'jmail1.Prority = 1 '邮件的紧急程序,1 为最快,5 为最慢, 3 为默认值
jmail1.Send(IIf(MailSMTPServer = "", "127.0.0.1", MailSMTPServer)) '执行邮件发送(通过邮件服务器地址)
jmail1.Close() '关闭对象
Return True
Catch ex As Exception
Throw New Exception("使用JMail发送邮件失败:" & ex.Message)
Return False
End Try
End Function
Private Function UpAddFile() As String
'
'上载文件到UpFiles/MailAddFile目录,返回文件路径名
'如果没有文件上载返回空串
If Me.File1.PostedFile.ContentLength > 0 Then
'
'生成文件名
Dim strExt, strFileName, strPathAll As String
'
'扩展名,处理扩展名限制
'在web.config 节中配置,如:
'
'
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(".") & ",", CompareMethod.Text) <= 0 Then
Dim myEx As New Exception("扩展名限制。禁止上载后缀为""" & strExt & """的文件")
Throw myEx
End If
'
'处理文件大小限制
'在web.config 节中配置,如:
'单位是K
'
Dim intSetSize As Integer
If AttachFileSize & "" <> "" Then
intSetSize = CInt("0" & AttachFileSize)
Else
intSetSize = CInt("0" & GetConfigValue("MailAttachFileSize"))
End If
If intSetSize * 1024 < File1.PostedFile.ContentLength Then
Dim myEx As New Exception("附件长度超过限制。禁止上载大于" & intSetSize & "K的文件。")
Throw myEx
End If
strFileName = Path.GetFileName(Me.File1.PostedFile.FileName)
Dim strDir As String
strDir = Server.MapPath(gRootDir & GetConfigValue("MailAttachFile"))
strPathAll = Path.Combine(strDir, strFileName)
'
'如果有,先删除
If File.Exists(strPathAll) Then
File.Delete(strPathAll)
End If
'
'上载文件
File1.PostedFile.SaveAs(strPathAll)
Return strPathAll
Else
Return ""
End If
End Function
End Class
发邮件至: | |
您的邮箱: | *邮件地址格式错误。 |
您的姓名: | |
建议或意见: |
*必填项不能为空。 |
附件: | |
| |
| |
|