Avatar billede fredand Forsker
30. september 2004 - 10:49 Der er 10 kommentarer og
1 løsning

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
Avatar billede arne_v Ekspert
30. september 2004 - 11:37 #1
I am 99.999% sure that Hotmail does not allow you to connect to
their SMTP server !

You need yo connect to the SMTP server provided by your ISP.
Avatar billede arne_v Ekspert
30. september 2004 - 11:38 #2
So if you have dialed in via TDC Internet you need to use TDC SMTP server.

And so on.

The name of the SMTP server is in the paper you get when you signnup.
Avatar billede arne_v Ekspert
30. september 2004 - 11:39 #3
You need to use that SMTP server even though the FROM address you supply is
not at that ISP.
Avatar billede arne_v Ekspert
30. september 2004 - 11:39 #4
And yes - you should  be able to send via 56K dialup line.
Avatar billede fredand Forsker
30. september 2004 - 14:38 #5
Thanks mate!

Ok I see. I use a free internet from the Telia in sweden so there is no SMTP includede. I just pay for every minute online.

Did you try the code above or do you just think it should work since it looks so good? ;-)

BTW give a svar so I can reward you!

Best regards
Fredrik
Avatar billede arne_v Ekspert
30. september 2004 - 14:43 #6
I am not sue whether it works or not.

I can try it later today.

I send email sligtly different.

See http://www.eksperten.dk/artikler/77
Avatar billede arne_v Ekspert
30. september 2004 - 14:44 #7
I am very surprised that you do not get an SMTP server for that internet access.

In DK you get that for all including the free ones.
Avatar billede arne_v Ekspert
30. september 2004 - 14:44 #8
answer
Avatar billede fredand Forsker
01. oktober 2004 - 07:35 #9
Hello!

Intresting that you get SMTP-services in DK. Acctually the telecom-companies in SE has got a lot of critics for the prices especially for the mobileprices.

I will take a look at your email article at once.

Best regards
Fredrik
Avatar billede arne_v Ekspert
01. oktober 2004 - 07:45 #10
http://www.limehost.dk/43/
http://www.infoserv.dk/page.asp?page_id=31

(men de kan som sagt kun benyttes af den udbyder men er connectet via)
Avatar billede fredand Forsker
05. oktober 2004 - 12:34 #11
Hello Arne!

When I changed the host adress it worked perfect!

Thanks!

Best regards
Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester