Getting exceptions (SmtpException, IOException, SocketException) while sending email

Below is the sample code,

Blob emlBlob = emailData.getMessageEml(); //blob data from Data base
InputStream emlInputStream = emlBlob.getBinaryStream();

MailMessage eml = MailMessage.load(emlInputStream); //step 1
MailMessage eml = MailMessage.load((“D:/Ershad/mailWithAttachment.eml”)); //step 2

SmtpClient client = new SmtpClient(host, port);
client.setSecurityOptions(SecurityOptions.Auto);

client.send(eml);

If using step 2, using directly eml file, iam able send the email successfully
If using step 1, ie when loading the input stream to mail message i am getting the below exception,

Failure sending mail. ERROR_DETAILS: com.aspose.email.SmtpException: Failure sending mail.
at com.aspose.email.SmtpClient.a(SourceFile:1835)
at com.aspose.email.SmtpClient.send(SourceFile:1638)

Caused by: com.aspose.email.system.exceptions.IOException: Read failure
at com.aspose.email.internal.q.f.read(Unknown Source)
at com.aspose.email.ec.read(SourceFile:248)
at com.aspose.email.gv.b(SourceFile:168)
at com.aspose.email.gv.a(SourceFile:160)
at com.aspose.email.aug.r(SourceFile:317)
at com.aspose.email.aug.n(SourceFile:126)
at com.aspose.email.cq.a(SourceFile:324)

Caused by: com.aspose.email.system.exceptions.SocketException: An existing connection was forcibly closed by the remote host.
at com.aspose.email.internal.q.g.c(Unknown Source)
at com.aspose.email.internal.q.g.a(Unknown Source)

Please help me in this case

@ershad.ics3,

We have tested this scenario using the latest version of Aspose.Email for Java 17.8 and are not able to reproduce the issue at our end. The message loaded from Input stream is sent out without any issue. We have used the following code sample at our end. Please give it a try with the latest version of the API and if the issue still persists, please share your sample EML file with us for further investigation at our end.

Sample Code

MailMessage eml = MailMessage.load("845521\\319.eml");
	
ByteArrayOutputStream stream = new ByteArrayOutputStream();

eml.save(stream);

ByteArrayInputStream inStream = new ByteArrayInputStream(stream.toByteArray());

MailMessage loadedStream = MailMessage.load(inStream);

SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "username", "password");

client.setSecurityOptions(SecurityOptions.Auto);

client.send(loadedStream);

Hi,

Thanks for reply.
We have saved email in database in blob format.
Now while retrieving eml, which is in blob format type, first converted into input stream and then trying to load it into mail message.
There is no .eml file physically saved…
Just for testing purpose, i have tried sending email bu loading sample eml file.

So without .eml file, can we load email content stored in database in blob by converting it into inputstream and load it to mailmessage to send email?

InputStream emlInputStream = emlBlob.getBinaryStream();

MailMessage eml = MailMessage.load(emlInputStream);

Please help to resolve this issue.

@ershad.ics3,

Aspose.Email can read MSG from disc file as well as from memory stream. This memory stream can be blob data from some database or stream of data received on network.

Can you please save the blob to disc in a .EML or .MSG (whichever format it was stored in DB) file and check if it opens fine in MS Outlook? This will verify if the mail structure is valid or not. Share the sample file with us as well for further investigation at our end.

Following is a sample code which generates memory stream by some mean say reading bytes from a file. This output memory stream is further used to create a mail message as given below:

MemoryStream memStream = new MemoryStream(File.ReadAllBytes(@“message.msg”));
memStream.Position = 0;

//Here MSG is read from memory stream
MailMessage mail = MailMessage.Load(memStream);
Console.WriteLine(“Subjet = {0}”, mail.Subject);

Hi,

I have converted blob to .eml file.PLease find the attached zip file containing .eml file.

.Test.zip (2.5 KB)

i have used below code for converting input stream to eml file,

Blob emlBlob = emailData.getMessageEml();
** InputStream emlInputStream = emlBlob.getBinaryStream();**
** OutputStream out = new FileOutputStream(“D:/NewFolder/Test.eml”);**
** byte[] buff = emlBlob.getBytes(1,(int)emlBlob.length());**
** out.write(buff);**
** out.close();**

Even while loading this .eml file, i am getting same exception as given in previous posts.

Also, can you please tell how to load mail message with only input stream(by not saving as eml/msg file) and send email.
I was using only below steps to load and then send email but getting exceptions,

Blob emlBlob = emailData.getMessageEml();
InputStream emlInputStream = emlBlob.getBinaryStream();
MailMessage eml = MailMessage.load(emlInputStream, new EmlLoadOptions());

can you please help us in resolving the issue

Can you please let us know the solution, if we don’t have the place to save the file(eml file) on cloud.
Without saving as eml file, can we load the email only by data(blob format) in database and send the email?

Please help us to resolve the issue.
Thanks.

@ershad.ics3,

As you mentioned that even if you load this file (test.eml) into MailMessage, you get the same exception. This shows that issue is not there in syntax of reading file into inputstream and then loading it into MailMessage. There is some problem with the mail message which needs to be analyzed.

  1. You may open this saved mail (test.eml) in some mail client like thunderbird or outlook and try to send it using any of these clients and share if mail is sent successfully or not.
  2. Try some other mails from the Database and share if issue is with all the mails or with this specific email.
  3. For simplifying the problem, please try following sample code which does not save any thing on disc and performs the following.
  • Create a simple email

  • Store it into InputStream which is equivalent to reading mail from database

  • Load this input stream into MailMessage and send it via SmtpClient

    MailMessage eml = new MailMessage(“user@domain.com”,“user@domain.com”,“Test Subject”, “Test Body”);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    eml.save(stream);
    ByteArrayInputStream inStream = new ByteArrayInputStream(stream.toByteArray());
    MailMessage loadedStream = MailMessage.load(inStream);
    SmtpClient client = new SmtpClient(“smtp.domain.com”, 587, “user”, “password”);
    client.setSecurityOptions(SecurityOptions.Auto);
    client.send(loadedStream);

Please give a try to all these steps and share the feedback. Also if possible, please arrange some test account on the said server and share the credentials with us for testing. It will help us to observe the problem and provide assistance accordingly.

We are sorry for the inconvenience caused to you in this regard.

Please find below analysis done by me on 1,2,3 points discussed in the previous message,

  1. I have opened test.eml using outlook and tried sending it. Mail was sent successfully. So there is NO issue with eml file.
    But while loading the same eml file into mailMessage using below steps i am getting smtpexception/ioexception/socketexception

      MailMessage eml = new MailMessage();
      eml = MailMessage.load(("D:/Test.eml"));
      eml.setFrom(new MailAddress("FromMail@domain.com"));
      eml.setSubject(emailData.getSubject());
      MailAddressCollection recipients = new MailAddressCollection();
      recipients.addItem(new MailAddress("ToMail@domain.com"));
      eml.setTo(recipients);
      SmtpClient client = new SmtpClient(host, port);
      client.setSecurityOptions(SecurityOptions.Auto);
      client.send(eml);
    
  2. Tried other examples (mails saved as blob) in DB as well. I am getting same issue for all.

  3. The sample code(shared by you) is working properly. I am able to send the email.

Briefing you on the requirement again,

eml data (blob format in DB) ----> convert to input stream ----> loading the mail -----> send email using smtp client.

I have tried below 2 ways to send email from blob data format without saving the data physically in eml format.

Trail-1

   MailMessage eml = new MailMessage();
   Blob emlBlob = emailData.getMessageEml(); //getting data from DB and saving in 'emlBlob'
   InputStream emlInputStream = emlBlob.getBinaryStream();
   eml = MailMessage.load((emlInputStream ));
   eml.setFrom(new MailAddress("FromMail@domain.com"));
   eml.setSubject(emailData.getSubject());
   MailAddressCollection recipients = new MailAddressCollection();
   recipients.addItem(new MailAddress("ToMail@domain.com"));
   eml.setTo(recipients);
   SmtpClient client = new SmtpClient(host, port);
   client.setSecurityOptions(SecurityOptions.Auto);
  client.send(eml);

Trail-2

 MailMessage eml = new MailMessage();
 Blob emlBlob = emailData.getMessageEml();
 long blobLength = emlBlob.length();
 byte[] bdata = emlBlob.getBytes(1, (int) blobLength);
 ByteArrayInputStream bytestream= new ByteArrayInputStream(bdata);
 eml = MailMessage.load(bytestream);
 eml.setFrom(new MailAddress("FromMail@domain.com"));
 eml.setSubject(emailData.getSubject());
 MailAddressCollection recipients = new MailAddressCollection();
 recipients.addItem(new MailAddress("ToMail@domain.com"));
 eml.setTo(recipients);
 SmtpClient client = new SmtpClient(host, port);
 client.setSecurityOptions(SecurityOptions.Auto);
 client.send(eml);

Both above trails are failing with same smtpexception/ioexception/socketexception.

Can you suggest solution here.

@ershad.ics3,

Could you please share with us which database are you using? We also need your sample code for inserting and retreiving data from the database. Also, please confirm to us if you are using the latest version of the Aspose.Email for Java API at your end? If possible, please share a sample runnable application with us that we can use at our end for reproducing the issue. This will help us investigate the issue and assist you further.

When we executed the code in other region. email was sent without any exception.
Looks like firewall or security blockage for email loaded with eml (blob data).
Thanks for your support.

@ershad.ics3,

Thank you for your feedback and feel free to write us back if you have any other query related to Aspose.Email.

My local machine is Windows 7 OS and Dev server is Linux.
When i send sample email with just subject, body, (without loading input stream) i am able send email in both my local machine and linux server.
But when i load mail message with input stream and send the email using SMTP client, iam getting above exceptions(smtp, io, socket exceptions) in my local machine. But email is sent in Linux server.

Are there any restrictions on sending email while loading with input stream?
There is no firewall issue in my local machine, because i am able send email without loading input stream.

Please let us know.

@ershad.ics3,

There is no such restriction or limitation on sending email while loading with input stream. Had this been so, the API won’t work on Linux server as well. Could you please check it on another Windows Machine just to verify if it works or not? Please also share which database are you working with and the code for insertion of message file to the database.