和VB.net基本一样
不过还是分开来记录比较好点,呵呵!
界面:
后台代码:
Partial Class TestMail
Inherits System.Web.UI.Page
Dim maillist As String
Dim mSplit As Array
Dim mlist As New ArrayList
Dim mMailAddr As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub send_mail(ByVal strSubject As String, ByVal mFile As String)
Dim madFrom As New System.Net.Mail.MailAddress(txtfrom.Text.Trim) '寄件人
' Dim madFrom As New System.Net.Mail.MailAddress(Environment.UserName & "@cn.flextronics.com") '寄件人
Dim smt As New System.Net.Mail.SmtpClient() 'SMTP Server
smt.Host = txtserver.Text.Trim '發信主機名稱
Dim mms As New System.Net.Mail.MailMessage
mms.From = madFrom
mms.SubjectEncoding = System.Text.Encoding.GetEncoding("UTF-8")
mms.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8")
Call sMail_list(mms)
mms.Subject = strSubject.Replace(Chr(10), "").Replace(Chr(13), "") '信件主旨
mms.Body = "Dear All: <br> Test Mail for web send" '信件內容
Dim attach As System.Net.Mail.Attachment = New Net.Mail.Attachment(txtattach.Text)
mms.Attachments.Add(attach)
mms.IsBodyHtml = True
Try
smt.Send(mms)
Response.Write("<script>alert('發送成功');</script>")
Catch ex As Exception
Finally
mms.Dispose()
End Try
End Sub
Sub sMail_list(ByVal colAddrs As System.Net.Mail.MailMessage)
'Dim mMailAddr As String
Dim NewPath As String = Environment.CurrentDirectory
Dim i, j As Integer
' Dim MMEN As Integer
Dim mstring As String
maillist = txtto.Text.Trim
mSplit = Trim(maillist.ToLower).Split(",")
If mSplit.Length > 0 Then
For j = 0 To mSplit.Length - 1
If mSplit(j).ToString.Trim <> "" Then
If InStr(mSplit(j).ToString.Trim, "@") = 0 Then
mSplit(j) &= "@hotmail.com"
End If
mlist.Add(mSplit(j).ToString.Trim)
End If
Next
End If
If mlist.Count <> 0 Then
For i = 0 To mlist.Count - 1
mMailAddr = Trim(mlist.Item(i))
If mMailAddr.Trim <> "" Then
If InStr(mMailAddr, "@") = 0 Then
mMailAddr &= "@hotmail.com"
End If
colAddrs.To.Add(mMailAddr)
End If
Next
End If
colAddrs.To.Add("charycheng@arimacomm.com.cn")
End Sub
Protected Sub btnsend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsend.Click
Dim strsubject As String = txtsubject.Text
send_mail(strsubject & Now, "Hello<br>" & Now & "<br>Comput Name :" & My.Computer.Name)
End Sub
End Class