发送邮件事例
protected void btnSend_Click(object sender, EventArgs e)
{
string rcvAddr = txtMailAddr.Text.Trim();
if (rcvAddr.Length > 0)
{
//邮件信息
MailMessage mail = new MailMessage();
mail.From = new MailAddress("Socansoft<liaoyongjun@gmail.com>");
mail.Subject = "测试邮件";
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.Default;
mail.Body = "你好,这是自动发送的,请勿回复!";
string[] rcvs = rcvAddr.Split(new char[] { ',' });
foreach (string rcv in rcvs)
{
mail.To.Add(new MailAddress(rcv));
}
//SMTP服务器
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
//需要验证,用户名和密码要正确
client.Credentials = new System.Net.NetworkCredential("liaoyongjun@gmail.com", "123456");
//接收邮件的邮箱
try
{
client.Send(mail);
Response.Write("邮件成功发送到" + mail.To.ToString() + "<br>");
}
catch (Exception ex)
{
Response.Write(ex.Message + "<br>");
}
}
else
{
Response.Write("联系邮箱不能为空");
}
}
{
string rcvAddr = txtMailAddr.Text.Trim();
if (rcvAddr.Length > 0)
{
//邮件信息
MailMessage mail = new MailMessage();
mail.From = new MailAddress("Socansoft<liaoyongjun@gmail.com>");
mail.Subject = "测试邮件";
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.Default;
mail.Body = "你好,这是自动发送的,请勿回复!";
string[] rcvs = rcvAddr.Split(new char[] { ',' });
foreach (string rcv in rcvs)
{
mail.To.Add(new MailAddress(rcv));
}
//SMTP服务器
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
//需要验证,用户名和密码要正确
client.Credentials = new System.Net.NetworkCredential("liaoyongjun@gmail.com", "123456");
//接收邮件的邮箱
try
{
client.Send(mail);
Response.Write("邮件成功发送到" + mail.To.ToString() + "<br>");
}
catch (Exception ex)
{
Response.Write(ex.Message + "<br>");
}
}
else
{
Response.Write("联系邮箱不能为空");
}
}