1、 对象(Object)
.Web.Mail名称空间用来发送邮件对象有SmtpMail、MailMessage和MailAttachment等 3个对象具体作用下文有介绍说明
2、 属性(Propertiy)
.Web.Mail名称空间主要属性是MailMessage对象属性下表列出了MailMessage对象属性名称及其意义:
属性名称 代表意义
From 发信人地址(源地址)
To 接收人地址(目地制)
Subject 邮件标题
Priority 邮件优先级(High,Low,Normal)
Attachment 邮件附件
Bcc 暗送地址
Cc 抄送地址
Body 邮件主体
BodyFormat 邮件格式(Html格式、Text格式)
Bodyencoding 编码(Base64,UUencode)
3、 思路方法(Method)
Send思路方法邮件就是通过Send思路方法发送出去该思路方法有两种方式:
1) SmtpMail.Send(“源地址”,”目标地址”,”主题”,”内容”)
2) SmtpMail.Send(MailMessage)
'1.发送简单信件
Dim mail As New MailMessage
mail.To = "[email protected]"
mail.From = "[email protected]"
mail.Subject = "this is a test email."
mail.Body = "this is my test email body"
SmtpMail.SmtpServer = "localhost" 'your real server goes here
SmtpMail.Send(mail)
'这里smtpserver只能是那些不需要验证smtp服务器像126,sina,yahoo等等邮箱都需要验证所以不能用用这些邮箱发信后面会讲到
'2.发送html信件
Dim mail As New MailMessage
mail.To = "[email protected]"
mail.From = "[email protected]"
mail.Subject = "this is a test email."
mail.BodyFormat = MailFormat.Html
mail.Body = "this is my test email body.<br><b>this part is in bold</b>"
SmtpMail.SmtpServer = "localhost" 'your real server goes hereSmtpMail.Send(mail)
'3.发送附件
Dim mail As New MailMessage
mail.To = "[email protected]"
mail.From = "[email protected]"
mail.Subject = "this is a test email."
mail.Body = "this is my test email body."
Dim attachment As New MailAttachment(Server.MapPath("test.txt")) 'create the attachment
mail.Attachments.Add(attachment) 'add the attachment
SmtpMail.SmtpServer = "localhost" 'your real server goes here
SmtpMail.Send(mail)
'4修改发件人和收件人名称比如发件人地址是[email protected],我们用outlook收到信From栏里将直接显示[email protected].能不能在From栏里显示友好点名字呢?比如显示Tony Gong.
Dim mail As New MailMessage
mail.To = """John"" <[email protected]>"
mail.From = """Tony Gong"" <[email protected]>"
mail.Subject = "this is a test email."
mail.Body = "this is my test email body."
SmtpMail.SmtpServer = "localhost" 'your real server goes here
SmtpMail.Send(mail)
'5,发送给多人
Dim mail As New MailMessage
mail.To = "[email protected];[email protected];[email protected]"
mail.From = "[email protected]"
mail.Subject = "this is a test email."
mail.Body = "this is my test email body."
SmtpMail.SmtpServer = "localhost" 'your real server goes here
SmtpMail.Send(mail)
'6用需要Smtp验证邮箱发信
'现在为了防止垃圾邮件(, 绝大部分Smtp服务器需要验证了)
'发信思路方法如下:
Dim mail As New MailMessage
mail.To = "[email protected]"
mail.From = "[email protected]"
mail.Subject = "this is a test email."
mail.Body = "Some text goes here"
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 'basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc") ' your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "Your Password") ' your password here
SmtpMail.SmtpServer = "smtp.126.com" 'your real server goes here
SmtpMail.Send(mail)
‘7,修改smtp服务器端口以及使用SSL加密大部分smtp服务器端口是25但有些却不是同时绝大部分Smtp服务器不需要SSL登陆有些却需要比如Gmailsmtp端口是:465同时支持SSL代码如下:
Dim mail As New MailMessage
mail.To = "[email protected]"
mail.From = "[email protected]"
mail.Subject = "this is a test email."
mail.Body = "Some text goes here"
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 'basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc") ' your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "Your Password") ' your password here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465)
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true")
SmtpMail.SmtpServer = "smtp.126.com" 'your real server goes here
SmtpMail.Send(mail)
最新评论