首页 > 代码库 > c#发邮件详解

c#发邮件详解

//发送人
var from = this.txtSendMailFrom.Text.Trim();
//收件人
var to = this.txtMailSendTo.Text.Trim();
//标题
var title = this.txtSendTitle.Text.Trim();
//内容
var info = this.txtInfo.Text.Trim();
var msg = new MailMessage();
//正文或内容
msg.Body = info;
//发送网站
//msg.Body = new System.Net.WebClient() { Encoding = Encoding.UTF8 }.DownloadString("http://www.baidu.com");
//格式
msg.BodyEncoding = Encoding.UTF8;
//发送人地址
msg.From = new MailAddress(from);
//指定可以发送静态页面的代码
msg.IsBodyHtml = false;
msg.Subject = title;
//发送附件,去重复
foreach (var item in files.Distinct())
{
msg.Attachments.Add(new Attachment(item));
}
//多发
var tolist = to.Split(‘;‘);
foreach (var item in tolist)
{
msg.To.Add(new MailAddress(item));
}
//单发
//msg.To.Add(new MailAddress(to));
//默认端口是25,发送邮件的协议
try
{
//设置邮箱端口,pop3端口:110, smtp端口是:25
SmtpClient sc = new SmtpClient();
//SmtpClient client = new SmtpClient("smtp.exmail.qq.com");腾讯企业邮箱
//邮箱设置:POP3发 SMTP收
//sc.DeliveryMethod = SmtpDeliveryMethod.Network;//通过网络发送到stmp邮件服务器
sc.Host = "smtp.163.com";
sc.Port = 25;
//sc.EnableSsl = true;//SMTP 服务器要求安全连接需要设置此属性
//sc.Timeout = 999;//超时时间
//(发件人,密码)
sc.Credentials = new System.Net.NetworkCredential("duling12345a@163.com", "duling12345aA");
sc.Send(msg);
MessageBox.Show("发送成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

c#发邮件详解