C#发邮件主要涉及两个类:
- MailMessage:邮件内容(收发人地址,内容,标题等等)
- SmtpClient:邮件客户端(服务器,端口号,发送方式,发送动作,传入参数是MailMessage)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MailTest;
using System.Net;
using System.Net.Mail;
namespace MailTest
{
public class Program
{
static void Main(string[] args)
{
MailMessage msg = new MailMessage();//邮件信息类
msg.From = new MailAddress("xxx@xxx.com", "test", Encoding.UTF8);//实际发件人,显示发件人,编码
msg.To.Add("xxx@xxx.com");//收件人,这里可以对字符串处理,循环add收件人
msg.CC.Add("xxx@xxx.com");//抄送人,同上
msg.Subject = "test123";//邮件主题
msg.BodyEncoding = Encoding.UTF8;//邮件内容编码
msg.SubjectEncoding = Encoding.UTF8;//邮件主题编码
msg.IsBodyHtml = false;//是否是html格式,如果是,内容可以写成html样式
//msg.IsBodyHtml = true;//是否是html格式,如果是,内容可以写成html样式
//string filePath