on 08-23-2006 5:45 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Have you provided the correct SMTP server name? From your post it's "mail.enteg.com".
Regards,
Satyajit.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
95 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.