Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Mail  RSS feed

 
Mark Leong
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to create a form to send mail using the JavaMail API. However I keep on getting the message saying that host cannot be found.
I have imported the following:-
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

To start the mail, I have performed the following:-

//Create the JavaMail session
Properties properties = System.getProperties();
properties.put("mail.smtp.host", EmailServer);
Session session = Session.getDefaultInstance(properties, null);
//Construct the message
MimeMessage message = new MimeMessage(session);
//Set the from address
Address fromAddress = new InternetAddress(EmailFrom);
message.setFrom(fromAddress);
//Parse and set the recipient address
Address[] toAddresses = InternetAddress.parse(EmailTo);
message.setRecipients(Message.RecipientType.TO, toAddresses);
Address[] ccAddresses = InternetAddress.parse(EmailCC);
message.setRecipients(Message.RecipientType.CC, ccAddresses);
Address[] bccAddresses = InternetAddress.parse(EmailBCC);
message.setRecipients(Message.RecipientType.BCC, bccAddresses);
//Set the subject and text
message.setSubject(EmailSubject);
message.setText(EmailMessage);
Transport.send(message);
I have used my own ISP email server.

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Mail questions are really better asked in the "Other Java APIs" forum, so I've moved this one.
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Well I guess you havent tried to connect to your mail server.
To do that you will have to create a Store object of the following type...
// Get a Store object
Store store = session.getStore("[your Server .. imap or smtp ]");
store.connect(host, user, password);

I guess that should be the problem.
All the best
Regds
Gautham Kasinath
 
Mark Leong
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Gautham.
The problem was actually the server. I cannot specify another smtp server except for the one hosting the servlet. The smtp server does not allow relaying.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!