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
            {
                
            }    

转载于:https://www.cnblogs.com/py891021/archive/2009/09/02/1558541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值