发邮件并且带附件

int  __fastcall  TM::SendMail(const  char  *  cTo,  const  char  *  cCc,  const  char  *  cBcc,/ 
                                                          const  char*  cSubject,  const  char  *  cBody,  const  char*  cAttachList,/ 
                                                          char  *  cMsg) 
  { 
          int  iRet=0; 
          if(!SetupOk) 
                  return  -2; 
   
          IdMsgSend->Clear();    //清除,否则包含有上一条的信息 
   
          IdMsgSend->ContentType  =  ContentType; 
          IdMsgSend->From->Text  =  LocalMail; 
          if  (ReturnReciept) 
          {//{We  set  the  recipient  to  the  From  E-Mail  address  } 
                IdMsgSend->ReceiptRecipient->Text  =  IdMsgSend->From->Text; 
          } 
          else 
          {//  {indicate  that  there  is  no  receipt  recipiant} 
                IdMsgSend->ReceiptRecipient->Text  =  ""; 
          } 
   
          IdMsgSend->Recipients->EMailAddresses  =  cTo;  //{  To:  header  } 
          IdMsgSend->Subject  =  cSubject;  //{  Subject:  header  } 
          IdMsgSend->CCList->EMailAddresses  =  cCc;//  {CC} 
          IdMsgSend->BccList->EMailAddresses  =  cBcc;  //{BCC} 
   
          IdMsgSend->Priority  =  TIdMessagePriority(Priority);  //  {  Message  Priority  } 
          IdMsgSend->Body->Text  =  String(cBody); 
   
          if(strlen(cAttachList)) 
          { 
                  TStringList  *  sl=  new  TStringList; 
                  sl->Text=String(cAttachList); 
                  for(int  i=0;i <sl->Count;i++) 
                  { 
                          IdMsgSend->MessageParts->Add(); 
                          new  TIdAttachment(IdMsgSend->MessageParts,sl->Strings[i]); 
                  } 
                  delete  sl; 
          } 
          if(!SMTP->Connected()) 
          { 
                  try 
                  { 
                          SMTP->Connect(); 
                  } 
                  catch(Exception  &e) 
                  { 
                          strcpy(cMsg,"连接SMTP服务器失败!错误信息:"); 
                          strcat(cMsg,e.Message.c_str()); 
                          iRet  =  -1; 
                          return  iRet; 
                  } 
          } 
          if(SMTP->Connected()) 
          { 
                  try 
                  { 
                          SMTP->Send(IdMsgSend); 
                  } 
                  catch(Exception  &e) 
                  { 
                          strcpy(cMsg,e.Message.c_str()); 
                          iRet  =  -1; 
                  } 
          } 
          else 
          { 
                  strcpy(cMsg,"连接SMTP服务器失败!"); 
                  iRet  =  -1; 
          } 
          return  iRet; 
  } 
   
  //设置发送的SMTP属性 
  //参数:in:cHost,SMTP服务器地址 
  //                    iPort,    SMTP端口 
  //                    cLocalMail  发件人的邮箱 
  //                    iAuth  是否认证  0,不认证,1认证 
  //                    cUsername  认证用户名 
  //                    cPassword    认证密码 
  //            OUT:  无 
  //返回值  0:    成功设置 
  //              -1:失败,缺少属性 
  int  __fastcall  TM::SMTP_Setup(const  char  *  cHost,  const  int  iPort,  const  char  *cLocalMail,/ 
                                                    const  int  iAuth,  const  char  *  cUsername,  const  char  *cPassword) 
  { 
          int  iRet=0; 
          if(SMTP->Connected()) 
                  SMTP->Disconnect(); 
          SMTP->Host  =  String(cHost); 
          SMTP->Port  =  iPort; 
          this->LocalMail  =  String(cLocalMail); 
   
          switch  (iAuth) 
          { 
                  //  {authentication  settings} 
                  case    0: 
                        SMTP->AuthenticationType  =  atNone; 
                      break; 
                  case  1: 
                        SMTP->AuthenticationType  =  atLogin;  //{Simple  Login} 
                        break; 
          }; 
          SMTP->Username  =  cUsername; 
          SMTP->Password  =  cPassword; 
           
          SetupOk  =  true; 
          return  iRet; 
  } 

 

如果你遇到了这个问题,可能是因为你没有正确设置附件的路径,或者路径不正确。 以下是一些可能的解决方案: 1. 确认附件路径是否正确 在代码中,你需要指定附件的完整路径。例如,如果你的附件在`/Users/username/Documents/file.txt`,那么你应该使用以下代码来指定路径: ```python attachment = '/Users/username/Documents/file.txt' ``` 如果你不确定路径是否正确,可以使用`os.path.exists()`函数来检查文件是否存在。例如: ```python import os attachment = '/Users/username/Documents/file.txt' if os.path.exists(attachment): # do something with the attachment else: print('File not found:', attachment) ``` 2. 确认文件是否存在并具有读取权限 在尝试打开文件之前,你需要确保文件存在并具有读取权限。你可以使用`os.access()`函数来检查文件是否具有读取权限。例如: ```python import os attachment = '/Users/username/Documents/file.txt' if os.path.exists(attachment) and os.access(attachment, os.R_OK): # do something with the attachment else: print('File not found or not readable:', attachment) ``` 3. 使用绝对路径 如果你不确定相对路径是否正确,可以使用绝对路径来指定附件路径。例如: ```python import os current_dir = os.path.abspath(os.path.dirname(__file__)) attachment = os.path.join(current_dir, 'file.txt') if os.path.exists(attachment) and os.access(attachment, os.R_OK): # do something with the attachment else: print('File not found or not readable:', attachment) ``` 这将使用当前脚本的绝对路径作为基础路径,然后使用`os.path.join()`函数将文件名添加到路径中。 4. 确认文件名是否正确 如果你使用的是相对路径,确保文件名和文件扩展名正确。例如,如果你的文件名是`file.txt`而不是`file.txt.doc`,则应该使用以下代码: ```python attachment = 'file.txt' ``` 5. 检查邮件服务器设置 如果你的代码中附件路径正确,但仍然无法找到附件,那么可能是因为邮件服务器配置不正确。确保你已经正确配置了SMTP服务器和端口,并且已经提供了正确的用户名和密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值