C# Email发送邮件

本文实例为大家分享了C# Email发送邮件的具体代码,供大家参考,具体内容如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinUI.BAS
{
    public partial class sendMail : Form
    {
        public sendMail()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //回执地址
            var Receipt = "****@163.com";

            //实例化两个必要的
            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient();

            //发送邮箱地址
            mail.From = new MailAddress("****@163.com");

            //收件人(可以群发)
            mail.To.Add(new MailAddress("****@163.com"));

            //是否以HTML格式发送
            mail.IsBodyHtml = true;
            //主题的编码格式
            mail.SubjectEncoding = Encoding.UTF8;
            //邮件的标题
            mail.Subject = "测试一下发件的标题";
            //内容的编码格式
            mail.BodyEncoding = Encoding.UTF8;
            //邮件的优先级
            mail.Priority = MailPriority.Normal;
            //发送内容,带一个图片标签,用于对方打开之后,回发你填写的地址信息
            mail.Body = @"获取打开邮件的用户IP,图片由服务器自动生成:<img src='" + Receipt + "'>";
            //收件人可以在邮件里面
            mail.Headers.Add("Disposition-Notification-To", "回执信息");

            //发件邮箱的服务器地址
            smtp.Host = "smtp.163.com";
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Timeout = 1000000;
            //是否为SSL加密
            smtp.EnableSsl = true;
            //设置端口,如果不设置的话,默认端口为25
            smtp.Port = 25;
            smtp.UseDefaultCredentials = true;
            //验证发件人的凭据
            smtp.Credentials = new System.Net.NetworkCredential("****@163.com", "这里的密码是授权码");

            try
            {
                //发送邮件
                smtp.Send(mail);
                MessageBox.Show("发送成功");
            }
            catch (Exception e1)
            {
                MessageBox.Show("发送失败:" + e1);
            }
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/qq_37545120/article/details/81171610