asp.net中利用CDONTS發送郵件的列子

以用戶控件形式寫的。 sendmail.ascx: <%@ Control Language="vb" AutoEventWireup="false" Codebehind="SendMail2.ascx.vb" Inherits="LYEduInfo.SendMail2" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

*郵件地址格式錯誤。


*必填項不能爲空。

 

 

 

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
發佈了48 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章