Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Java Mail

 
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.

 
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.
 
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.
 
Alas, poor Yorick, he knew this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic