Skip to Content
0
Former Member
Dec 06, 2007 at 01:46 PM

Send mail using JSP

73 Views

Hey,

i need your help once again. I'm trying to write a JSP, that sends a mail to a given recipient.

I do have a working java code for this, but still desperatly trying to "convert" it to JSP and implement it as an iView.

Here's the java code:

package mail;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class sendmail
{
  public static void main (String args[])
  {

String host = "myhost";
String port = "25";
String sender = "sample@sender.com";
String user = "myuser";
String password = "mypw";

String recipient = "sample@recipient.com";
String subject = "Testmail";
String message = "This is the Testmail body";

Properties props = new Properties();
props.put("mail.host", host);
props.put("mail.smtp.port", port);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
javax.mail.Session mailSession = javax.mail.Session.getInstance(props);
try {
	Message msg = new MimeMessage(mailSession);
	msg.setFrom(new InternetAddress(sender));
	InternetAddress[] address = {new InternetAddress(recipient)};
	msg.setRecipients(Message.RecipientType.TO, address);
	msg.setSubject(subject);
	msg.setSentDate(new java.util.Date());
	msg.setText(message);
	Transport tr = mailSession.getTransport("smtp");
	tr.connect(host, user, password);
	msg.saveChanges();
	tr.sendMessage(msg, msg.getAllRecipients());
	tr.close();
	System.out.println("Mail sent!");
} catch (MessagingException me) {
	System.out.println("Error sending mail:"+me);
}
}
}

Does anyone know how to do this in JSP?

Best regards, Marko