一、asp.net发送邮件
代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
public partial class email : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//新浪邮箱
SendSMTPEMail("smtp.sina.com", "邮箱", "密码", "123456@qq.com", "标题", "内容");
//网易126邮箱
//SendSMTPEMail("smtp.126.com", "邮箱", "密码", "123456@qq.com", "标题", "内容");
}
public void SendSMTPEMail(string strSmtpServer, string strFrom,
string strFromPass, string strto, string strSubject, string strBody)
{
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UsedefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
}
二、asp.net发送邮件,带附件邮件
asp.net发送邮件:
邮件附件不需要先上传到服务器再发送,直接从选择的路径中取到即可发送。
代码:
/// <summary>
/// 发送edm邮件,可以附加附件
/// </summary>
/// <param name="StrTo">收件人地址</param>
/// <param name="toDisplayName">收件人名稱</param>
/// <param name="strFrom">发件人地址</param>
/// <param name="fromDisplayName">发件人名稱</param>
/// <param name="StrBody">邮件內容</param>
/// <param name="strSubject">邮件主題</param>
/// <param name="path">附件路徑</param>
/// <returns>返回是否发送成功</returns>
public static bool SendEDMEmailBy47Server(string StrTo, string toDisplayName, string strFrom, string fromDisplayName, string StrBody, string strSubject,string path)
{
System.Net.Mail.MailAddress fromEmail = new System.Net.Mail.MailAddress(strFrom, fromDisplayName);//发件人的地址和发件人名称
System.Net.Mail.MailAddress toEmail = new System.Net.Mail.MailAddress(StrTo, toDisplayName);//收件人地址和收件人名称
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(fromEmail, toEmail);
mail.Body = StrBody;
mail.Subject = strSubject;
mail.IsBodyHtml = true;
mail.BodyEncoding = Encoding.UTF8;
mail.Priority = System.Net.Mail.MailPriority.Normal;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
smtpClient.Host = "...";//这里设置成你的smtp主机名或者IP地址
smtpClient.Port = 25;
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential("xxx", "xxx");//你的用户名和密码
//以下是邮件附件部分,path一般是从FileUpload控件中取~
FileInfo fi;
if (path != "")
{
fi = new FileInfo(path);
if (fi.Exists)
{
System.Net.Mail.Attachment firstatt = new System.Net.Mail.Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposion=new System.Net.Mime.ContentDisposition();
disposion.CreationDate=System.IO.File.GetCreationTime(path);
disposion.ModificationDate=System.IO.File.GetLastWriteTime(path);
disposion.ReadDate=System.IO.File.GetLastAccessTime(path);
mail.Attachments.Add(firstatt);
}
}
try
{
smtpClient.Send(mail);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}