Sending Mail through firewall
843830Mar 18 2005 — edited Mar 20 2005Respected developers,
I would like to seek your help for the development of an email application for an intranet. The javamail code which i am using need to send mail over the intranet protected by a firewall. Unfortunately, the email sending process is blocked.
I have inserted the mail code in a jsp below. Probably, i need to implement some relay service or use another port but i am not sure. Please, help me find a solution to this problem.
Thank you in advance for your help and consideration.
<%-- Send email & attachment file --%>
<%@ page import=" javax.activation.*,java.util.*,java.io.*,javax.mail.internet.*,javax.mail.* " %>
<html>
<head>
<title>Send email</title>
<%@ include file = "style.jsp" %>
</head>
<body>
<%@ include file = "header.jsp" %>
<div style="position:absolute; top:130px; left:10px; width:175px">
<%@ include file = "menu.jsp" %>
</div>
<div style="position:absolute; top:130px; left:200px; width:650px">
<%
if(request.getMethod().equals("post") )
{
boolean status = true;
String mailServer = "smtp.intnet.mu"; //stores the smtp host mailserver address
//retrieves values from form
String fromEmail = request.getParameter("from");
String toEmail = request.getParameter("to");
String messageEnter = request.getParameter("message");
String fileName = request.getParameter("myFile");
try
{
//create the properties and session object
Properties props = new Properties();
props.put("mail.smtp.host", mailServer);
Session s = Session.getInstance(props,null); //Session object is used for sending
MimeMessage message = new MimeMessage(s); //Create a message used by created session
InternetAddress from = new InternetAddress(fromEmail);
message.setFrom(from);
InternetAddress to = new InternetAddress(toEmail);
message.addRecipient(Message.RecipientType.TO, to);
message.setSubject("Send Email");
//message.setText(messageEnter);
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(messageEnter);
// Part two is attachment
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
Transport.send(message);
}
catch (Exception e)
{
System.out.println(e.getMessage() );
out.println("ERROR, your message failed, reason is: " + e);
status = false;
}
if (status == true)
{
out.println("Your message to " + toEmail + " was sent successfully!");
}
}
else
{
%>
<h1>Send email</h1>
<form method="post" name="mail" action="mailer.jsp">
<table BORDER="0">
<tr>
<td>To :</td>
<td><input type="text" name="to" size=24></td>
</tr>
<p>
<tr>
<td>From :</td>
<td><input type="text" name="from" size=24></td>
</tr>
<p>
<tr>
<td>Message :</td>
<td><textarea name="message" rows="5" cols="65"></textarea></td>
</tr>
<p>
<tr>
<td>File attachment: </td>
<td><input type="file" name="myFile"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="submit" name="Command"></td>
</tr>
</table>
</form>
<%
}
%>
</div>
</body>
</html>