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 (1)

Accepted Solutions (1)

Former Member
0 Kudos

Smitha,

Try using this.


public string sendMail (string from, string to, string cc, string bcc, string subject, string body) {
 // Mail initialization 
MailMessage mail = new MailMessage();
 mail.From = from;
 mail.To = to;
 mail.Cc = cc;
 mail.Bcc = bcc;
 mail.Subject = subject;
 mail.BodyFormat = MailFormat.Text;
 mail.Body = body;
 // Smtp configuration
 SmtpMail.SmtpServer = "smtp.gmail.com";
 // - smtp.gmail.com use smtp authentication
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myemail@gmail.com");
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "mypassword");
 // - smtp.gmail.com use port 465 or 587
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
 // - smtp.gmail.com use STARTTLS (some clients call this SSL)
 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
 // Mail sending
 try {
   SmtpMail.Send(mail);
   return "";
 } catch (Exception ex) {
   return ex.Message;
} 

Bala

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

Have you provided the correct SMTP server name? From your post it's "mail.enteg.com".

Regards,

Satyajit.

vijayakhanna_raman
Active Contributor
0 Kudos

Hi,

I have used this code and i was able to send mail with attachments Sucessfully.

1)U should give the name of host(mail server) correctly i.e. where the mail is sent.

2)Add the jar mail.jar ans activation.jar.

3)Ur webas server and mail server should have connection.

4)Write this code in any action event handler

Properties p = new Properties();

p.put("mail.transport.protocol", "smtp");

p.put("mail.smtp.host", "host name");

p.put("mail.smtp.port", "25");

Session session = Session.getInstance(p);

session.setDebug(false);

// Define message

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress("fromadress"));

message.addRecipient( Message.RecipientType.TO, new InternetAddress(toadress));

message.setSubject("Hi ");

MimeBodyPart messageBodyPart = new MimeBodyPart();

//fill message

messageBodyPart.setText("PFA");

Multipart multipart = new MimeMultipart();

multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

// Part two is attachment

messageBodyPart = new MimeBodyPart();

String fileAttachment="path ot attachment file";

DataSource source = new FileDataSource(fileAttachment);

messageBodyPart.setDataHandler(new DataHandler(source));

messageBodyPart.setFileName("xxx");

multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

// Send message

Transport.send(message);

} catch (Exception e) {

wdComponentAPI.getMessageManager().raiseException("Exception"+e,true);

}

Hope this helps,

REgards,

Vijayakhanna Raman

Former Member
0 Kudos

Thanks all..

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

regards

smitha