SmtpClient 类

SmtpClient类

允许应用程序使用简单邮件传输协议 (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 Form6 : 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();
}
}
}

转载于:https://www.cnblogs.com/s-d-g/p/6077776.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值