//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Mainfrm_Unit.h"
#include "IdAttachmentFile.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdExplicitTLSClientServerBase"
#pragma link "IdMessageClient"
#pragma link "IdSMTP"
#pragma link "IdSMTPBase"
#pragma link "IdTCPClient"
#pragma link "IdTCPConnection"
#pragma link "IdIOHandler"
#pragma link "IdIOHandlerSocket"
#pragma link "IdIOHandlerStack"
#pragma link "IdSSL"
#pragma link "IdSSLOpenSSL"
#pragma link "IdMessage"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn1Click(TObject *Sender)
{
//
IdMessage1->CharSet = "gb2312";
IdMessage1->Recipients->EMailAddresses = edtDest->Text; //邮件接收者的信箱
IdMessage1->From->Text = edtSource->Text; //邮件发送者的信箱
IdMessage1->Subject = edtTitle->Text; //邮件的标题
IdMessage1->Body->Add(mmoMsg->Text);//邮件内容
//IdMessage1->CCList->EMailAddresses = "gzxsg@126.com"; //邮件的抄送地址
IdMessage1->MessageParts->Clear();
IdMessage1->MessageParts->Add();
UnicodeString AttFile = edtAttchment->Text;//"d:\\单页属性表.dotx";//附件文件名
TIdAttachmentFile *idAtta = new TIdAttachmentFile(IdMessage1->MessageParts,AttFile);
IdSMTP1->Host = edtServer->Text;
IdSMTP1->Port = edtServerPort->Text.ToIntDef(25);
//IdSMTP1->AuthenticationType = atLogin;
IdSMTP1->AuthType = satDefault;
IdSMTP1->Username = edtUser->Text;
IdSMTP1->Password = edtPassword->Text;
try
{
IdSMTP1->Connect();
IdSMTP1->Authenticate();
IdSMTP1->Send(IdMessage1);
Application->MessageBox(L"发送完成!",L"邮件系统",MB_ICONINFORMATION);
}
catch(Exception &e)
{
Application->MessageBox(e.Message.c_str(),L"邮件系统",MB_OK+MB_ICONWARNING);
}
IdSMTP1->Disconnect();
// IdMessage1->Recipients->EMailAddresses = "8368382@qq.com"; //邮件接收者的信箱
// IdMessage1->From->Text = "gzxsg@126.com"; //邮件发送者的信箱
// IdMessage1->Subject = "测试发送电子邮件"; //邮件的标题
// IdMessage1->Body->Add("利用C++Builder中的IdSMTP和IdMessage控件发送电子邮件");//邮件内容
// IdMessage1->CCList->EMailAddresses = "8368382@126.com"; //邮件的抄送地址
// IdMessage1->MessageParts->Clear();
// IdMessage1->MessageParts->Add();
// //AnsiString AttFile=" d:\\a.txt";//附件文件名
// //TIdAttachmentFile *idAtta=new TIdAttachmentFile(IdMessage1->MessageParts,AttFile);
// IdSMTP1->Host = "smtp.126.com";
// IdSMTP1->Port = 25;
// //IdSMTP1->AuthenticationType = atLogin;
// IdSMTP1->AuthType = satDefault;
// IdSMTP1->Username = "gzxsg";
// IdSMTP1->Password = "79780812";
// IdSMTP1->Connect();
// IdSMTP1->Authenticate();
// IdSMTP1->Send(IdMessage1);
// IdSMTP1->Disconnect();
// IdSMTP1->Host = "smtp.126.com";
// IdSMTP1->Port = 25;
// IdSMTP1->IOHandler = m_IdSSLIOHandlerSocketOpenSSL;
// IdSMTP1->UseTLS = utNoTLSSupport;
//
//
// IdSMTP1->Username = L"gzxsg";
// IdSMTP1->Password = L"79780812" ;
// IdSMTP1->AuthType = satDefault;
// IdSMTP1->Authenticate();
//
//
// IdMessage1->ContentType = "text/html";
// IdMessage1->Subject = "TEST"; // 邮件标题,只要是汉字, 就会报错 RangeCheck error,英没问题. C++ BUILDER 2010自带的indy
//
// IdMessage1->ReceiptRecipient->Address = L"8368382@qq.com";
// if (!IdSMTP1->Connected())
// {
// try
// {
//
// IdSMTP1->Connect();
// }catch(...)
// {
// Application->MessageBox(L"连接邮件服务器失败!", L"提示", MB_OK + MB_ICONINFORMATION);
// return;
// }
// }
// if (IdSMTP1->Connected())
// {
// try
// {
// IdSMTP1->Send(IdMessage1);
// }
// catch(...)
// {
// Application->MessageBox(L"发送邮件失败!", L"提示", MB_OK + MB_ICONWARNING);
// return;
// }
// }
//
// Application->MessageBox(L"邮件发送成功!", L"提示", MB_OK + MB_ICONINFORMATION);
// int recnum, k; AnsiString addr;
// IdSMTP1->Host = ComboBoxaddr->Text.Trim ();//设定SMTP服务器地址和端口号
// IdSMTP1->Port =25;
// if(CheckBox_AUTH->Checked ==true) //判断并设置SMTP服务器是否需要认证
// {
// IdSMTP1->Authentication Type = at Login;
// IdSMTP1->UserId = Edit name->Text. Trim ();
// IdSMTP1->Password=MaskEdit_pass->Text. Trim ();
// }
//
// IdMessage1->Content Type = "text/html"; //根据用户填写的信息创建邮件
// IdMessage1->Subject =Edit3->Text.Trim ();
// ADOTable1->First(); //循环读取邮件地址并发送实现群发
// Recnum= ADOTable1->Record Count;
// for (k=0;k<=recnum-1; k++)
// {
// Addr=Trim (ADOTable1->Field Byname ("Email")->AsString);
// IdMessage1->Recipients->Email Addresses =addr;
// if (!IdSMTP1->Connected())
// {
// try
// {
// IdSMTP1->Connect ();
// }
// }
// catch (Exception %26amp;e)
// {
// Application->Message Box(e.Message, "连接SMTP服务器失败", MB_ICONWARNING);
// }
// if (IdSMTP1->Connected ())
// {
// try
// {
// IdSMTP1->Send (IdMessage1);
// }
// catch (Exception %26amp;e)
// {
// Application->Message Box(e.Message, "发送失败", MB_ICONWARNING); }
// }
// ADOTable1->Next ();}
// Application->Message Box ("发送完成!","邮件系统",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//
}
//---------------------------------------------------------------------------