Free Support Forum - aspose.com

Send Email Aspose.email in java problem with smtp


#1

I have a Aspose.total license. i am trying to send an Email in java using aspose.email. Here is the sample code i am referring to
SmtpClient client = new SmtpClient(“smtp.gmail.com”, 587, “username(my gmail username)”, “password”);

// Set Security options for the server
client.setSecurityOptions(SecurityOptions.Auto);

// Create a new Email Message
MailMessage msg = new MailMessage("senderemail@gmail.com", "recieveremail@gmail.com", “First Message from Java”, “Body”);
System.out.println(“Sending message…”);

// Send the Message now
client.send(msg);

but here is the issue i am facing.
Exception in thread “main” AsposeException: ConnectFailure
—> AsposeException: ConnectFailure
—> class com.aspose.email.system.exceptions.IOException: ConnectFailure —> class com.aspose.email.system.exceptions.SocketException: No connection could be made because the target machine actively refused it.
com.aspose.email.internal.q.g.b(Unknown Source)
com.aspose.email.asx.a(SourceFile:314)
com.aspose.email.asx.a(SourceFile:219)
com.aspose.email.asx.a(SourceFile:159)
com.aspose.email.en.a(SourceFile:89)
com.aspose.email.en.a(SourceFile:75)
com.aspose.email.hl.a(SourceFile:128)
com.aspose.email.atq.a(SourceFile:280)
com.aspose.email.atq.b(SourceFile:247)
com.aspose.email.atq.a(SourceFile:27)
com.aspose.email.atr.a(SourceFile:150).

Can you please tell me how to fix this issue.
My main concern is to send an email with attachment. It will be very helpful if you could send the sample code to send an email with attachment. Hope i will get the fast response.


#2

Hi Support team,

I expected a fast reply.


#3

@kishan_yalamaddi,

We have tested this functionality at our end and didn’t face any issue sending out email. Please make sure that you have allowed less secure apps to connect to your Gmail account in Gmail settings. Otherwise, it will not allow your application to connect to your mail account. If the issue still persists, please share a test account credentials with us that we can use to connect and verify the same problem at our end.

Please go through, Working with Attachments, to know further about adding attachments to a MailMessage for sending out in email.


#4

Thanks for the reply and i have looked into the Attachments.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the resource directory.
String dataDir = Utils.getSharedDataDir(AddAttachmentToANewEmailMessage.class) + “email/”;

//Create an instance of MailMessage class
MailMessage message = new MailMessage();

//From
message.setFrom(new MailAddress("sender@sender.com"));

//To
message.getTo().addMailAddress(new MailAddress("receiver@gmail.com"));

//Adding 1st attachment
//Create an instance of Attachment class
Attachment attachment;

//Load an attachment
attachment = new Attachment(dataDir + “1.txt”);

//Add attachment in instance of MailMessage class
message.getAttachments().addItem(attachment);

//Add 2nd Attachment
message.getAttachments().addItem(new Attachment(dataDir + “1.jpg”));

//Add 3rd Attachment
message.getAttachments().addItem(new Attachment(dataDir + “1.doc”));

//Add 4th Attachment
message.getAttachments().addItem(new Attachment(dataDir + “1.rar”));

//Add 5th Attachment
message.getAttachments().addItem(new Attachment(dataDir + “1.pdf”));

//Save message to disc
message.save(dataDir + “AddAttachmentToANewEmailMessage_out.msg”, SaveOptions.getDefaultMsg());

Here we are saving the .msg file in the given directory with to and from address. how to email this .msg file ?


#5

@kishan_yalamaddi,

You may please initialize the smtpclient and send the message. There is no need to save the message on disc. Once you attach the last attachment, then send the message using smtp client. Please give a try to the following sample code and share the feedback.

...
...
//Add 5th Attachment
message.getAttachments().addItem(new Attachment(dataDir + "1.pdf"));

SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "sender", "password");
client.send(message);

#6

Thanks for the reply that worked. Here is the code is used

	MailMessage message = new MailMessage();

//From
	message.setFrom(new MailAddress("sender@sender.com"));

	//To
	message.getTo().addMailAddress(new MailAddress("receiver@gmail.com"));
	//Adding 1st attachment
	//Create an instance of Attachment class
	Attachment attachment;

	//Load an attachment
	attachment = new Attachment("C://temp1/B.pdf");

	//Add attachment in instance of MailMessage class
	message.getAttachments().addItem(attachment);
	
	SmtpClient client = new SmtpClient("smtp.outlook.com", 587, "username", "password");
	
        	client.send(message);

It is working fine with the normal internet but when i connected to my office network it is giving exception. What will be reason behind it. here is the Exception. Please what should i do from my end to get rid of this issue. the code above is working fine with normal internet.

Exception in thread “main” AsposeException: ConnectFailure
—> AsposeException: ConnectFailure
—> class com.aspose.email.system.exceptions.IOException: ConnectFailure —> class com.aspose.email.system.exceptions.SocketException: No connection could be made because the target machine actively refused it.
com.aspose.email.internal.q.g.b(Unknown Source)
com.aspose.email.asx.a(SourceFile:314)
com.aspose.email.asx.a(SourceFile:219)
com.aspose.email.asx.a(SourceFile:159)
com.aspose.email.en.a(SourceFile:89)
com.aspose.email.en.a(SourceFile:75)
com.aspose.email.hl.a(SourceFile:128)
com.aspose.email.atq.a(SourceFile:280)
com.aspose.email.atq.b(SourceFile:247)
com.aspose.email.atq.a(SourceFile:27)
com.aspose.email.atr.a(SourceFile:150)
com.aspose.email.axk.a(SourceFile:193)
com.aspose.email.internal.af.g.a(Unknown Source)
com.aspose.email.internal.af.h.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
— End of inner exception stack trace —
com.aspose.email.asx.a(SourceFile:167)
com.aspose.email.en.a(SourceFile:89)
com.aspose.email.en.a(SourceFile:75)
com.aspose.email.hl.a(SourceFile:128)
com.aspose.email.atq.a(SourceFile:280)
com.aspose.email.atq.b(SourceFile:247)
com.aspose.email.atq.a(SourceFile:27)
com.aspose.email.atr.a(SourceFile:150)
com.aspose.email.axk.a(SourceFile:193)
com.aspose.email.internal.af.g.a(Unknown Source)
com.aspose.email.internal.af.h.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
— End of inner exception stack trace —
[Ljava.lang.StackTraceElement;@6279cee3
— End of inner exception stack trace —
[Ljava.lang.StackTraceElement;@4206a205
at com.aspose.email.atq.a(SourceFile:176)
at com.aspose.email.hp.g(SourceFile:648)
at com.aspose.email.hp.B(SourceFile:584)
at com.aspose.email.hp.a(SourceFile:565)
at com.aspose.email.SmtpClient.beginSend(SourceFile:1433)
at com.aspose.email.SmtpClient.send(SourceFile:1616)
at com.aspose.email.SmtpClient.send(SourceFile:1520)
at com.wk.gbs.dc.util.outlook.main(outlook.java:45)
Caused by: AsposeException: ConnectFailure
—> class com.aspose.email.system.exceptions.IOException: ConnectFailure —> class com.aspose.email.system.exceptions.SocketException: No connection could be made because the target machine actively refused it.
com.aspose.email.internal.q.g.b(Unknown Source)
com.aspose.email.asx.a(SourceFile:314)
com.aspose.email.asx.a(SourceFile:219)
com.aspose.email.asx.a(SourceFile:159)
com.aspose.email.en.a(SourceFile:89)
com.aspose.email.en.a(SourceFile:75)
com.aspose.email.hl.a(SourceFile:128)
com.aspose.email.atq.a(SourceFile:280)
com.aspose.email.atq.b(SourceFile:247)
com.aspose.email.atq.a(SourceFile:27)
com.aspose.email.atr.a(SourceFile:150)
com.aspose.email.axk.a(SourceFile:193)
com.aspose.email.internal.af.g.a(Unknown Source)
com.aspose.email.internal.af.h.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
— End of inner exception stack trace —
com.aspose.email.asx.a(SourceFile:167)
com.aspose.email.en.a(SourceFile:89)
com.aspose.email.en.a(SourceFile:75)
com.aspose.email.hl.a(SourceFile:128)
com.aspose.email.atq.a(SourceFile:280)
com.aspose.email.atq.b(SourceFile:247)
com.aspose.email.atq.a(SourceFile:27)
com.aspose.email.atr.a(SourceFile:150)
com.aspose.email.axk.a(SourceFile:193)
com.aspose.email.internal.af.g.a(Unknown Source)
com.aspose.email.internal.af.h.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
— End of inner exception stack trace —
[Ljava.lang.StackTraceElement;@57175e74
at com.aspose.email.atq.b(SourceFile:263)
at com.aspose.email.atq.a(SourceFile:27)
at com.aspose.email.atr.a(SourceFile:150)
at com.aspose.email.axk.a(SourceFile:193)
at com.aspose.email.internal.af.g.a(Unknown Source)
at com.aspose.email.internal.af.h.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: class com.aspose.email.system.exceptions.IOException: ConnectFailure —> class com.aspose.email.system.exceptions.SocketException: No connection could be made because the target machine actively refused it.
com.aspose.email.internal.q.g.b(Unknown Source)
com.aspose.email.asx.a(SourceFile:314)
com.aspose.email.asx.a(SourceFile:219)
com.aspose.email.asx.a(SourceFile:159)
com.aspose.email.en.a(SourceFile:89)
com.aspose.email.en.a(SourceFile:75)
com.aspose.email.hl.a(SourceFile:128)
com.aspose.email.atq.a(SourceFile:280)
com.aspose.email.atq.b(SourceFile:247)
com.aspose.email.atq.a(SourceFile:27)
com.aspose.email.atr.a(SourceFile:150)
com.aspose.email.axk.a(SourceFile:193)
com.aspose.email.internal.af.g.a(Unknown Source)
com.aspose.email.internal.af.h.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
— End of inner exception stack trace —
com.aspose.email.asx.a(SourceFile:167)
com.aspose.email.en.a(SourceFile:89)
com.aspose.email.en.a(SourceFile:75)
com.aspose.email.hl.a(SourceFile:128)
com.aspose.email.atq.a(SourceFile:280)
com.aspose.email.atq.b(SourceFile:247)
com.aspose.email.atq.a(SourceFile:27)
com.aspose.email.atr.a(SourceFile:150)
com.aspose.email.axk.a(SourceFile:193)
com.aspose.email.internal.af.g.a(Unknown Source)
com.aspose.email.internal.af.h.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
at com.aspose.email.asx.a(SourceFile:167)
at com.aspose.email.en.a(SourceFile:89)
at com.aspose.email.en.a(SourceFile:75)
at com.aspose.email.hl.a(SourceFile:128)
at com.aspose.email.atq.a(SourceFile:280)
at com.aspose.email.atq.b(SourceFile:247)
… 6 more
Caused by: class com.aspose.email.system.exceptions.SocketException: No connection could be made because the target machine actively refused it.
com.aspose.email.internal.q.g.b(Unknown Source)
com.aspose.email.asx.a(SourceFile:314)
com.aspose.email.asx.a(SourceFile:219)
com.aspose.email.asx.a(SourceFile:159)
com.aspose.email.en.a(SourceFile:89)
com.aspose.email.en.a(SourceFile:75)
com.aspose.email.hl.a(SourceFile:128)
com.aspose.email.atq.a(SourceFile:280)
com.aspose.email.atq.b(SourceFile:247)
com.aspose.email.atq.a(SourceFile:27)
com.aspose.email.atr.a(SourceFile:150)
com.aspose.email.axk.a(SourceFile:193)
com.aspose.email.internal.af.g.a(Unknown Source)
com.aspose.email.internal.af.h.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
at com.aspose.email.internal.q.g.b(Unknown Source)
at com.aspose.email.asx.a(SourceFile:314)
at com.aspose.email.asx.a(SourceFile:219)
at com.aspose.email.asx.a(SourceFile:159)
… 11 more


#7

@kishan_yalamaddi,

Thank you for writing to Aspose support team again. It seems to be some issue with the configuration of the mail server deployed in your local network. Please confirm the smtp server configuration like smtp server name, security options, authentication method and port number from your mail server administrator. Once confirmed, try to send mail through any third party client like Outlook or Mozilla Thunderbird using these configurations provided by the mail server administrator. Once you are successful, use these configurations in the code mentioned above and share the feedback.


#8

Thank you so much for helping me out. it worked for by changing the Host Name and Port.

Also, i checked the API’s but i didn’t find it. Can you please tell me how to convert the rtf to html and send it as email body.


#9

@kishanaasa,

There is no direct method to convert RTF to HTML using Aspose.Email API. You can, however, use MapiMessage.SetBodyContent to set the message’s RTF body and then convert it to MailMessage for sending via email client.

mapiMsg.SetBodyContent("", BodyContentType.Rtf);

MailMessage eml = msg.ToMailMessage(new MailConversionOptions());

Now, you can send this EML using email client.

If using Aspose.Words API is an option at your end, you can use following sample code to convert RTF to HTML.

Document doc = new Document("Input.rtf");

doc.Save("Output.html");

#10

@kashif.iqbal

I tried but couldn’t get it. Here is the code i am using.

MapiMessage mapi = new MapiMessage();

mapi.setBodyContent( “C://temp1/sample.rtf”, BodyContentType.Rtf);

MailMessage eml = mapi.toMailMessage(new MailConversionOptions());

eml.setFrom(new MailAddress("from@domain.com"));

eml.getTo().addMailAddress(new MailAddress("to@domain.com"));

SmtpClient client = new SmtpClient( HostName, port , username,
password);

client.setSecurityOptions(SecurityOptions.Auto);

client.send(eml);

I am able to get an email but i am not getting the rtf content. i am just getting the rtf path (C://temp1/sample.rtf) in the body.

Please help to to solve this. Thanks in advance.


#11

@kishanaasa,

setBodyContent doesn’t take file path as input, but RTF content itself. Please load the text contents and pass it on to this method for working.