Code
try
{
System.Net.Mail.MailMessage mailObj = new System.Net.Mail.MailMessage();
mailObj.IsBodyHtml = true;
mailObj.Subject = "邮件标题";
mailObj.Body = "邮件正文";
mailObj.To.Add("收信人地址");
System.Net.Mail.SmtpClient SmtpMail = new SmtpClient("smtp.gmail.com");
mailObj.From = new MailAddress("发信人@gmail.com", "你的名字", System.Text.Encoding.UTF8);
SmtpMail.Credentials = new System.Net.NetworkCredential("发信人@gmail.com", "密码");
//gmail 专有配置 开始
SmtpMail.Port = 587;
SmtpMail.EnableSsl = true;
//gmail 专有配置 结束
SmtpMail.Send(mailObj);
Response.Write("邮件发送成功!");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
当然,通过在web.config中配置一下,更简单
Code
<system.net>
<mailSettings>
<smtp>
<network host ="smtp.gmail.com" userName ="发信人@gmail.com" port ="587" password ="*****"/>
</smtp>
</mailSettings>
</system.net>
客户端代码如下:
Code
SmtpClient client = new SmtpClient();
MailMessage mm = new MailMessage("发件人@gamil.com", "收件人@qq.com", "Ttile", "body");
client.Send(mm);
三行代码搞定!(高手见笑了!)