C#发送邮件代码,亲测可用。
using System;
using System.Net;
using System.Net.Mail;namespace MailSend
{class Program{static void Main(string[] args){try{MailAddress receiver = new MailAddress("666666666@qq.com");//666666666@qq.com 换成收件人的邮箱MailAddress sender = new MailAddress("111111111@163.com");//111111111@163.com 换成发件的邮箱MailMessage message = new MailMessage();message.From = sender;//发件人message.To.Add(receiver);//收件人//message.CC.Add(sender);//抄送人message.Subject = "Test Message";//标题message.Body = "Hello!";//内容message.IsBodyHtml = true;//是否支持内容为HTMLSmtpClient client = new SmtpClient();client.Host = "smtp.163.com";//这个是163邮箱的smtp,用什么邮箱发件就用相应的smtp//client.Port = 465;client.EnableSsl = true;//是否启用SSLclient.Timeout = 10000;//超时client.DeliveryMethod = SmtpDeliveryMethod.Network;client.UseDefaultCredentials = false;client.Credentials = new NetworkCredential("111111111@163.com", "QWEDFTWDVGHTYUT");//111111111@163.com 换成发件的邮箱 QWEDFTWDVGHTYUT换成密钥client.Send(message);}catch (Exception e){} }}
}
密钥的获取如下,开启IMAP和POP3之后会弹出一个提示窗口,窗口里就是密钥,一定要复制下来,它只显示一次。