Two Laptop Bag*
The moose likes Other JSE/JEE APIs and the fly likes Sending an email in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Sending an email in JSP" Watch "Sending an email in JSP" New topic
Author

Sending an email in JSP

john latham
Greenhorn

Joined: May 28, 2006
Posts: 22
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

Joined: May 28, 2006
Posts: 22
The error that i seem to be getting is as follows:


Any ideas?
Sunesh Kumar
Ranch Hand

Joined: Oct 17, 2005
Posts: 89
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...


Thanks & Regards<br />Sunesh Kumar Baachu
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Moved to other Java APIs.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending an email in JSP
 
Similar Threads
Sending HTML format mail thru JSP
NoSuchProviderException: smtp
basic question about smtp server
how to send a mail from a jsp?
I'm trying to make my code send me email - pliz help.