.net发送带图片格式Email

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 " 1 true ref  mail);
            
if  (attachfile  !=   "" )
            {
                attachFile(attachfile, 
" file " 2 false ref  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;
            
        }
    }

转载于:https://www.cnblogs.com/liuweicfyj/archive/2009/07/15/1524190.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值