关于邮件发送问题(idsmtp),高手请指教!

时间:2022-06-01 18:32:59

IdMessage1->Clear();

    IdMessage1->BccList->EMailAddresses="";                         //暗抄
    IdMessage1->CCList->EMailAddresses="";                          //抄送
    IdMessage1->ReceiptRecipient->Text="";
    TIdAttachment(IdMessage1->MessageParts, "test.txt");            //发送附件

    IdMessage1->Priority = TIdMessagePriority(0);                    //高优先级
    IdMessage1->Recipients->EMailAddresses = edtAccept->Text.Trim(); //收件人地址
    IdMessage1->Subject = edtSubject->Text;                          //主题
    IdMessage1->Body->Assign(Memo1->Lines);                          //发送内容
    IdMessage1->From->Address = edtSend->Text.Trim();                //发件人地址

    IdSMTP1->Host = edtServer->Text.Trim();
    IdSMTP1->Port = 25;

    IdSMTP1->UserId = edtUserID->Text.Trim();
    IdSMTP1->Password = edtPassword->Text.Trim();
    IdSMTP1->AuthenticationType = atLogin;
    try
    {
        IdSMTP1->Connect();
        bool bflag = IdSMTP1->Authenticate();
        IdSMTP1->Send(IdMessage1);
    }
    __finally
    {
        IdSMTP1->Disconnect();
    }

8 个解决方案

#1


上面的代码在邮件服务器区域内能发送邮件,但不能发送到外部邮件服务器,提示收件人地址识别不了.

问题可能出在不能解释域名,如收件人地址:XXX@163.com,idsmtp怎么根据字符串XXX@163.com来解释要发送到哪里去呢?

#2


是不是上面的代码少写了,那idsmtp控件还有什么属性要设置吗?

#3


下面是我的代码!
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{

  if (NMSMTP1->Connected)
    NMSMTP1->Disconnect();
  else
    {
      NMSMTP1->Host = Trim(Edit1->Text);
      NMSMTP1->UserID = Trim(Edit2->Text);
      try{
      NMSMTP1->Connect();
      }
      catch(...)
      {
      StatusBar1->SimpleText="连接失败!";
      return;
      }
      StatusBar1->SimpleText="连接成功!";
      BitBtn1->Enabled=true;
    }

    StatusBar1->SimpleText="正在发送...";
    NMSMTP1->ClearParams = true;
    NMSMTP1->SubType = mtPlain;
    NMSMTP1->EncodeType = uuMime;
    NMSMTP1->PostMessage->FromAddress ="wang_dr2002@163.com";
    NMSMTP1->PostMessage->FromName=EditName->Text;
    NMSMTP1->PostMessage->ToAddress->Add(EditAddress->Text);
    NMSMTP1->PostMessage->Subject=EditSubject->Text;
    NMSMTP1->PostMessage->Body->Add(Body1->Text);
    NMSMTP1->PostMessage->LocalProgram="SendMail";
    NMSMTP1->SendMail();
    StatusBar1->SimpleText="发送完成";
    NMSMTP1->Disconnect();
}

void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
  if (NMSMTP1->Connected)

    NMSMTP1->Disconnect();

  else

    {

      NMSMTP1->Host = Trim(Edit1->Text);

      NMSMTP1->UserID = Trim(Edit2->Text);

      try{
      NMSMTP1->Connect();
      }
      catch(...)
      {
      StatusBar1->SimpleText="连接失败!";
      return;
      }
      StatusBar1->SimpleText="连接成功!";
    }
}
void __fastcall TForm1::NMSMTP1AttachmentNotFound(AnsiString Filename)
{
  Memo5->Lines->Add("File attachment "+Filename+" not found");
}

void __fastcall TForm1::NMSMTP1AuthenticationFailed(bool &Handled)
{
  AnsiString S;
  S = NMSMTP1->UserID;
  if (InputQuery("Authentication Failed", "Invalid User ID. New User ID: ", S))
    {
      NMSMTP1->UserID = S;
      Handled = TRUE;
    }
}

void __fastcall TForm1::NMSMTP1Connect(TObject *Sender)
{
  Memo5->Lines->Add("Connected");
  AnsiString strUserName, strPassword;
  strUserName = EncodeString(Trim(Edit1->Text));
  strPassword = EncodeString("19681110");//Password是密码
//进行认证,输入编码后的用户名、密码
  NMSMTP1->Transaction("EHLO") ;
  NMSMTP1->Transaction("AUTH LOGIN");
  NMSMTP1->Transaction(strUserName);
  NMSMTP1->Transaction(strPassword);
  StatusBar1->SimpleText = "连接成功";
}

void __fastcall TForm1::NMSMTP1SendStart(TObject *Sender)
{
  Memo5->Lines->Add("Sending Message");
}

void __fastcall TForm1::NMSMTP1EncodeStart(AnsiString Filename)
{
  Memo5->Lines->Add("Encoding "+Filename);
}

void __fastcall TForm1::NMSMTP1EncodeEnd(AnsiString Filename)
{
  Memo5->Lines->Add(Filename+" encoded");
}

void __fastcall TForm1::NMSMTP1Failure(TObject *Sender)
{
  Memo5->Lines->Add("Message delivery failure");
}

void __fastcall TForm1::NMSMTP1Success(TObject *Sender)
{
  Memo5->Lines->Add("Message sent successfully");
}

void __fastcall TForm1::NMSMTP1HeaderIncomplete(bool &handled, int hiType)
{
  AnsiString S;
  switch(hiType)
    {
      case hiFromAddress:
        {
          if (InputQuery("Missing From Address", "Enter From Address: ", S))
            {
              NMSMTP1->PostMessage->FromAddress = S;
              handled = TRUE;
            }
        }
      case hiToAddress:
        {
          if (InputQuery("Missing To Address", "Enter To Address: ", S))
            {
              NMSMTP1->PostMessage->ToAddress->Text = S;
              handled = TRUE;
            }
        }
    }
}

void __fastcall TForm1::NMSMTP1RecipientNotFound(AnsiString Recipient)
{
  Memo5->Lines->Add("Recipient "+Recipient+" not found");
}

void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
  if (NMSMTP1->Verify(Edit2->Text))
    ShowMessage("username is wrong.");
  else
    ShowMessage("username is correct.");
}

#4



AnsiString __fastcall TForm1::EncodeString(AnsiString Decoded)
{
    TMemoryStream *mmTemp=new TMemoryStream();
    TMemoryStream *mmDecoded=new TMemoryStream();
    AnsiString strTemp;

    TStringList* Mylist = new TStringList();

    Mylist->Add(Decoded);
    Mylist->SaveToStream(mmTemp);
    mmTemp->Position = 0;
//    {剔除mmTemp从strTemp中带来的字符#13#10}
    mmDecoded->CopyFrom(mmTemp,mmTemp->Size-2);
//    {对mmDecoded进行Base64编码,由mmTemp返回编码后的结果}

    EncodeBASE64(mmTemp,mmDecoded);

//    {获得Base64编码后的字符串}
    mmTemp->Position=0;

    Mylist->LoadFromStream(mmTemp);
//    {返回结果必须从strTemp[0]中获得,如果使用strTemp.Text会
//    带来不必要的字符#13#10}

    strTemp=Mylist->Text;
    return strTemp;
}

int  __fastcall TForm1::EncodeBASE64(TMemoryStream *Encoded,TMemoryStream *Decoded)
{

    char const _Code64[64]=
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    int I;
    //TByteArray B[2279];
    byte  B[2279];
    int J, K, L, M, Quads;

    char Stream[76];
    String EncLine ;

    Encoded->Clear();

    Stream[0]=0;
    Quads = 0;
//    {为提高效率,每2280字节流为一组进行编码}
    J = Decoded->Size / 2280;
    Decoded->Position = 0;
//    {对前J*2280个字节流进行编码}

    for(I = 1;I<=J;I++ )
    {
        Decoded->Read(B, 2280);
        for (M= 0;M<=39;M++)
        {
            for (K = 0;K<=18;K++)
            {
                L= 57*M + 3*K;

                Stream[Quads+1] = _Code64[(B[L] / 4)+1];
                Stream[Quads+2] = _Code64[(B[L] % 4)*16 + (B[L+1] / 16)+1];
                Stream[Quads+3] = _Code64[(B[L+1] % 16)*4 + (B[L+2] / 64)+1];
                Stream[Quads+4] = _Code64[B[L+2] % 64+1];
                Quads=Quads+ 4;
                if (Quads == 76 )
                {
                    Stream[0] = 76;

                    EncLine = AnsiString(Stream) + "\n";
                    Encoded->Write(&EncLine[1], EncLine.Length());
                    Quads = 0;
                }
            }
        }
    }

//    {对以2280为模的余数字节流进行编码}
    J = (Decoded->Size % 2280) / 3;
    for (I = 1;I<=J;I++)
    {
        Decoded->Read(B, 3);
        Stream[Quads+1] = _Code64[(B[0] / 4)+1];
        Stream[Quads+2] = _Code64[(B[0] % 4)*16 + (B[1] / 16)+1];
        Stream[Quads+3] = _Code64[(B[1] % 16)*4 + (B[2] / 64)+1];
        Stream[Quads+4] = _Code64[B[2] % 64+1];
        Quads=Quads+ 4;
//        {每行76个字符}
        if (Quads == 76 )
        {
            Stream[0] = 76;
            EncLine = (AnsiString)Stream+"\n";
            Encoded->Write(&EncLine[1], EncLine.Length());
            Quads = 0;
        }
    }
//    {“=”补位}
    if ((Decoded->Size % 3) == 2 )
    {
        Decoded->Read(B, 2);
        Stream[Quads+1] = _Code64[(B[0] / 4)+1];
        Stream[Quads+2] = _Code64[(B[0] % 4)*16 + (B[1] / 16)+1];
        Stream[Quads+3] = _Code64[(B[1] % 16)*4 + 1];
        Stream[Quads+4] = '=';
        Quads= Quads+4;
    }

    if ((Decoded->Size % 3) == 1 )
    {
        Decoded->Read(B, 1);
        Stream[Quads+1] = _Code64[(B[0] / 4)+1];
        Stream[Quads+2] = _Code64[(B[0] % 4)*16 + 1];
        Stream[Quads+3] = '=';
        Stream[Quads+4] = '=';
        Quads= Quads+4;
    }

    Stream[0] = Quads;
    if (Quads > 0)
    {
        EncLine = (AnsiString)Stream+"\n";
        Encoded->Write(&EncLine[1], EncLine.Length());
    }

    int Result = Encoded->Size;

    return   Result;
}


#5


xwhpc(xwh) ,你能将项目代码发给我吗,我在本机上测试下,我的Email:huozhu.qiu@mt.com
谢谢!

#6


to xwhpc(xwh),如果你嫌分数少的话,我会再加分给你的,只要我能解决问题!

#7



andy_qhz(老邱):

你的代码没错啊?!!!
我测试了通过!

是不是你的邮箱名,或者密码不对?

    IdSMTP1->Host = "smtp.sina.com.cn";
    IdSMTP1->Port = 25;

    IdSMTP1->UserId = edtUserID->Text.Trim();       // 用户id,比如:andy_qhz 不是andy_qhz@sina.com
    IdSMTP1->Password = edtPassword->Text.Trim();   // 你的邮箱密码

#8


谢谢两位的热心,但问题还没解决,应该出在邮件服务器域名解释上,
那么多天都没人来解答,也就只能作算.

#1


上面的代码在邮件服务器区域内能发送邮件,但不能发送到外部邮件服务器,提示收件人地址识别不了.

问题可能出在不能解释域名,如收件人地址:XXX@163.com,idsmtp怎么根据字符串XXX@163.com来解释要发送到哪里去呢?

#2


是不是上面的代码少写了,那idsmtp控件还有什么属性要设置吗?

#3


下面是我的代码!
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{

  if (NMSMTP1->Connected)
    NMSMTP1->Disconnect();
  else
    {
      NMSMTP1->Host = Trim(Edit1->Text);
      NMSMTP1->UserID = Trim(Edit2->Text);
      try{
      NMSMTP1->Connect();
      }
      catch(...)
      {
      StatusBar1->SimpleText="连接失败!";
      return;
      }
      StatusBar1->SimpleText="连接成功!";
      BitBtn1->Enabled=true;
    }

    StatusBar1->SimpleText="正在发送...";
    NMSMTP1->ClearParams = true;
    NMSMTP1->SubType = mtPlain;
    NMSMTP1->EncodeType = uuMime;
    NMSMTP1->PostMessage->FromAddress ="wang_dr2002@163.com";
    NMSMTP1->PostMessage->FromName=EditName->Text;
    NMSMTP1->PostMessage->ToAddress->Add(EditAddress->Text);
    NMSMTP1->PostMessage->Subject=EditSubject->Text;
    NMSMTP1->PostMessage->Body->Add(Body1->Text);
    NMSMTP1->PostMessage->LocalProgram="SendMail";
    NMSMTP1->SendMail();
    StatusBar1->SimpleText="发送完成";
    NMSMTP1->Disconnect();
}

void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
  if (NMSMTP1->Connected)

    NMSMTP1->Disconnect();

  else

    {

      NMSMTP1->Host = Trim(Edit1->Text);

      NMSMTP1->UserID = Trim(Edit2->Text);

      try{
      NMSMTP1->Connect();
      }
      catch(...)
      {
      StatusBar1->SimpleText="连接失败!";
      return;
      }
      StatusBar1->SimpleText="连接成功!";
    }
}
void __fastcall TForm1::NMSMTP1AttachmentNotFound(AnsiString Filename)
{
  Memo5->Lines->Add("File attachment "+Filename+" not found");
}

void __fastcall TForm1::NMSMTP1AuthenticationFailed(bool &Handled)
{
  AnsiString S;
  S = NMSMTP1->UserID;
  if (InputQuery("Authentication Failed", "Invalid User ID. New User ID: ", S))
    {
      NMSMTP1->UserID = S;
      Handled = TRUE;
    }
}

void __fastcall TForm1::NMSMTP1Connect(TObject *Sender)
{
  Memo5->Lines->Add("Connected");
  AnsiString strUserName, strPassword;
  strUserName = EncodeString(Trim(Edit1->Text));
  strPassword = EncodeString("19681110");//Password是密码
//进行认证,输入编码后的用户名、密码
  NMSMTP1->Transaction("EHLO") ;
  NMSMTP1->Transaction("AUTH LOGIN");
  NMSMTP1->Transaction(strUserName);
  NMSMTP1->Transaction(strPassword);
  StatusBar1->SimpleText = "连接成功";
}

void __fastcall TForm1::NMSMTP1SendStart(TObject *Sender)
{
  Memo5->Lines->Add("Sending Message");
}

void __fastcall TForm1::NMSMTP1EncodeStart(AnsiString Filename)
{
  Memo5->Lines->Add("Encoding "+Filename);
}

void __fastcall TForm1::NMSMTP1EncodeEnd(AnsiString Filename)
{
  Memo5->Lines->Add(Filename+" encoded");
}

void __fastcall TForm1::NMSMTP1Failure(TObject *Sender)
{
  Memo5->Lines->Add("Message delivery failure");
}

void __fastcall TForm1::NMSMTP1Success(TObject *Sender)
{
  Memo5->Lines->Add("Message sent successfully");
}

void __fastcall TForm1::NMSMTP1HeaderIncomplete(bool &handled, int hiType)
{
  AnsiString S;
  switch(hiType)
    {
      case hiFromAddress:
        {
          if (InputQuery("Missing From Address", "Enter From Address: ", S))
            {
              NMSMTP1->PostMessage->FromAddress = S;
              handled = TRUE;
            }
        }
      case hiToAddress:
        {
          if (InputQuery("Missing To Address", "Enter To Address: ", S))
            {
              NMSMTP1->PostMessage->ToAddress->Text = S;
              handled = TRUE;
            }
        }
    }
}

void __fastcall TForm1::NMSMTP1RecipientNotFound(AnsiString Recipient)
{
  Memo5->Lines->Add("Recipient "+Recipient+" not found");
}

void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
  if (NMSMTP1->Verify(Edit2->Text))
    ShowMessage("username is wrong.");
  else
    ShowMessage("username is correct.");
}

#4



AnsiString __fastcall TForm1::EncodeString(AnsiString Decoded)
{
    TMemoryStream *mmTemp=new TMemoryStream();
    TMemoryStream *mmDecoded=new TMemoryStream();
    AnsiString strTemp;

    TStringList* Mylist = new TStringList();

    Mylist->Add(Decoded);
    Mylist->SaveToStream(mmTemp);
    mmTemp->Position = 0;
//    {剔除mmTemp从strTemp中带来的字符#13#10}
    mmDecoded->CopyFrom(mmTemp,mmTemp->Size-2);
//    {对mmDecoded进行Base64编码,由mmTemp返回编码后的结果}

    EncodeBASE64(mmTemp,mmDecoded);

//    {获得Base64编码后的字符串}
    mmTemp->Position=0;

    Mylist->LoadFromStream(mmTemp);
//    {返回结果必须从strTemp[0]中获得,如果使用strTemp.Text会
//    带来不必要的字符#13#10}

    strTemp=Mylist->Text;
    return strTemp;
}

int  __fastcall TForm1::EncodeBASE64(TMemoryStream *Encoded,TMemoryStream *Decoded)
{

    char const _Code64[64]=
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    int I;
    //TByteArray B[2279];
    byte  B[2279];
    int J, K, L, M, Quads;

    char Stream[76];
    String EncLine ;

    Encoded->Clear();

    Stream[0]=0;
    Quads = 0;
//    {为提高效率,每2280字节流为一组进行编码}
    J = Decoded->Size / 2280;
    Decoded->Position = 0;
//    {对前J*2280个字节流进行编码}

    for(I = 1;I<=J;I++ )
    {
        Decoded->Read(B, 2280);
        for (M= 0;M<=39;M++)
        {
            for (K = 0;K<=18;K++)
            {
                L= 57*M + 3*K;

                Stream[Quads+1] = _Code64[(B[L] / 4)+1];
                Stream[Quads+2] = _Code64[(B[L] % 4)*16 + (B[L+1] / 16)+1];
                Stream[Quads+3] = _Code64[(B[L+1] % 16)*4 + (B[L+2] / 64)+1];
                Stream[Quads+4] = _Code64[B[L+2] % 64+1];
                Quads=Quads+ 4;
                if (Quads == 76 )
                {
                    Stream[0] = 76;

                    EncLine = AnsiString(Stream) + "\n";
                    Encoded->Write(&EncLine[1], EncLine.Length());
                    Quads = 0;
                }
            }
        }
    }

//    {对以2280为模的余数字节流进行编码}
    J = (Decoded->Size % 2280) / 3;
    for (I = 1;I<=J;I++)
    {
        Decoded->Read(B, 3);
        Stream[Quads+1] = _Code64[(B[0] / 4)+1];
        Stream[Quads+2] = _Code64[(B[0] % 4)*16 + (B[1] / 16)+1];
        Stream[Quads+3] = _Code64[(B[1] % 16)*4 + (B[2] / 64)+1];
        Stream[Quads+4] = _Code64[B[2] % 64+1];
        Quads=Quads+ 4;
//        {每行76个字符}
        if (Quads == 76 )
        {
            Stream[0] = 76;
            EncLine = (AnsiString)Stream+"\n";
            Encoded->Write(&EncLine[1], EncLine.Length());
            Quads = 0;
        }
    }
//    {“=”补位}
    if ((Decoded->Size % 3) == 2 )
    {
        Decoded->Read(B, 2);
        Stream[Quads+1] = _Code64[(B[0] / 4)+1];
        Stream[Quads+2] = _Code64[(B[0] % 4)*16 + (B[1] / 16)+1];
        Stream[Quads+3] = _Code64[(B[1] % 16)*4 + 1];
        Stream[Quads+4] = '=';
        Quads= Quads+4;
    }

    if ((Decoded->Size % 3) == 1 )
    {
        Decoded->Read(B, 1);
        Stream[Quads+1] = _Code64[(B[0] / 4)+1];
        Stream[Quads+2] = _Code64[(B[0] % 4)*16 + 1];
        Stream[Quads+3] = '=';
        Stream[Quads+4] = '=';
        Quads= Quads+4;
    }

    Stream[0] = Quads;
    if (Quads > 0)
    {
        EncLine = (AnsiString)Stream+"\n";
        Encoded->Write(&EncLine[1], EncLine.Length());
    }

    int Result = Encoded->Size;

    return   Result;
}


#5


xwhpc(xwh) ,你能将项目代码发给我吗,我在本机上测试下,我的Email:huozhu.qiu@mt.com
谢谢!

#6


to xwhpc(xwh),如果你嫌分数少的话,我会再加分给你的,只要我能解决问题!

#7



andy_qhz(老邱):

你的代码没错啊?!!!
我测试了通过!

是不是你的邮箱名,或者密码不对?

    IdSMTP1->Host = "smtp.sina.com.cn";
    IdSMTP1->Port = 25;

    IdSMTP1->UserId = edtUserID->Text.Trim();       // 用户id,比如:andy_qhz 不是andy_qhz@sina.com
    IdSMTP1->Password = edtPassword->Text.Trim();   // 你的邮箱密码

#8


谢谢两位的热心,但问题还没解决,应该出在邮件服务器域名解释上,
那么多天都没人来解答,也就只能作算.