C# 发送邮件 例子
C# 用 using System.Net.Mail 发送邮件
操作方法
- 01
先看看样子
- 02
代码如图所示 大家觉得有用 欢迎大家鼓励
- 03
源码如下
- 04
public partial class Form1 : Form { public string smtpService = "smtp.qq.com"; public string sendEmail = "965338545@qq.com"; public string sendpwd = "123456"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //确定smtp服务器地址 实例化一个Smtp客户端 SmtpClient smtpClient =new SmtpClient(); smtpClient.Host = smtpService; //smtpClient.Port = "";//qq邮箱可以不用端口 //构建发件地址和收件地址 MailAddress sendAddress=new MailAddress(sendEmail,"SP的我"); MailAddress receiverAddress=new MailAddress(textBox2.Text); //构造一个Email的Message对象 内容信息 MailMessage message = new MailMessage(sendAddress,receiverAddress); message.Subject = "邮件主题"+DateTime.Now; message.SubjectEncoding=Encoding.UTF8; message.Body = textBox1.Text; message.BodyEncoding=Encoding.UTF8; //设置邮件的信息 如登陆密码 账号 //邮件发送方式 通过网络发送到smtp服务器 smtpClient.DeliveryMethod=SmtpDeliveryMethod.Network; //如果服务器支持安全连接,则将安全连接设为true smtpClient.EnableSsl = true; try { smtpClient.UseDefaultCredentials = false; //发件用户登陆信息 NetworkCredential senderCredential = new NetworkCredential(sendEmail, sendpwd); smtpClient.Credentials = senderCredential; //发送邮件 smtpClient.Send(message); MessageBox.Show("发送成功!"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }