发送email
主要的代码如下:
using System.Web.Mail;
MailMessage myMail = new MailMessage();
myMail.To = "ast03@mails.tsinghua.edu.cn"; //接受方的电子邮件地址
myMail.From = "youyouyou@mails.tsinghua.edu.cn"; //发送方的电子邮件地址
myMail.BodyFormat = MailFormat.Text; // 邮件的正文编码方式,可以选text或者html
myMail.Priority = MailPriority.Normal; //邮件的优先级
myMail.Subject = "hello.testing!!!"; //邮件的标题
myMail.Body = "just for fun ,never mind!!!!"; //邮件的正文内容
SmtpMail.Send(myMail);
需要引用system.web.mail里的类来实现.其中MailMessage这个是电子邮件的主要类. 最后,利用SmtpMail这个类里的send方法来发送邮件.(请启动smtp服务)
二、vs2005
引用 System.Net.Mail命名空间,安全性得到了增强
public static void SendWebMailAndAttach(string server)
{
string file = "e:\inetpub\wwwroot\Test2005All\TestXML\testXML.xml";
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("Test@126.com", "Test@126.com", "text message for you.", "Test Title");
System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
message.Attachments.Add(data);
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(server);
client.Credentials = new NetworkCredential("用户名", "密码");
client.Send(message);
data.Dispose();
}
{
string file = "e:\inetpub\wwwroot\Test2005All\TestXML\testXML.xml";
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("Test@126.com", "Test@126.com", "text message for you.", "Test Title");
System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(file, System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
message.Attachments.Add(data);
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(server);
client.Credentials = new NetworkCredential("用户名", "密码");
client.Send(message);
data.Dispose();
}