cancel
Showing results for 
Search instead for 
Did you mean: 

Using SMTP to send mail

Former Member
0 Kudos

Hi,

'm trying to send mails from my webdynpro application, and I used SMTP. for that i downloaded jscape.inetfactory. and the following pgm i wrote.

No compilation error.. But exception comes.. it is given at the end...

-


CODE----


import com.jscape.inet.smtp.*;

import com.jscape.inet.mime.*;

import com.jscape.inet.email.*;

import java.io.*;

public static void main(String[] args) {

}

public class Testsmtp extends SmtpAdapter {

public void sendMessage(String hostname, String to, String from, String subject, String body, String attachment) throws SmtpException,

IOException, MimeException {

// create new Smtp instance

Smtp smtp = new Smtp(hostname);

// enable debugging

smtp.setDebug(true);

// register this class to capture SMTP related events

smtp.addSmtpListener(this);

// connect to SMTP server

smtp.connect();

// create email message

EmailMessage email = new EmailMessage();

email.setTo(to);

email.setFrom(from);

email.setSubject(subject);

email.setBody(body);

// add attachment to email message

//email.addAttachment(new Attachment(new File(attachment)));

// send email message

smtp.send(email);

// disconnect from SMTP server

smtp.disconnect();

}

// capture connect event

public void connected(SmtpConnectedEvent evt) {

System.out.println("Connected to SMTP server: " + evt.getHostname());

}

// capture disconnect event

public void disconnected(SmtpDisconnectedEvent evt) {

System.out.println("Disconnected from SMTP server: " + evt.getHostname());

}

public static void main(String[] args) {

String hostname;

String to;

String from;

String subject;

String body;

String attach;

try {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter SMTP hostname (e.g. mail.domain.com): ");

hostname = reader.readLine();

System.out.print("Enter To address (e.g. recipient@domain.com): ");

to = reader.readLine();

System.out.print("Enter From address (e.g. sender@domain.com): ");

from = reader.readLine();

subject = "iNet Factory Attachment Example";

body = "see attached image";

attach = "image.gif";

Testsmtp example = new Testsmtp();

example.sendMessage(hostname,to,from,subject,body,attach);

System.out.println("sent");

}

catch(Exception e) {

e.printStackTrace();

}

}

}

-


Output-Exception----


Enter SMTP hostname (e.g. mail.domain.com): mail.enteg.com

Enter To address (e.g. recipient@domain.com): smitha.s@enteg.com

Enter From address (e.g. sender@domain.com): jesmila.s@enteg.com

com.jscape.inet.smtp.SmtpException: an I/O error occured: Could not connect for 30000 milliseconds

at com.jscape.inet.smtp.Smtp.connect(Unknown Source)

at Testsmtp.sendMessage(Testsmtp.java:36)

at Testsmtp.main(Testsmtp.java:84)

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

hi

If the exception is unknown source then check for the hostname that you are providing.You can also provide the IP address of the SMTP/POP server instead of the hostname.

some SMTP Server also require authentication.in your code i didn`t find any property for Username & password that is being supplied,so i assume it is not required as you have already signed on to the application.Also check if the SMTP server that you are trying to connect allows connection directly.If it doesnot allow then contact your system admin & ask him to allow requests comming from your machine i.e IP address to access the server.

suresh_krishnamoorthy
Active Contributor
0 Kudos

Hi Smita Nair,

If you already aware this link avoid it.

Regards, Suresh KB

Former Member
0 Kudos

Thanks all..

I'll try it and 'll get back to you..

regards

smitha