//命名空间
using System.Net.Mail;
//方法
public int Email(string title, string content, string address)
{
string name = emailname.Text;
string pwd = emailpwd.Text;
MailAddress from = new MailAddress(name);//自己的邮箱名
MailAddress to = new MailAddress(address);
MailMessage message = new MailMessage(from, to);
message.Subject = title;
message.IsBodyHtml = true;//邮件是否是HTML格式
message.Body = content;
message.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
SmtpClient client = new SmtpClient("smtp.163.com");
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential(name, pwd);//分别为自己的邮箱名和密码
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
return 1;
}
//调用
protected void Button1_Click(object sender, EventArgs e)
{
string name = emailname.Text;
string pwd = emailpwd.Text;
string title = txttitle.Text;
string content = txtcontent.Text;
string toname = textTo.Text;
Email(title, content, toname);//邮件标题,内容,收件人
}
注:发件人使用的只能是163邮箱