C#发送eMail的类


using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mail;

namespace BaseLib
{
    
public class SendMail
    
{
        
public void sendTxtMail(string from, string pass, string to, string subject, MailPriority priority, string body, string smtpServer, System.Collections.ArrayList files)
        
{
            MailMessage msg 
= new MailMessage();
            msg.From 
= from;
            msg.To 
= to;

            msg.Subject 
= subject;
            msg.Priority 
= priority;
            msg.BodyFormat 
= MailFormat.Text;
            msg.Body 
= body;
            msg.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate""1"); //basic authentication
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", from.Substring(0, from.IndexOf("@"))); //set your username here
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pass); //set your password here

            
for (int i = 0; i < files.Count; i++)
            
{
                
if (System.IO.File.Exists(files[i].ToString()))
                
{
                    msg.Attachments.Add(
new MailAttachment(files[i].ToString()));
                }


            }

            SmtpMail.SmtpServer 
= smtpServer;

            
try
            
{
                SmtpMail.Send(msg);
            }

            
catch(Exception ex)
            

                
            }


        }


        
public void sendHtmlMail(string from, string pass, string to, string subject, MailPriority priority, string body, string smtpServer, System.Collections.ArrayList files)
        
{
            MailMessage msg 
= new MailMessage();
            msg.From 
= from;
            msg.To 
= to;

            msg.Subject 
= subject;
            msg.Priority 
= priority;
            msg.BodyFormat 
= MailFormat.Html;
            msg.Body 
= body;
            msg.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate""1"); //basic authentication
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", from.Substring(0, from.IndexOf("@"))); //set your username here
            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pass); //set your password here

            
for (int i = 0; i < files.Count; i++)
            
{
                
if (System.IO.File.Exists(files[i].ToString()))
                
{
                    msg.Attachments.Add(
new MailAttachment(files[i].ToString()));
                }


            }

            SmtpMail.SmtpServer 
= smtpServer;

            SmtpMail.Send(msg);

        }

    }

}

posted on 2008-07-10 22:03  王丹小筑  阅读(339)  评论(0)    收藏  举报

导航