许可应用顺序运用简朴邮件传输协定 (SMTP) 发送电子邮件。
定名空间:system.net.mail
属性
ClientCertificates:指定应运用哪一个证书来竖立平安套接字层(SSL)衔接
Credentials:猎取或设置用来对发件人举行身份验证的凭证
DeliveryFormat:猎取或设置所运用的通报花样SmtpClient发送电子邮件
DeliveryMethod:指定怎样发送的电子邮件将处置惩罚音讯
EnableSsl:指定是不是SmtpClient运用平安套接字层(SSL)加密的衔接
Host:猎取或设置一个或多个SMTP生意业务纪录所用的主机的IP地点
PickupDirectoryLocation:猎取或设置应用顺序在其中保留邮件以处置惩罚由当地SMTP效劳器的文件夹
Port:猎取或设置用于SMTP事物的端口
ServicePoint:猎取用于传输电子邮件的网络衔接
TargetName:猎取或设置效劳供应顺序称号(SPN)时运用扩大的庇护用于举行身份验证
Timeout:猎取或设置一个值,指定Send挪用的超时时候
UseDefaultCredentials:猎取或设置Boolean值,该值掌握是不是DefaultCredentials随要求一同发送
要领
Dispose()
将一条QUIT音讯发送到SMTP效劳器、一般完毕TCP衔接,并开释当前实例所运用的SmtpClient类的一切资本
Dispose(Boolean)
将一条QUIT音讯发送到SMTP效劳器、一般完毕TCP衔接时,开释当前实例所运用的SmtpClient类的一切资本,并可根据需要开释托管资本
Equals(Object)
肯定指定的对象是不是即是当前对象
Finalize()
在垃圾接纳机制将接纳某一对象前许可该对象尝试开释资本并实行其他清算操纵
GetHashCode()
作为默许的哈希函数
GetType()
猎取当前实例的Type
MemberwiseClone()
建立当前Object的浅表副本
OnSendCompleted(AsyncCompletedEventArgs)
激发SendComplete事宜
Send(MailMessage)
将指定的音讯发送到SMTP效劳器以举行通报
Send(String, String, String, String)
将指定的电子邮件发送到SMTP效劳器举行通报。邮件发件人、收件人、主题和音讯正文运用指定String对象
SendAsync(MailMessage, Object)
将指定的电子邮件发送到 SMTP 效劳器以举行通报。 此要领不会阻挠挪用线程,并许可挪用方将对象通报给该操纵完成时挪用的要领
SendAsync(String, String, String, String, Object)
将一封电子邮件发送到 SMTP 效劳器以举行通报。 邮件发件人、 收件人、 主题和音讯正文运用指定 String 对象。 此要领不会阻挠挪用线程,并许可挪用方将对象通报给该操纵完成时挪用的要领。
SendAsyncCancel()
作废异步操纵以发送电子邮件
SendMailAsync(MailMessage)
将指定的音讯发送到 SMTP 效劳器以举行异步操纵的情势通报。
SendMailAsync(String, String, String, String)
将指定的音讯发送到 SMTP 效劳器以便以异步操纵的情势托付。 。 邮件发件人、 收件人、 主题和音讯正文运用指定 String 对象。
ToString()
返回示意当前对象的字符串。(继承自 Object。)
事宜
SendCompleted
当异步电子邮件发送操纵完成时发作
备注
下表中所示的类用于构建运用可发送的电子邮件 SmtpClient。
Attachment类
示意文件附件,此类许可您将文件、流、或文本附加到电子邮件
MailAddress类
示意发件人和收件人的电子邮件地点
MailMessage类
示意一封电子邮件
组织并发送一封电子邮件运用 SmtpClient, ,您必需指定以下信息︰
用于发送电子邮件的 SMTP 主机效劳器。
关于身份验证,假如 SMTP 效劳器所需的凭证。
发件人电子邮件地点。
电子邮件地点或收件人的地点。
音讯内容。
若要包含运用电子邮件附件,起首建立附件运用 Attachment 类,然后再增加到音讯经由过程 MailMessage.Attachments 属性。 详细取决于运用收件人和附件的文件范例的电子邮件的读取器的情况下,某些收件人不可能能够读取附件。 关于不能坚持其原始花样显现的附件的客户端,您能够经由过程指定替换视图 MailMessage.AlternateViews 属性。
能够运用该应用顺序或计算机配置文件来指定用于一切的默许主机、 端口和凭证值 SmtpClient 对象。
若要在守候传输到 SMTP 效劳器的电子邮件时发送电子邮件和块,运用一个同步 Send 要领。 若要许可顺序的主线程继承实行传输电子邮件时,运用异步之一 SendAsync 要领。 SendCompleted 激发事宜时 SendAsync 操纵完成。 若要吸收此事宜,必需增加 SendCompletedEventHandler 托付给 SendCompleted。 SendCompletedEventHandler 托付必需援用的回调要领,用于处置惩罚关照的 SendCompleted 事宜。 若要作废异步电子邮件传输,运用 SendAsyncCancel 要领。
邮件发送界面重要代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;using System.Windows.Forms; using System.IO;using System.Net;using System.Net.Mail; namespace SendEmail{ public partial class Form3 : Form{ string severaddress;string mailuser;string userpwd;public Form3(){ InitializeComponent(); } private void button1_Click(object sender, EventArgs e){ Form6 form = new Form6(); form.SendParaHandler +=new Form6.SendPara(reload); //事宜的挂接form.Show(); } public void reload(){ StreamReader read = new StreamReader(@"fajianren.asdf"); severaddress = read.ReadLine(); mailuser = read.ReadLine(); userpwd = read.ReadLine(); read.Close();} private void Form3_Load(object sender, EventArgs e){ reload();} public bool sendmail(string mailfrom,string mailto,string mailsubject,string mailbody){ MailAddress from = new MailAddress(mailfrom); MailMessage message = new MailMessage(); try{message.From = from; message.To.Add(mailto); message.Subject = mailsubject; message.Body = mailbody; message.Priority = MailPriority.Normal; SmtpClient smtp = new SmtpClient(); smtp.Host = severaddress; smtp.UseDefaultCredentials = false; smtp.EnableSsl = true; smtp.Credentials = new NetworkCredential(mailuser,userpwd); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(message); } catch(Exception e){ return false; } return true; } private void button2_Click(object sender, EventArgs e){ string mailfrom = mailuser; string mailto = textBox1.Text; string mailsubject = textBox2.Text; string mailbody = textBox3.Text; if (sendmail(mailfrom, mailto, mailsubject, mailbody)){ MessageBox.Show("邮件发送胜利"); } else{ MessageBox.Show("邮件发送失利"); } } } }
设置发件人信息界面重要代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace SendEmail { public partial class Form 6 : Form {public Form6(){InitializeComponent(); } private void button1_Click(object sender, EventArgs e){ Write(); } //加载信息 private void Form6_Load(object sender, EventArgs e){ StreamReader read = new StreamReader(@"fajianren.asdf"); textBox1.Text = read.ReadLine(); textBox2.Text = read.ReadLine(); textBox3.Text = read.ReadLine(); read.Close(); } //写入信息 public void Write(){ StreamWriter write = new StreamWriter(@"fajianren.asdf"); write.WriteLine(textBox1.Text); write.WriteLine(textBox2.Text); write.WriteLine(textBox3.Text); write.Close(); } public delegate void SendPara(); //定义托付 public event SendPara SendParaHandler; //定义事宜 private void button2_Click(object sender, EventArgs e){ SendParaHandler.Invoke(); Write(); this.Close(); } } }
以上就是C#拾遗之SmtpClient类的内容,更多相关内容请关注ki4网(www.ki4.cn)!