这两个方法很容易理解,只实现了最基本的功能,如果需要可以查看源代码获取更多信息。
邮件发送
方法一:使用System.Web.Mail命名空间(此方法我测试没有成功过)
#region 发送邮件:此方法失败
protected void SendFailed()
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.From = "test@ gmail.com";
mail.To = " test@ gmail.com ";
mail.Subject = "For Test";
mail.Priority = System.Web.Mail.MailPriority.Normal;
mail.BodyEncoding = Encoding.Default;
mail.BodyFormat = MailFormat.Html;
mail.Body = "this is a Email!";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "test"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "****"); //set your password here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "587");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
SmtpMail.SmtpServer = "smtp.gmail.com";
SmtpMail.Send(mail);
}
#endregion
方法二:使用System.Net.Mail命名空间(此方法测试成功)我使用的gmail的邮箱,以及他提供免费smtp服务,之前试了好几个邮箱都不成功。Gmail的smtp服务必须经过ssl加密,才可以验证成功。
#region 发送邮件:此方法可行
protected void SendSuccess()
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress("test@gmail.com", "someone");//必须是提供smtp服务的邮件服务器
message.To.Add(new MailAddress("test@yahoo.com.cn"));
message.Subject = "测试邮件" ;
message.CC.Add(new MailAddress("test@126.com"));
message.Bcc.Add(new MailAddress("test@126.com"));
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Body = "邮件发送测试";
message.Priority = System.Net.Mail.MailPriority.High;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); // 587;//Gmail使用的端口
client.Credentials = new System.Net.NetworkCredential("test@gmail.com", "password"); //这里是申请的邮箱和密码
client.EnableSsl = true; //必须经过ssl加密
try
{
client.Send(message);
Response.Write("邮件已经成功发送到" + message.To.ToString() + "
");
}
catch (Exception ee)
{
Response.Write(ee.Message + "
" /* + ee.InnerException.Message*/ );
}
}
#endregion
邮件接收
我使用的是LumiSoft.Net这个开源的项目,也是从一个网友哪里看到的下载地址,然后自己看了下代码,写了个简单的接收方法。首先将代码中relrease目录下的dll文件引用到项目中。
using LumiSoft.Net.POP3.Client;
using LumiSoft.Net.Mail;
……
public IList ReceiveMail()
{
IList mailList = new List();
using (POP3_Client client = new POP3_Client())
{
client.Connect("pop.gmail.com",995,true);
client.Authenticate("zw.seaman", "zw_seaman", false);
POP3_ClientMessageCollection coll = client.Messages;
for (int i = 0; i < coll.Count; i++)
{
POP3_ClientMessage message = coll[i];
Mail_Message mm = Mail_Message.ParseFromByte(coll[i].MessageToByte());
mailList.Add(mm);
}
}
return mailList;
}
protected void Page_Load(object sender, EventArgs e)
{
IList mailList = new ZMail.Mail().ReceiveMail();
foreach (Mail_Message mail in mailList)
{
StringBuilder sb = new StringBuilder();
sb.Append(mail.From.ToString()).Append(" 发送给 ");
sb.Append(mail.To.ToString()).Append("
") ;
sb.Append(mail.Subject).Append("
");
sb.Append(mail.BodyHtmlText).Append("
");
Response.Write(sb.ToString());
}
}
这两个方法很容易理解,只实现了最基本的功能,如果需要可以查看源代码获取更多信息。