VB.NET如何实现邮件发送

原创|其它|编辑:郝浩|2010-03-12 11:08:16.000|阅读 1561 次

概述:程序中要用到名称空间(NameSpace)—System.Web.Mail(在.net Framework SDK Beta1版本中叫做System.Web.Util)类库里所提供的邮件发送的对象、属性和方法。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  程序中要用到名称空间(NameSpace)—System.Web.Mail(在.net Framework SDK Beta1版本中叫做System.Web.Util)类库里所提供的邮件发送的对象、属性和方法。

  1、 对象(Object)

  System.Web.Mail名称空间用来发送邮件的对象有SmtpMail、MailMessage和MailAttachment等三个对象,具体作用下文有说明。

  2、 属性(Propertiy)

  System.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 = "me@mycompany.com"
  mail.From = "you@yourcompany.com"
  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 = "me@mycompany.com"
  mail.From = "you@yourcompany.com"
  mail.Subject = "this is a test email."
  mail.BodyFormat = MailFormat.Html
  mail.Body = "this is my test email body.
this part is in bold"
  SmtpMail.SmtpServer = "localhost"    'your real server goes hereSmtpMail.Send(mail)

  '3.发送附件


  Dim mail As New MailMessage
  mail.To = "me@mycompany.com"
  mail.From = "you@yourcompany.com"
  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,修改发件人和收件人的名称比如发件人的地址是abc@126.com,我们用OutLook收到信,From一栏里将直接显示abc@126.com.能不能在From一栏里显示友好一点的名字呢?比如显示Tony Gong.


 Dim mail As New MailMessage
  mail.To = """John"" "
  mail.From = """Tony Gong"" "
  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 = "me@mycompany.com;him@hiscompany.com;her@hercompany.com"
  mail.From = "you@yourcompany.com"
  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 = "me@mycompany.com"
  mail.From = "abc@126.com"
  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")  'set your username here
  mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "Your Password")  'set your password here
  SmtpMail.SmtpServer = "smtp.126.com" 'your real server goes here
  SmtpMail.Send(mail)

  ‘7,修改smtp服务器的端口,以及使用SSL加密大部分smtp服务器的端口是25,但有些却不是同时,绝大部分Smtp服务器不需要SSL登陆,有些却需要比如Gmail,smtp端口是:465,同时支持SSL代码如下:


    Dim mail As New MailMessage
  mail.To = "me@mycompany.com"
  mail.From = "abc@126.com"
  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") 'set your username here
  mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "Your Password") 'set 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)


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP