Delphi XE5 Indy:为什么不发送消息?

时间:2015-05-19 18:45:39

标签: delphi smtp indy

我已经创建了在我的应用程序中发送邮件的功能。应用程序正常连接到服务器但不发送消息。 该功能如下:

function TForm1.SendEmail(sendTo, subject, body: string; 
attachFiles: TStringList; smtpHost: string; smtpPort: Integer; smtpUser,
smtpPass: string; tls: TIdUseTLS): boolean;
var
    smtp: TIdSmtp;
    ssl: TIdSSLIOHandlerSocketOpenSSL;
    msg: TIdMessage;
    i: Integer;
begin
    smtp:=TIdSmtp.Create(nil);
    ssl:=TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    msg:=TIdMessage.Create(nil);
    try
        try
            smtp.Host:=smtpHost;
            smtp.Port:=smtpPort;
            smtp.Username:=smtpUser;
            smtp.Password:=smtpPass;

            if not (tls=utNoTLSSupport) then begin
                ssl.Destination:=smtpHost + ':' + IntToStr(smtpPort);
                ssl.Host:=smtpHost;
                ssl.Port:=smtpPort;
                ssl.SSLOptions.Method:=sslvTLSv1;

                smtp.IOHandler:=ssl;
                smtp.UseTLS:=tls;
            end;

            msg.Recipients.EMailAddresses := sendTo;
            msg.Subject:=subject;
            msg.Body.Text:=body;

            if(Assigned(attachFiles)) then begin
                for i := 0 to attachFiles.Count - 1 do begin
                   if FileExists(attachFiles[i]) then
                        TIdAttachmentFile.Create(msg.MessageParts, attachFiles[i]);
                end;
            end;
            smtp.Connect; //Works normally till this line
            smtp.Send(msg); // <-- PROBLEM AT THIS LINE
            smtp.Disconnect;
            result:=true;
        finally
            msg.Free;
            ssl.Free;
            smtp.Free;
        end;
    except
       result:=false;
    end;
end;

它正常连接,但不发送消息。我在代码中通过评论提到了它。

0 个答案:

没有答案