使用:Delphi XE7,Indy 10的最新每日版本。
此代码在2个月前开始运作。现在我使用相同的帐户详细信息重新编译了我的应用程序,它不再起作用了。异常消息是“Disconnected”。
使用的SMTP端口是587。
代码如下:
FIdSMTP := TIdSMTP.Create;
FIdSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
FIdMessage := TIdMessage.Create;
FIdAttachment := TIdAttachmentFile.Create(FIdMessage.MessageParts);
if (FEmailServer.SMTPUseSSL) then
begin
FIdSSLIOHandler.SSLOptions.Method := sslvTLSv1;
// FIdSSLIOHandler.SSLOptions.Method := sslvSSLv3;
FIdSMTP.IOHandler := FIdSSLIOHandler;
FIdSMTP.UseTLS := utUseExplicitTLS;
// FIdSMTP.UseTLS := utUseImplicitTLS;
end;
FIdSMTP.Host := FEmailServer.SMTPHost; //smtp.gmail.com
FIdSMTP.Port := FEmailServer.SMTPPort; //587
FIdSMTP.AuthType := satDefault;
FIdSMTP.Username := FEmailServer.SMTPUserName;
FIdSMTP.Password := FEmailServer.SMTPPassword;
FIdMessage.Recipients[0].Address := mailmessage.RecipientEmailAddr;
FIdMessage.From.Name := mailmessage.SenderName;
FIdMessage.Subject := mailmessage.EmailSubject;
FIdMessage.Body.Text := mailmessage.EmailMessage;
FIdAttachment.LoadFromFile(mailmessage.AttachmentFile);
FIdAttachment.FileName := ExtractFileName(mailmessage.AttachmentFile);
try
if (not FIdSMTP.Connected) then
FIdSMTP.Connect;
FIdSMTP.Send(FIdMessage);
except
on E: Exception do
ShowMessage(E.Message);
end;
您是否看到任何明显可能导致其不发送电子邮件的内容?
答案 0 :(得分:3)
谢谢你,雷米!美丽小费!此代码现在有效:
FIdSMTP := TIdSMTP.Create;
FIdSSLIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
FIdMessage := TIdMessage.Create;
FIdAttachment := TIdAttachmentFile.Create(FIdMessage.MessageParts);
FIdSASLCRAMSHA1 := TIdSASLCRAMSHA1.Create;
FIdSASLCRAMMD5 := TIdSASLCRAMMD5.Create;
FIdSASLDigest := TIdSASLDigest.Create;
FIdSASLSKey := TIdSASLSKey.Create;
FIdSASLOTP := TIdSASLOTP.Create;
FIdSASLAnonymous := TIdSASLAnonymous.Create;
FIdSASLExternal := TIdSASLExternal.Create;
FIdSASLLogin := TIdSASLLogin.Create;
FIdSASLPlain := TIdSASLPlain.Create;
if (FEmailServer.SMTPUseSSL) then
begin
FIdSSLIOHandler.SSLOptions.Method := sslvTLSv1;
// FIdSSLIOHandler.SSLOptions.Method := sslvSSLv3;
FIdSMTP.IOHandler := FIdSSLIOHandler;
FIdSMTP.UseTLS := utUseExplicitTLS;
// FIdSMTP.UseTLS := utUseImplicitTLS;
end;
FIdSMTP.Host := FEmailServer.SMTPHost; //smtp.gmail.com
FIdSMTP.Port := FEmailServer.SMTPPort; //587
FIdSMTP.AuthType := satSASL;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLCRAMSHA1;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLCRAMMD5;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLDigest;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLSKey;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLOTP;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLAnonymous;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLExternal;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLLogin;
FIdSMTP.SASLMechanisms.Add.SASL := FIdSASLPlain;
FIdSMTP.Username := FEmailServer.SMTPUserName;
FIdSMTP.Password := FEmailServer.SMTPPassword;
FIdMessage.Recipients[0].Address := mailmessage.RecipientEmailAddr;
FIdMessage.From.Name := mailmessage.SenderName;
FIdMessage.Subject := mailmessage.EmailSubject;
FIdMessage.Body.Text := mailmessage.EmailMessage;
FIdAttachment.LoadFromFile(mailmessage.AttachmentFile);
FIdAttachment.FileName := ExtractFileName(mailmessage.AttachmentFile);
try
if (not FIdSMTP.Connected) then
FIdSMTP.Connect;
FIdSMTP.Send(FIdMessage);
except
on E: Exception do
ShowMessage(E.Message);
end;
释放对象:
FIdAttachment.Free;
FIdMessage.Free;
FIdSSLIOHandler.Free;
FIdSMTP.Free;
FIdSASLCRAMSHA1.Free;
FIdSASLCRAMMD5.Free;
FIdSASLDigest.Free;
FIdSASLSKey.Free;;
FIdSASLOTP.Free;
FIdSASLAnonymous.Free;
FIdSASLExternal.Free;
FIdSASLLogin.Free;
FIdSASLPlain.Free;
..使用清单:
使用Classes,SysUtils,Windows,IdSMTP,IdMessage,IdSSLOpenSSL,
IdExplicitTLSClientServerBase,IdAttachmentFile,IdSASL, IdSASLCollection,IdSASLAnonymous,IdSASLDigest,IdSASLExternal, IdSASLLogin,IdSASLOTP,IdSASLPlain,IdSASLSKey,IdSASLUserPass,
IdSASL_CRAM_MD5,IdSASL_CRAM_SHA1;