• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending an email in JSP

 
john latham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Im trying to send an email in a jsp page, you can see what ive done below. Currently im not entering any username or password for my gmail account - where/how can i do this, also can i send emails using my gmail in a jsp page.


[ May 29, 2006: Message edited by: john latham ]
 
john latham
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error that i seem to be getting is as follows:


Any ideas?
 
Sunesh Kumar
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

why cant you try this...
Mailing with attachment
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;

public final class MailerBean extends Object implements Serializable {

/* Bean Properties */
private String to = null;
private String from = null;
private String subject = null;
private String message = null;
public static Properties props = null;
public static Session session = null;

static {
/*Setting Properties for STMP host */
props = System.getProperties();
props.put("mail.smtp.host", "ideal-solutions.co.in");
//session = Session.getDefaultInstance(props, null);
session = Session.getInstance(props, null);
}
/* Setter Methods */
public void setTo(String to) {
this.to = to;
}

public void setFrom(String from) {
this.from = from;
}

public void setSubject(String subject) {
this.subject = subject;
}

public void setMessage(String message) {
this.message = message;
}
/* Sends Email */
public void sendMail() throws Exception {
if(!this.everythingIsSet())
throw new Exception("Could not send email.");
try {
MimeMessage message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(this.to));
message.setFrom(new InternetAddress(this.from));
message.setSubject(this.subject);
message.setText(this.message);
Transport.send(message);
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;

public final class MailerBean extends Object implements Serializable {

/* Bean Properties */
private String to = null;
private String from = null;
private String subject = null;
private String message = null;
public static Properties props = null;
public static Session session = null;

static {
/*Setting Properties for STMP host */
props = System.getProperties();
props.put("mail.smtp.host", "ideal-solutions.co.in");
//session = Session.getDefaultInstance(props, null);
session = Session.getInstance(props, null);
}
/* Setter Methods */
public void setTo(String to) {
this.to = to;
}

public void setFrom(String from) {
this.from = from;
}

public void setSubject(String subject) {
this.subject = subject;
}

public void setMessage(String message) {
this.message = message;
}
/* Sends Email */
public void sendMail() throws Exception {
if(!this.everythingIsSet())
throw new Exception("Could not send email.");
try {
MimeMessage message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(this.to));
message.setFrom(new InternetAddress(this.from));
message.setSubject(this.subject);
message.setText(this.message);
Transport.send(message);
} catch (MessagingException e) {
throw new Exception(e.getMessage());
}
}

/* Checks whether all properties have been set or not */
private boolean everythingIsSet() {
if((this.to == null) || (this.from == null) ||
(this.subject == null) || (this.message == null))
return false;

if((this.to.indexOf("@") == -1) ||
(this.to.indexOf(".") == -1))
return false;

if((this.from.indexOf("@") == -1) ||
(this.from.indexOf(".") == -1))
return false;

return true;
}
}

NOTE:
u must have activation.jar and mail.jar in you lib folder..ok
hope this helps

Bye...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to other Java APIs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic