SmtpClient.send Method Throws "Object Has Been Disposed" Exception

We are seeing an exception “Object has been disposed” when calling com.aspose.email.SmtpClient.send. Using aspose.email for java 17.4.0.

public SmtpClient getSmtpClient() {
if (smtpClient == null) {
smtpClient = new SmtpClient(propertiesUtils.getSmtpClientHost(),
propertiesUtils.getSmtpClientPort(), propertiesUtils.getSmtpClientUserName(), propertiesUtils.getSmtpClientPassword(),
SecurityOptions.Auto);
}
return smtpClient;
}

public void send(MailMessage emailMessage){
try {
getSmtpClient().send(emailMessage);

Any ideas what we might be doing wrong?

com.comcast.sa.esl.emailoutbound.services.SmtpEmailClient.send - Exception in send: Object has been disposed. ERROR_DETAILS: com.aspose.email.AsposeException: Object has been disposed. at com.aspose.email.cm.b(SourceFile:146) at com.aspose.email.gv.a(SourceFile:95) at com.aspose.email.cy.a(SourceFile:375) at com.aspose.email.cy.w(SourceFile:350) at com.aspose.email.SmtpClient.a(SourceFile:1864) at com.aspose.email.SmtpClient.a(SourceFile:1819) at com.aspose.email.SmtpClient.send(SourceFile:1638) at com.comcast.sa.esl.emailoutbound.services.SmtpEmailClient.send(SmtpEmailClient.java:77) at com.comcast.sa.esl.emailoutbound.services.EmailOutboundService.process(EmailOutboundService.java:323) at com.comcast.sa.esl.emailoutbound.services.EmailOutboundService$$FastClassBySpringCGLIB$$10450722.invoke() at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)

@mlittle4444,
Thank you for contacting support. There were many updates after version 17.4. Please use the latest version of Aspose.Email.

Hi Andrey,

I realized that, its probably going to take me a couple weeks to get the new version to production. I have also decided to make the smtpclient object live on the methods local stack as its not a super high volume situation… But if by chance you had seen something like this and had and suggestions as I prepare a version of code to deploy to production let me know. thanks, Mike

@mlittle4444,
With Aspose.Email 17.4, I was able to send a message by using SmtpClient. You can try to use the following code snippet in a separate simple project to check the problem:

SmtpClient smtpClient = new SmtpClient(
    host, port, username, password, SecurityOptions.Auto);

smtpClient.setTimeout(30000);

MailMessage message = new MailMessage();
message.setFrom(MailAddress.to_MailAddress(username));
message.getTo().add(receiverAddress);
message.setSubject("Test SMTP");
message.setBody("Test");

smtpClient.send(message);

I am assuming your SmtpClient class instance has been disposed before sending an email message. You should check your application code carefully.

Documents: Connecting to SMTP Server, Sending and Forwarding Messages
API Reference: SmtpClient Class

1 Like

Thank you for your extra help here!