NorthStarDA
IS-IT--Management
hi,
I am trying to write a script that will take attributes and build either a text,html, or multi-part email. It's a custom tag for my App Server. I successfully got it to send a multipart message as well as text messages alone, but when i put in a conditional, it does not work.
my code is below if anyone can see what im doing wrong
thanks for any help you guys can give me
** im very new to java so if any of this is silly, don't laugh at me
I am trying to write a script that will take attributes and build either a text,html, or multi-part email. It's a custom tag for my App Server. I successfully got it to send a multipart message as well as text messages alone, but when i put in a conditional, it does not work.
my code is below if anyone can see what im doing wrong
thanks for any help you guys can give me
** im very new to java so if any of this is silly, don't laugh at me
Code:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import com.allaire.cfx.*;
public class AuthSMTP3 implements CustomTag
{
public void processRequest( Request request, Response response ) throws Exception
{
// validate that required attributes were passed
if (!request.attributeExists( "TO" ) ||
!request.attributeExists( "FROM" ) ||
!request.attributeExists( "SUBJECT" ) ||
!request.attributeExists( "TYPE" ) ||
!request.attributeExists( "HOST" ) )
{
throw new Exception(
"Missing attribute (TO, FROM, SUBJECT, TYPE, and HOST are all " +
"required attributes for this tag)" ) ;
}
else if ( request.getAttribute("TYPE") == "text" &&
!request.attributeExists( "TEXTPART" ) )
{
throw new Exception(
"The TEXTPART attribute is required when TYPE=text");
}
else if ( request.getAttribute("TYPE") == "html" &&
!request.attributeExists( "HTMLPART" ) )
{
throw new Exception(
"The HTMLPART attribute is required when TYPE=html");
}
else if ( request.getAttribute("TYPE") == "multipart" &&
!request.attributeExists( "HTMLPART" ) &&
!request.attributeExists( "TEXTPART") )
{
throw new Exception(
"The HTMLPART AND TEXTPART attributes are required when TYPE=multipart");
}
if ( request.attributeExists("USER") && !request.attributeExists("PASSWORD"))
{
throw new Exception(
"The PASSWORD attribute is required when using USER");
}
else if ( request.attributeExists("PASSWORD") && !request.attributeExists("USER"))
{
throw new Exception(
"The USER attribute is required when using PASSWORD");
}
String HTMLPART = request.getAttribute("HTMLPART");
String TEXTPART = request.getAttribute("TEXTPART");
String user = request.getAttribute("USER");
String password = request.getAttribute("PASSWORD");
String to = request.getAttribute("TO");
String from = request.getAttribute("FROM");
String subject = request.getAttribute("SUBJECT");
String type = request.getAttribute("TYPE");
String host = request.getAttribute("HOST");
//default port to 25
int port = 25;
if(request.attributeExists("PORT"))
{
port = Integer.parseInt(request.getAttribute("PORT"));
}
Properties props = new Properties();
props.put("mail.smtp.host", host);
if (request.attributeExists("USER"))
{
props.put("mail.smtp.auth", "true");
}
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
try
{
if (request.getAttribute("TYPE") == "multipart")
{
// create a message
Multipart multipart = new MimeMultipart("alternative");
// Create the text part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(TEXTPART);
multipart.addBodyPart(messageBodyPart);
// Create the html part
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(HTMLPART, "text/html");
multipart.addBodyPart(messageBodyPart);
// Associate multi-part with message
msg.setContent(multipart);
}
else if (request.getAttribute("TYPE") == "text")
{
msg.setText(TEXTPART);
}
else if (request.getAttribute("TYPE") == "html")
{
msg.setContent(HTMLPART, "text/html");
}
// send the message
Transport tr = session.getTransport("smtp");
System.out.println();
tr.connect(host, port, user, password);
msg.saveChanges();
tr.sendMessage( msg, msg.getAllRecipients() );
}
catch (Exception sfx)
{
response.write("Mail send Failure! " + sfx.getMessage());
}
response.write("Mail sent successfully.");
}
}