using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

 
public class Mail
    {
        
public void sendMail(string[] to, string[] cc, string subject, string body,string attachfile)
        {
            MailMessage mail 
= new MailMessage();
            mail.From 
= new MailAddress("abc@def.com");
            
if (to != null)
                
foreach (string _to in to)
                {
                    
if (_to != "")
                    {
                        mail.To.Add(_to);
                    }
                }

            
if (cc != null)
                
foreach (string _cc in cc)
                {
                    
if (_cc != "")
                    {
                        mail.CC.Add(_cc);
                    }
                }
            
//""前加@,""内的内容就不用改了
            attachFile("E:\\test\\class\\Study\\studyWebApplication\\BLL\\office\\picture\\head.jpg""head"0,true,ref mail);
            attachFile(
@"E:\test\class\Study\studyWebApplication\BLL\office\picture\taile.jpg""taile"1trueref mail);
            
if (attachfile != "")
            {
                attachFile(attachfile, 
"file"2falseref mail);
            }
            mail.Subject 
= subject;
            
//mail以html的格式发送
            mail.IsBodyHtml = true;
            mail.Body 
= string.Format(@"<table><tr><td><img src=""{0}""/></td></tr><tr><td valign=top style=""height:200px"">" + body + @"</td></tr><tr><td><img src=""{1}""/></td></tr></table>",
                
"cid:" + mail.Attachments[0].ContentId, "cid:" + mail.Attachments[1].ContentId);
            var s 
= new SmtpClient();
            s.Port 
= 25;
            
//邮件服务器地址
            s.Host = "111.111.111.111";
            s.Send(mail);
        }
        
//ref 传引用,如对象等都要用这个形式
        
//ContentDisposition.Inline用来说明是以内联还是附件的方式发送
        private void attachFile(string attachfile, string cid, int i,bool disattach,ref MailMessage mail)
        {
            Attachment attachFile 
= new Attachment(attachfile);
            mail.Attachments.Add(attachFile);
            mail.Attachments[i].ContentId 
= cid;
            mail.Attachments[i].ContentDisposition.Inline 
= disattach;
            mail.Attachments[i].NameEncoding 
= mail.SubjectEncoding = mail.BodyEncoding = Encoding.UTF8;
            
        }
    }
posted on 2009-07-15 15:24  长风一剑  阅读(883)  评论(0编辑  收藏  举报