string file = Server.MapPath("email_test.txt");
System.Web.Mail.MailMessage mes = new System.Web.Mail.MailMessage();
mes.To = "發送郵箱";
mes.From = "接收郵箱";
MailAttachment mailAttachment = new MailAttachment(file);
mes.Attachments.Add(mailAttachment);
mes.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//基本許可權
mes.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "發送郵箱名");
mes.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "發送郵箱密碼");
mes.BodyEncoding = System.Text.Encoding.UTF8;
mes.Body = "郵件 本文 email content";
//以下可以選擇文本轉譯格式
mes.BodyFormat = MailFormat.Html;
mes.Subject = "郵件發送測試";
SmtpMail.SmtpServer = "smtp.163.com";
SmtpMail.Send(mes);
Response.Write("發送成功!");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
//以下是利伺服器pop3協議發送郵件
try
{
string file = Server.MapPath("email_test.txt");
MailAddress from = new MailAddress("zhengpengfei0791@163.com");//執行個體一個郵件發送地址
MailAddress to = new MailAddress("solomon_msn@live.cn");//執行個體一個郵件接收地址
MailMessage mes = new MailMessage(from, to);
Attachment myAttachment = new Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
mes.Attachments.Add(myAttachment);
mes.IsBodyHtml = true;
mes.Subject = "郵件測試";
mes.Subject = "郵件內文 Email Content<img src='";
SmtpClient client = new SmtpClient();
client.Host = Request.UserHostAddress;
client.Port = 25;
client.Credentials = new System.Net.NetworkCredential("toEmail", "test");
client.Send(mes);
Response.Write("郵件發送成功!");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}