C# 发送邮件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SendEmailExam
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
txtContent.Text = "<h1>Hello</h1>";
txtTo.Text = "xxx@163.com";
txtSubject.Text = "Hello";
txtCC.Text = "xxx@126.com;xxx@163.com";
}
private void btnSend_Click(object sender, EventArgs e)
{
SmtpClient sc = new SmtpClient();
NetworkCredential cred = new NetworkCredential("admin", "admin");
sc.Credentials = cred;
sc.Host = "smtp.163.com";
MailMessage mm = new MailMessage();
mm.From = new MailAddress("xxx@163.com", "xxx");
mm.Sender = new MailAddress("xxx@163.com","xxx");
string[] tos = txtTo.Text.Split(';');
foreach (string item in tos)
{
mm.To.Add(new MailAddress(item));
}
string[] ccs = txtCC.Text.Split(';');
foreach (string item in ccs)
{
mm.CC.Add(new MailAddress(item));
}
mm.Subject = txtSubject.Text;
mm.Body = txtContent.Text;
mm.IsBodyHtml = true;
mm.Priority = MailPriority.High;
mm.Attachments.Add(new Attachment(txtAccessory.Text));
sc.Send(mm);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
txtAccessory.Text = ofd.FileName;
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SendEmailExam
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
txtContent.Text = "<h1>Hello</h1>";
txtTo.Text = "xxx@163.com";
txtSubject.Text = "Hello";
txtCC.Text = "xxx@126.com;xxx@163.com";
}
private void btnSend_Click(object sender, EventArgs e)
{
SmtpClient sc = new SmtpClient();
NetworkCredential cred = new NetworkCredential("admin", "admin");
sc.Credentials = cred;
sc.Host = "smtp.163.com";
MailMessage mm = new MailMessage();
mm.From = new MailAddress("xxx@163.com", "xxx");
mm.Sender = new MailAddress("xxx@163.com","xxx");
string[] tos = txtTo.Text.Split(';');
foreach (string item in tos)
{
mm.To.Add(new MailAddress(item));
}
string[] ccs = txtCC.Text.Split(';');
foreach (string item in ccs)
{
mm.CC.Add(new MailAddress(item));
}
mm.Subject = txtSubject.Text;
mm.Body = txtContent.Text;
mm.IsBodyHtml = true;
mm.Priority = MailPriority.High;
mm.Attachments.Add(new Attachment(txtAccessory.Text));
sc.Send(mm);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
txtAccessory.Text = ofd.FileName;
}
}
}
}