在c++builder中使用indy编程,想要在邮件体内嵌入一个jpg图片,不用超链接的方法怎么实现。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在c++builder中使用indy编程,想要在邮件体内嵌入一个jpg图片,不用超链接的方法怎么实现。相关的知识,希望对你有一定的参考价值。
使用的是indy组件。怎样将一个图片作为邮件体的一部分发送出去?
哥们,我试过了,不能出图,请问怎么在邮件体里面出图呢,不用超链接的方式.
2//---------------------------------------------------------------------------
3
4#include <vcl.h>
5#pragma hdrstop
6
7#include "Main.h"
8//---------------------------------------------------------------------------
9#pragma package(smart_init)
10#pragma link "IdExplicitTLSClientServerBase"
11#pragma link "IdSMTPBase"
12#pragma resource "*.dfm"
13TForm1 *Form1;
14//---------------------------------------------------------------------------
15__fastcall TForm1::TForm1(TComponent* Owner)
16 : TForm(Owner)
17
18
19//---------------------------------------------------------------------------
20void __fastcall TForm1::btnSendClick(TObject *Sender)
21
22 //Mail
23 TIdText *idBody, *idhtml;
24 TIdAttachmentFile *idAtta;
25 try
26
27
28 idMsg=new TIdMessage(Application);
29 //Msg base header
30 idMsg->From->Name=edtName->Text.Trim();
31 idMsg->From->Address=edtMailaddr->Text.Trim();
32 idMsg->ReplyTo->EMailAddresses="no_reply@163.com";
33 idMsg->ContentType="multipart/alternative";
34 idMsg->ContentTransferEncoding="base64";
35 idMsg->AttachmentEncoding="MIME";
36 idMsg->Encoding=meDefault;
37 idMsg->CharSet="gb2312";
38 idMsg->Subject=edtSubject->Text.Trim();
39 idMsg->Recipients->EMailAddresses=edtTo->Text.Trim();
40 idMsg->Priority=mpNormal;
41
42 //msg body plain
43 idBody=new TIdText(idMsg->MessageParts, idMsg->Body);
44 idBody->CharSet="utf-8";
45 idBody->ContentType="text/plain";
46 idBody->ContentTransfer="base64";
47 idBody->Body->Add(mmoContent->Text);
48
49 //msg body html
50 idHtml=new TIdText(idMsg->MessageParts, idMsg->Body);
51 idHtml->CharSet="utf-8";
52 idHtml->ContentType="text/html";
53 idHtml->ContentTransfer="base64";
54 idHtml->Body->Add("<HTML><HEAD><TITLE>Mail</TITLE></HEAD>");
55 idHtml->Body->Add("<BODY>");
56 idHtml->Body->Add(mmoContent->Text);
57 idHtml->Body->Add("</BODY></HTML>");
58
59 //msg body attachment
60 if (edtAttach->Text.Trim()!="")
61
62 if (FileExists(edtAttach->Text.Trim()))
63
64 idAtta=new TIdAttachmentFile(idMsg->MessageParts, edtAttach->Text.Trim());
65
66 idAtta->ContentType="application/octet-stream";
67 idAtta->ContentDisposition="attachment";
68 idAtta->ContentTransfer="base64";
69 idAtta->FileName=ExtractFileName(edtAttach->Text.Trim());
70
71 else
72
73 edtAttach->Text="";
74
75
76
77 catch(Exception &exception)
78
79 //show Error!
80 idMsg->Clear();
81 delete idMsg;
82 return;
83
84
85 //SMTP Server
86 try
87
88 idSmtp->Username=edtUsername->Text;
89 idSmtp->Password=edtPasswd->Text;
90 idSmtp->HeloName="SMTP";
91 idSmtp->MailAgent="DreamMail";
92 idSmtp->UseEhlo=true;
93 idSmtp->ReadTimeout=5000;
94 idSmtp->Connect(edtServer->Text, StrToInt(edtPort->Text));
95 idSmtp->Send(idMsg);
96
97 catch()
98
99 //Show error!
100 idSmtp->Disconnect();
101 delete idMsg;
102 return;
103
104 idSmtp->Disconnect();
105 delete idMsg;
106 ShowMessage("Mail Sent!");
107
108//---------------------------------------------------------------------------
109void __fastcall TForm1::idSmtpConnected(TObject *Sender)
110
111 btnSend->Enabled=false;
112
113//---------------------------------------------------------------------------
114void __fastcall TForm1::idSmtpDisconnected(TObject *Sender)
115
116 btnSend->Enabled=true;
117
118//---------------------------------------------------------------------------
119void __fastcall TForm1::btnOpenfileClick(TObject *Sender)
120
121 try
122
123 TOpenDialog *opdGetfile=new TOpenDialog(Application);
124 opdGetfile->Options.Clear();
125 opdGetfile->Title = "Select attachment";
126 opdGetfile->Options << ofFileMustExist;
127 opdGetfile->InitialDir=ExtractFilePath(Application->ExeName);
128 opdGetfile->Filter = "All files (*.*)|*.*";
129 opdGetfile->FilterIndex = 2;
130 if(opdGetfile->Execute())
131
132 if (FileExists(opdGetfile->FileName))
133 edtAttach->Text=opdGetfile->FileName;
134
135
136
137 catch()
138
139 return;
140
141
142
143
144//---------------------------------------------------------------------------
145
1//main.h
2//---------------------------------------------------------------------------
3
4#ifndef MainH
5#define MainH
6//---------------------------------------------------------------------------
7#include <Classes.hpp>
8#include <Controls.hpp>
9#include <StdCtrls.hpp>
10#include <Forms.hpp>
11#include "IdExplicitTLSClientServerBase.hpp"
12#include "IdSMTPBase.hpp"
13#include <IdBaseComponent.hpp>
14#include <IdComponent.hpp>
15#include <IdMessageClient.hpp>
16#include <IdSMTP.hpp>
17#include <IdTCPClient.hpp>
18#include <IdTCPConnection.hpp>
19#include <IdText.hpp> //TIdText needed
20#include <IdAttachmentFile.hpp> //TIdAttachment needed
21//#include <IdMessageCoderMIME.hpp>
22#include <IdCoderHeader.hpp>
23
24//---------------------------------------------------------------------------
25class TForm1 : public TForm
26
27__published: // IDE-managed Components
28 TGroupBox *grpServer;
29 TEdit *edtServer;
30 TEdit *edtPort;
31 TEdit *edtUsername;
32 TEdit *edtPasswd;
33 TIdSMTP *idSmtp;
34 TGroupBox *grpMail;
35 TEdit *edtName;
36 TEdit *edtMailaddr;
37 TEdit *edtTo;
38 TEdit *edtSubject;
39 TMemo *mmoContent;
40 TButton *btnSend;
41 TEdit *edtAttach;
42 TButton *btnOpenfile;
43 TLabel *lbl1;
44 TLabel *lbl2;
45 TLabel *lbl3;
46 TLabel *lbl4;
47 TLabel *lbl5;
48 TLabel *lbl6;
49 TLabel *lbl7;
50 TLabel *lbl8;
51 TLabel *lbl9;
52 void __fastcall btnSendClick(TObject *Sender);
53 void __fastcall idSmtpConnected(TObject *Sender);
54 void __fastcall idSmtpDisconnected(TObject *Sender);
55 void __fastcall btnOpenfileClick(TObject *Sender);
56private: // User declarations
57 TIdMessage *idMsg;
58 //TIdMessageEncoderMIME *idMsg;
59public: // User declarations
60 __fastcall TForm1(TComponent* Owner);
61;
62//---------------------------------------------------------------------------
63extern PACKAGE TForm1 *Form1;
64//---------------------------------------------------------------------------
65#endif
66
使用 Delphi 和 Indy 以编程方式通过 Progress 事件从 Internet 下载文件
【中文标题】使用 Delphi 和 Indy 以编程方式通过 Progress 事件从 Internet 下载文件【英文标题】:Download a File from internet programmatically with an Progress event using Delphi and Indy 【发布时间】:2011-01-12 04:07:00 【问题描述】:我需要一种通过 HTTP 使用 Delphi 从 Internet 下载文件的方法, 其中包括一个 Progress 事件,我正在寻找一种使用 Indy 组件的方法。
我正在使用 Delphi 7。
【问题讨论】:
【参考方案1】:我已经编写了这个示例,仅使用一个 HTTP GET,使用 Indy 10,希望它也适用于 Indy 9:
uses
... IdHTTP, IdComponent;
type
TFormMain = class(TForm)
...
private
...
procedure HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
end;
...
procedure TFormMain.Button1Click(Sender: TObject);
var
Http: TIdHTTP;
MS: TMemoryStream;
begin
Http := TIdHTTP.Create(nil);
try
MS := TMemoryStream.Create;
try
Http.OnWork:= HttpWork;
Http.Get('http://live.sysinternals.com/ADExplorer.exe', MS);
MS.SaveToFile('C:\ADExplorer.exe');
finally
MS.Free;
end;
finally
Http.Free;
end;
end;
procedure TFormMain.HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
var
Http: TIdHTTP;
ContentLength: Int64;
Percent: Integer;
begin
Http := TIdHTTP(ASender);
ContentLength := Http.Response.ContentLength;
if (Pos('chunked', LowerCase(Http.Response.TransferEncoding)) = 0) and
(ContentLength > 0) then
begin
Percent := 100*AWorkCount div ContentLength;
MemoOutput.Lines.Add(IntToStr(Percent));
end;
end;
【讨论】:
Response.ContentLength 值并不总是有效的。特别是,在使用“分块”传输编码的 HTTP 1.1 回复中,不允许使用“Content-Length”标头。在分块传输期间,数据的总大小无法提前知道,因为数据在多个块中传输,并且每个块在内部都有自己的大小。 更好?现在我在IdHTTP.pas
单元内使用与TIdCustomHTTP.ReadResult()
完全相同的条件
别忘了在 OnWork 活动中写上Application.ProcessMessages();
!
不要使用Application.ProcessMessages()
。这将为所有未决消息抽出消息队列,如果您不小心,可能会导致副作用和重入问题。最好使用TForm.Update()
方法来仅处理待处理的绘制消息而不处理其他消息。
与其避免Application.ProcessMessages()
,不如设计您的应用程序,使其能够在文件下载期间处理接收消息。这可以通过将下载代码移动到一个线程中来完成(但您现在有两个 porbelms!),或者通过使用 Application.ProcessMessages()
并处理可能的情况(按钮单击、窗口关闭等)以防止重新进入问题 - - 通常最简单的方法是专门的下载对话框。如果您只使用TForm.Update()
,您将导致您的应用程序无法响应用户交互。以上是关于在c++builder中使用indy编程,想要在邮件体内嵌入一个jpg图片,不用超链接的方法怎么实现。的主要内容,如果未能解决你的问题,请参考以下文章
C++ Builder 2009 - IndySystem120.bpl - 未找到入口点