How to send an email with Java?
Hello!I'm sitting at home and tries to create a class that is anable to send a mail. I call this class from an EJB but I do not think that matters. It should be the same if you just run it. How ever I do not get it to work. So perhaps some one else can try it and se if they can get it to work.
I got three guesses why this not work for me:
My guess no1 is that my conection to the Internet is through a 56k modem and perhaps it is not possible to reach the internet with this code if you use a modem. Perhaps you need a "solid" connection. I do not know? So if any one with a solid connection could test it it would be great. ("solid" connection, I do not know if that is the right term, in sweden we call it "bredband" "broad band").
My guess no2 is that I have missunderstod how the mail-protocol works. I thougt that the host could be any EXISTING mail-server that is up and running. So mail.hotmail.com would be great. Cause I do guess that I have tried this before when I had access to the Internet through a solid connection. And then it acctually worked. But I might be wrong. Or perhaps it is so that the host must be the same domain as the from or to adress. Perhaps some one could correct me!
My guess no3 is that mail.hotmail.com has realized thet people like my that do not have access to a mail server tries to use mail.hotmail.com as a host and in some way blocks this function for now a days.
Below is 2 exceptions the first is when I do not are connected to the internet at all, and the second when I have conected the computer with the webserver to the internet through a 56K modem. There is a slight differens. Below that is my test code that you should be able to test.
Exception 1
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: Unknown SMTP host: mail.hotmail.com;
nested exception is:
java.net.UnknownHostException: mail.hotmail.com
at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at ejb.tools.MailManager.sendMail(MailManager.java:88)
at ejb.beans.MailManagerEJB.sendMail(MailManagerEJB.java:31)
at MailManagerRemote_StatelessSessionBeanWrapper4.sendMail(MailManagerRemote_StatelessSessionBeanWrapper4.java:36)
at albinoni_admin.servlets.mainsite.MainsiteContactServlet.sendReply(MainsiteContactServlet.java:144)
at albinoni_admin.servlets.mainsite.MainsiteContactServlet.start(MainsiteContactServlet.java:52)
at albinoni_admin.servlets.GeneralServlet.doGet(GeneralServlet.java:58)
at albinoni_admin.servlets.GeneralServlet.doPost(GeneralServlet.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:211)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:309)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:336)
at com.evermind._efb._plc(.:518)
at com.evermind._efb._adc(.:174)
at com.evermind._cs._yqb(.:614)
at com.evermind._cs._yrb(.:189)
at com.evermind._bx.run(.:62)
Exception 2
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: Could not connect to SMTP host: mail.hotm
ail.com, port: 25;
nested exception is:
java.net.SocketException: Network is unreachable: connect
at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at ejb.tools.MailManager.sendMail(MailManager.java:88)
at ejb.beans.MailManagerEJB.sendMail(MailManagerEJB.java:31)
at MailManagerRemote_StatelessSessionBeanWrapper4.sendMail(MailManagerRe
mote_StatelessSessionBeanWrapper4.java:36)
at albinoni_admin.servlets.mainsite.MainsiteContactServlet.sendReply(Mai
nsiteContactServlet.java:144)
at albinoni_admin.servlets.mainsite.MainsiteContactServlet.start(Mainsit
eContactServlet.java:52)
at albinoni_admin.servlets.GeneralServlet.doGet(GeneralServlet.java:58)
at albinoni_admin.servlets.GeneralServlet.doPost(GeneralServlet.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:211)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:309)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:336)
at com.evermind._efb._plc(.:518)
at com.evermind._efb._adc(.:174)
at com.evermind._cs._yqb(.:614)
at com.evermind._cs._yrb(.:189)
at com.evermind._bx.run(.:62)
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import javax.activation.*;
public class MailManager
{
public final static String TO = "TO";
public final static String CC = "CC";
public final static String BCC = "BCC";
public final static String FROM = "FROM";
public final static String SUBJECT = "SUBJECT";
public final static String MESSAGE = "MESSAGE";
public final static String HOST = "HOST";
public final static String PROTOCOL = "PROTOCOL";
public final static String USER = "USER";
public final static String PORT = "PORT";
public final static String PASSWORD = "PASSWORD";
public static File[] files;
static Properties mailProperties;
public MailManager()
{
}
public static void setPropertis(Properties mp)
{
mailProperties = mp;
}
public static void addProperty(String key, String value)
{
mailProperties.put(key, value);
}
public static boolean isPropertiesSetForSend()
{
String[] propertyValues = new String[5];
propertyValues[0] = mailProperties.getProperty("TO");
propertyValues[1] = mailProperties.getProperty("FROM");
propertyValues[2] = mailProperties.getProperty("HOST");
propertyValues[3] = mailProperties.getProperty("PROTOCOL");
propertyValues[4] = mailProperties.getProperty("MESSAGE");
for(int i = 0; i < propertyValues.length; i++)
{
if(propertyValues[i] == null || propertyValues[i].equals(""))
{
return false;
}
}
if(propertyValues.length == 0)
{
return false;
}
return true;
}
public static void sendMail() throws Exception
{
System.out.println(mailProperties.getProperty(HOST));
System.out.println(mailProperties.getProperty(PROTOCOL));
System.out.println(mailProperties.getProperty(TO));
System.out.println(mailProperties.getProperty(FROM));
System.out.println(mailProperties.getProperty(SUBJECT));
System.out.println(mailProperties.getProperty(MESSAGE));
Properties properties = new Properties();
properties.put("mail.smtp.host", mailProperties.getProperty(HOST));
Session mailSession = Session.getDefaultInstance(properties, null);
InternetAddress fromAddress = new InternetAddress(mailProperties.getProperty(FROM));
InternetAddress toAddress = new InternetAddress(mailProperties.getProperty(TO));
MimeMessage mimeMessage = new MimeMessage(mailSession);
mimeMessage.setFrom(fromAddress);
mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);
mimeMessage.setSentDate(new java.util.Date());
mimeMessage.setSubject(mailProperties.getProperty(SUBJECT));
mimeMessage.setText(mailProperties.getProperty(MESSAGE));
Transport.send(mimeMessage);
}
public static void main(String[] args)
{
Properties mailProperties = new Properties();
mailProperties.put(HOST, "mail.hotmail.com");
mailProperties.put(PROTOCOL, "mail.smtp.host");
mailProperties.put(TO, "fredand44@hotmail.com");
mailProperties.put(FROM, "fredand44@hotmail.com");
mailProperties.put(SUBJECT, "Test subject");
mailProperties.put(MESSAGE, "This is a test mail!");
MailManager mailManager = new MailManager();
mailManager.setPropertis(mailProperties);
if(mailManager.isPropertiesSetForSend())
{
try
{
mailManager.sendMail();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
Best regards
Fredrik
