C#编写发邮件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace _123456
{
    class Program
    {
        static void Main(string[] args)
        {
            
                string host = "smtp.163.com";// 邮件服务器smtp.163.com表示网易邮箱服务器    
                string userName = "17852031427@163.com";// 发送端账号   
                string password = "password";// 发送端密码(这个客户端重置后的密码)




                SmtpClient client = new SmtpClient();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式    
                client.Host = host;//邮件服务器
                client.UseDefaultCredentials = true;
                client.Credentials = new System.Net.NetworkCredential(userName, password);//用户名、密码

                //
                string strfrom = userName;
                string strto = "2512959622@qq.com";
                //string strcc = "2605625733@qq.com";//抄送


                string subject = "这是测试邮件标题5";//邮件的主题             
                string body = "测试邮件内容5";//发送的邮件正文  

                System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                msg.From = new MailAddress(strfrom, "xyf");
                msg.To.Add(strto);
                //msg.CC.Add(strcc);

                msg.Subject = subject;//邮件标题   
                msg.Body = body;//邮件内容   
                msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码   
                msg.IsBodyHtml = true;//是否是HTML邮件   
                msg.Priority = MailPriority.High;//邮件优先级   


                try
                {
                    client.Send(msg);
                    Console.WriteLine("发送成功");
                }
                catch (System.Net.Mail.SmtpException ex)
                {
                    Console.WriteLine(ex.Message, "发送邮件出错");
                }
            Console.ReadKey();
            
        }
    }
}

转自:https://www.cnblogs.com/xinyf/p/6294785.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值