Skip to main content

java code for sending an email

Java code for sending mails. An smtp server is required to relay mails.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.util.*" %>
<%@ page import="java.net.*" %>
<%@page import="sun.net.smtp.SmtpClient"%>
<%
String from = "/** from mail address, eg: joe@daltonbrothers.com */";
String to = "/** to mail address, eg: averell@daltonbrothers.com */";

try {
SmtpClient mailclient = new SmtpClient("/** smtp server, eg: 192.168.1.2 */");
mailclient.from(from);
mailclient.to(to);
PrintStream message = mailclient.startMessage();
message.println("To: " + to);
message.println("Subject: Lucky Luke");
message.println();
message.println("finger on a trigger");
message.println();
mailclient.closeServer();
} catch (IOException e) {
       out.println("ERROR SENDING EMAIL:" + e);
}

Comments

Popular posts from this blog