moss中利用传出电子邮件设置发送email

      以下方法结合System.Net.Mail命名空间下的smtp类以及moss中传出电子邮件设置进行邮件的发送.
1.发送邮件主方法

public bool SendMail(string fromAdd, string[] toAdd, string subject, string body, string[] fileName)
{
            MailMessage mailMessage 
= new MailMessage();
            mailMessage.From 
= new MailAddress(fromAdd);
            
for (int i = 0; i < toAdd.Length; i++)
            {
                
if (toAdd[i].Trim() != string.Empty)
                {
                    mailMessage.To.Add(
new MailAddress(toAdd[i]));
                }
            }
            mailMessage.Priority 
= MailPriority.High;
            mailMessage.IsBodyHtml 
= true;
            mailMessage.Subject 
= subject;
            mailMessage.Body 
= body;

            
if (fileName != null)
            {
                
for (int i = 0; i < fileName.Length; i++
                {
                    
string filePath = HttpContext.Current.Server.MapPath("~/wpresources/OAContactsManage/upload/"+ fileName[i].ToString();
                    mailMessage.Attachments.Add(
new Attachment(filePath));
                }
            }
            
try
            {
                  //通过对象模型取得moss配置的smtp服务器
                SPOutboundMailServiceInstance smtpServer 
= SPContext.Current.Site.WebApplication.OutboundMailServiceInstance;                
                System.Net.Mail.SmtpClient smtp 
= new System.Net.Mail.SmtpClient(smtpServer.Server.Address);                
                smtp.Send(mailMessage);
            }
            
catch (Exception ex)
            {
                
return false;
            }
            
finally
            {
                
foreach (Attachment item in mailMessage.Attachments)
                {
                    item.Dispose();   //一定要释放该对象,否则无法删除附件
                }
                DeleteFile(fileName);    
//不论邮件是否发送成功,删除服务器上临时附件                
            }
            
return true;
        }

2.邮件发送完毕后删除临时文件夹中的附件
public   void  DeleteFile( string [] fileName)
{
            
try
            {
                
// 下面两种方式都可以达到删除文件的目的
                
// string path = string.Empty;
                
// FileInfo fi = null;
                
// for (int i = 0; i < fileName.Length; i++)
                
// {
                
//     if (fileName[i].Trim() != "")
                
//     {
                
//         path = HttpContext.Current.Server.MapPath("~/wpresources/OAContactsManage/upload/") + fileName[i].ToString();
                
//         fi = new FileInfo(path);
                
//         if (fi.Exists)
                
//         {                            
                
//             fi.Delete();
                
//         }
                
//     }
                
// }

                
for  ( int  i  =   0 ; i  <  fileName.Length; i ++ )
                {
                    
if  ( ! fileName[i].Equals( string .Empty))
                    {
                        
string  path  =  HttpContext.Current.Server.MapPath( " / " +   " \\wpresources\\OAContactsManage\\upload\\ "   +  fileName[i].ToString();
                        
if  (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                    }
                }
            }
            
catch
            {
                
            }    

你可能感兴趣的:(email)