552
社区成员
发帖
与我相关
我的任务
分享
#include <IdBaseComponent.hpp>
#include <IdComponent.hpp>
#include <IdExplicitTLSClientServerBase.hpp>
#include <IdMessageClient.hpp>
#include <IdSMTP.hpp>
#include <IdSMTPBase.hpp>
#include <IdTCPClient.hpp>
#include <IdTCPConnection.hpp>
#include <IdMessage.hpp>
#include "IdAttachmentFile.hpp"
#include "IdText.hpp"
TIdText *idBody, *idHtml;
TIdAttachmentFile *idAtta;
TIdMessage *IdMessage1;
IdMessage1=new TIdMessage(Application);
IdMessage1->CharSet= "GB2312"; ////信的内容使用的字符集 "uft-8";
IdMessage1->ContentType="multipart/alternative";
IdMessage1->ContentTransferEncoding="base64";
IdMessage1->AttachmentEncoding="MIME"; //识别默认编码机制用于邮件部分
IdMessage1->Encoding=meDefault;
IdMessage1->Subject = "hhh测试电子邮箱" ; //标题
IdMessage1->Sender->Name = "测试"; //发信人姓名
//IdMessage1->From->Name="drlqyy";
//IdMessage1->From->Address="drlqyy@163.com";; //发件人地址
IdMessage1->From->Text = "drlqyy@163.com"; //发信人发信 Email 地址
IdMessage1->ReplyTo->EMailAddresses = "drlqyy@163.com"; //发信人回信 EMail 地址
IdMessage1->Recipients->EMailAddresses ="drlqyy <drlqyy@sina.com>"; //收件人地址
IdMessage1->Priority=mpNormal;
idBody=new TIdText(IdMessage1->MessageParts, IdMessage1->Body);
idBody->CharSet="gb2312";
idBody->ContentType="text/plain";
idBody->ContentTransfer="base64";
idBody->Body->Add("测试电子邮箱");
idHtml=new TIdText(IdMessage1->MessageParts, IdMessage1->Body);
idHtml->CharSet="gb2312";
idHtml->ContentType="text/html";
idHtml->ContentTransfer="base64";
idHtml->Body->Add("<HTML><HEAD><TITLE>Mail</TITLE></HEAD>"); //发送内容
idHtml->Body->Add("<BODY>");
idHtml->Body->Add("test五彩豆腐是在传统白豆腐中加入天然色汁和风味反电动势分公司狄梵思黛非师范大是大非士大夫\13");
idHtml->Body->Add("</BODY></HTML>");
//发顺附件
idAtta=new TIdAttachmentFile(IdMessage1->MessageParts, "D:\\111.txt");
idAtta->ContentType="application/octet-stream";
idAtta->ContentDisposition="attachment";
idAtta->ContentTransfer="base64";
idAtta->FileName=ExtractFileName("D:\\111.txt");
TIdSMTP *SMTP1 = new TIdSMTP(Application);
try
{
SMTP1->Username="drlqyy@163.com";
SMTP1->Password="88888888";
SMTP1->HeloName="SMTP";
SMTP1->MailAgent="DreamMail";
SMTP1->UseEhlo=true;
SMTP1->ReadTimeout=5000;
SMTP1->Connect("smtp.163.com", 25);
SMTP1->Send(IdMessage1);
}
catch(...)
{
SMTP1->Disconnect();
delete IdMessage1;
delete SMTP1;
return;
}
SMTP1->Disconnect();
delete IdMessage1;
delete SMTP1;
ShowMessage("发送成功!");
function ToBytes(const AValue: string; const ALength: Integer; const AIndex: Integer = 1;
const AEncoding: TIdEncoding = en7Bit): TIdBytes; overload;
var
LLength: Integer;
begin
ValidEncoding(AEncoding);
LLength := IndyLength(AValue, ALength, AIndex);
if LLength > 0 then
begin
if AEncoding = enUTF8 then begin
Result := StringToUTF8Bytes(AValue, AIndex, LLength);
end else
begin
// do just a byte to byte copy with no translation. VCL uses ANSI or MBCS.
// With MBCS we still map 1:1
SetLength(Result, LLength);
CopyTIdString(AValue, AIndex, Result, 0, LLength,AEncoding);
end;
end else begin
SetLength(Result, 0);
end;
end;