今天做个发邮件的小例子,虽然没什么技术含量,拿出来分享一下。
C#发送邮件
MailMessage mm = new MailMessage(); mm.BodyFormat = System.Web.Mail.MailFormat.Html; mm.From = "这里填写发件人的邮箱 如:(***@163.com)"; mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "这里填写发件人的邮箱的账号,@前的内容"); mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "这里填写发件人邮箱的密码"); mm.To = "这里则是填写收件人的邮箱"; mm.Subject = "这里填写邮件的标题"; mm.Body = "这里填写邮件内容"; System.Web.Mail.SmtpMail.SmtpServer = "smtp.163.com"; //发件服务器名称 try { System.Web.Mail.SmtpMail.Send(mm); //发送邮件 } catch (System.Exception err) { Response.Write(err.Message); }
Asp.NET 发送邮件
使用Asp.Net 发送电子邮件 使用的 System.Net.Mail 命名空间
MailAddress from = new MailAddress("发件人的电子邮件地址");
MailAddress to = new MailAddress("收件人的电子邮件地址");
MailMessage message = new MailMessage(from, to);
message.Subject = "邮件的主题";
message.Body = "邮件的内容";
SmtpClient smtp = new SmtpClient("电子邮件服务器端的名字或IP地址");
smtp.Credentials = new System.Net.NetworkCredential("发件人的电子邮件地址", "发件人电子邮件的密码");
try
{
//发送邮件
smtp.Send(message);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}