PDA

View Full Version : Java Mail Exception


AsifTheManRahman
April 1, 2005, 02:49 PM
I have written a program that allows users to send articles to other people via email. However, the following error is generated when trying to send the emails:

"Sending failed; nested exception is: javax.mail.MessagingException:
Could not connect to SMTP host: 172.18.2.101, port: 25; nested
exception is: java.net.ConnectException: Connection timed out"



This is what I have so far:

String recipientEmails=request.getParameter(RECIPIENT_EMA ILS);
String senderName=request.getParameter(SENDER_NAME);
String senderEmail=request.getParameter(SENDER_EMAIL);
String senderComment=request.getParameter(SENDER_COMMENT) ;
String articleLink=request.getParameter(ARTICLE_LINK);
String articleTitle=request.getParameter(ARTICLE_TITLE);

try{
Properties props = new Properties();
props.put("mail.smtp.host", application.getInitParameter("mail.smtp.host"));
javax.mail.Session mailSession = javax.mail.Session.getInstance(props, null);
//Session mailSession=Session.getDefaultInstance(new Properties(), null);
MimeMessage message = new MimeMessage(mailSession);

// build recipients
if (recipientEmails!=null){

//just in case the recipient email is in the form abc,xyz
//without a space in between the "," tokenize the emails

StringBuffer recipientEmailBuffer = new StringBuffer();
StringTokenizer st1=new StringTokenizer(recipientEmails,",");
while (st1.hasMoreTokens()){
recipientEmailBuffer.append(st1.nextToken());
recipientEmailBuffer.append(" ");
}

recipientEmails = recipientEmailBuffer.toString();
StringTokenizer st=new StringTokenizer(recipientEmails, EMAIL_SEPARATOR);
while (st.hasMoreTokens()){
Address toAddress=new InternetAddress(st.nextToken());
message.addRecipient(Message.RecipientType.TO, toAddress);
}
}

// build subject
message.setSubject(articleTitle);

// build sender
Address fromAddress=new InternetAddress(senderEmail, senderName);
message.setFrom(fromAddress);

// build text body
StringBuffer sb=new StringBuffer();
sb.append("<#html><#body>");
//sb.append("<p>Subject: ").append(articleTitle).append("<#br>");
sb.append("This story was sent to you by ").append(senderName);
sb.append(" (").append(senderEmail).append("). Please click on the link below to view the full story.</#p><#br>");
if (senderComment!=null && senderComment.trim().length()>0){
sb.append("<#p>").append(senderComment).append("<#/p><#br>");
}
sb.append("<#hr>").append(articleTitle).append("<#br>");
String httpLink="http://www.*website_name*.com"+request.getParameter(ARTICLE_HTTP_LINK);
sb.append("<#a href='").append(httpLink).append("'>").append(httpLink).append("</#a>");
sb.append("</#body></#html>");
//message.setText(sb.toString());
message.setContent(sb.toString(), "text/html");

// send email
Transport.send(message);


Any help would be appreciated. Thanks in advance :)

Edited on, April 1, 2005, 7:52 PM GMT, by AsifTheManRahman.
Reason: sheesh...how do you turn html off on this forum?

AsifTheManRahman
April 1, 2005, 03:12 PM
i've looked further into it, and looks like the IP "172.18.2.101" can't be detected at all. so it's an IP problem - not a code problem. i'll get it fixed.

thanks for viewing this thread :lol: