发邮件与收邮件
1.用VS2003自带SMTP发送邮件,
代码:
2.采用JMail来发送邮件和接受邮件
下载JMAIL,并安装:JMail
重新编绎jmail.dll,见http://tintown.cnblogs.com/archive/2006/01/26/323575.html
发送邮件代码:
代码:
1MailMessage objmail=new MailMessage ();
2 MailAttachment objmailattach=new MailAttachment (accessorymail.Value .ToString ());
3//附件
4 objmail.From =sourcemail.Value .ToString ();
5 objmail.To =targetmail.Value .ToString ();
6 objmail.Subject =subjectmail.Value .ToString ();
7 objmail.Body =contentmail.Value .ToString ();
8 objmail.Attachments .Add (objmailattach);
9
10 objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
11 objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "userid");//这里填写你邮箱的用户名
12 objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "pwd");//你邮箱的密码
13 SmtpMail.SmtpServer = "smtp.163.com";
14//如是局域网内发邮件,则把此设为内部邮件服务器的IP
15
16 try
17 {
18 SmtpMail.Send (objmail);
19 this.Response .Write ("ok");
20 }
21 catch(Exception ex)
22 {
23 this.Response .Write ("error"+ex.Message .ToString ()+ex.Source .ToString ());
24 }
2 MailAttachment objmailattach=new MailAttachment (accessorymail.Value .ToString ());
3//附件
4 objmail.From =sourcemail.Value .ToString ();
5 objmail.To =targetmail.Value .ToString ();
6 objmail.Subject =subjectmail.Value .ToString ();
7 objmail.Body =contentmail.Value .ToString ();
8 objmail.Attachments .Add (objmailattach);
9
10 objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
11 objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "userid");//这里填写你邮箱的用户名
12 objmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "pwd");//你邮箱的密码
13 SmtpMail.SmtpServer = "smtp.163.com";
14//如是局域网内发邮件,则把此设为内部邮件服务器的IP
15
16 try
17 {
18 SmtpMail.Send (objmail);
19 this.Response .Write ("ok");
20 }
21 catch(Exception ex)
22 {
23 this.Response .Write ("error"+ex.Message .ToString ()+ex.Source .ToString ());
24 }
2.采用JMail来发送邮件和接受邮件
下载JMAIL,并安装:JMail
重新编绎jmail.dll,见http://tintown.cnblogs.com/archive/2006/01/26/323575.html
发送邮件代码:
1Message jmail=new Message ();
2 jmail.AddRecipient ("接受邮箱",null,null);
3 jmail.From ="发送邮箱";
4 jmail.MailServerUserName ="发送邮箱用户名";
5 jmail.MailServerPassWord ="发送邮箱密码";
6 jmail.Subject ="test";
7 jmail.Body ="test jmail"+System.DateTime .Now.ToString (); jmail.Send ("smtp.163.com",false);
8 jmail.Close ();
9 this.Response .Write ("ok");
接受邮件代码:2 jmail.AddRecipient ("接受邮箱",null,null);
3 jmail.From ="发送邮箱";
4 jmail.MailServerUserName ="发送邮箱用户名";
5 jmail.MailServerPassWord ="发送邮箱密码";
6 jmail.Subject ="test";
7 jmail.Body ="test jmail"+System.DateTime .Now.ToString (); jmail.Send ("smtp.163.com",false);
8 jmail.Close ();
9 this.Response .Write ("ok");
1POP3 pop=new POP3Class ();
2 pop.Connect ("用户名","密码","pop3.163.com",110);
3 StringWriter sw=new StringWriter ();
4 HtmlTextWriter ht=new HtmlTextWriter (sw);
5 ht.RenderBeginTag ("table");
6 for(int i=1;i<pop.Messages .Count;i++)
7 {
8 ht.RenderBeginTag ("tr");
9 ht.RenderBeginTag ("td");
10 ht.WriteLine (i.ToString ());
11 ht.RenderEndTag ();
12 ht.RenderBeginTag ("td");
13 ht.WriteLine (pop.DownloadSingleMessage (i).Subject .ToString ());
14 ht.RenderEndTag ();
15 ht.RenderBeginTag ("td");
16 ht.WriteLine (pop.DownloadSingleMessage (i).From .ToString ());
17 ht.RenderEndTag ();
18 ht.RenderBeginTag ("td");
19 ht.WriteLine (pop.DownloadSingleMessage (i).Date .ToString ());
20 ht.RenderEndTag ();
21 ht.RenderBeginTag ("td");
22 ht.WriteLine (pop.DownloadSingleMessage (i).DeferredDelivery .ToString ());
23 ht.RenderEndTag ();
24 ht.RenderEndTag ();
25 }
26 ht.RenderEndTag ();
27 pop.Disconnect ();
28 this.Response .Write (sw.ToString ());
2 pop.Connect ("用户名","密码","pop3.163.com",110);
3 StringWriter sw=new StringWriter ();
4 HtmlTextWriter ht=new HtmlTextWriter (sw);
5 ht.RenderBeginTag ("table");
6 for(int i=1;i<pop.Messages .Count;i++)
7 {
8 ht.RenderBeginTag ("tr");
9 ht.RenderBeginTag ("td");
10 ht.WriteLine (i.ToString ());
11 ht.RenderEndTag ();
12 ht.RenderBeginTag ("td");
13 ht.WriteLine (pop.DownloadSingleMessage (i).Subject .ToString ());
14 ht.RenderEndTag ();
15 ht.RenderBeginTag ("td");
16 ht.WriteLine (pop.DownloadSingleMessage (i).From .ToString ());
17 ht.RenderEndTag ();
18 ht.RenderBeginTag ("td");
19 ht.WriteLine (pop.DownloadSingleMessage (i).Date .ToString ());
20 ht.RenderEndTag ();
21 ht.RenderBeginTag ("td");
22 ht.WriteLine (pop.DownloadSingleMessage (i).DeferredDelivery .ToString ());
23 ht.RenderEndTag ();
24 ht.RenderEndTag ();
25 }
26 ht.RenderEndTag ();
27 pop.Disconnect ();
28 this.Response .Write (sw.ToString ());