Avatar billede fredand Forsker
20. februar 2004 - 16:41 Der er 1 kommentar og
1 løsning

Problems with javamail (Test source valid)

Hello!
I try to build a smart mailmanager with javamail. But I have run into som problems.

When I run it in a webserver (Orion) I can send to my own hotmail-adress but not to any other adresses. To other ardesses I get this Exception:
javax.mail.SendFailedException: Sending failed;
  nested exception is:
        javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
        javax.mail.SendFailedException: 501 Invalid address

        at javax.mail.Transport.send0(Transport.java:219)
        at javax.mail.Transport.send(Transport.java:81)
        at dsv.tools.MailManager.sendMail(MailManager.java:81)
        at dsv.servlets.Uppgift4aServlet.start(Uppgift4aServlet.java:34)
        at dsv.servlets.GeneralServlet.doGet(GeneralServlet.java:49)
        at dsv.servlets.GeneralServlet.doPost(GeneralServlet.java:30)
        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)

When I run it as a standalone I can not send to any adresses at all. I get this exception all the time:

javax.mail.SendFailedException: Sending failed;
  nested exception is:
        javax.mail.MessagingException: IOException while sending message;
  nested exception is:
        javax.activation.UnsupportedDataTypeException: no object DCH for MIME ty
pe multipart/mixed; boundary="----=_Part_0_7640919.1077290888456"
        at javax.mail.Transport.send0(Transport.java:219)
        at javax.mail.Transport.send(Transport.java:81)
        at MailManager.sendMail(MailManager.java:81)
        at MailManager.main(MailManager.java:115)

I have no clue to solve either one of the problems but It would be great with some help.

Best regards
Fredrik

Below is the code to test this:

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";

    static Properties mailProperties;

    public MailManager()
    {
    }

    public static void setPropertis(Properties mp)
    {
        mailProperties = mp;
    }

    public static boolean isPropertiesSetForSend()
    {
        String[] propertyValues = new String[4];
        propertyValues[0] = mailProperties.getProperty("TO");
        propertyValues[1] = mailProperties.getProperty("FROM");
        propertyValues[2] = mailProperties.getProperty("HOST");
        propertyValues[3] = mailProperties.getProperty("PROTOCOL");
        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
    {
        if(isPropertiesSetForSend())
        {
            Date date = new Date();

            Properties props = new Properties();
            props.put("mail.smtp.host", mailProperties.getProperty(HOST));

            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(false);

            Multipart multipart = new MimeMultipart();

            Message msg = new MimeMessage(session);
            msg.setContent(multipart);
            msg.setFrom(new InternetAddress(mailProperties.getProperty(FROM)));
            msg.setSubject( mailProperties.getProperty(SUBJECT) );
            msg.setSentDate(date);

            BodyPart bodyPartMain = new MimeBodyPart();
            bodyPartMain.setText( mailProperties.getProperty(MESSAGE) );
            multipart.addBodyPart(bodyPartMain);

            if(mailProperties.getProperty(TO) != null && !mailProperties.getProperty(TO).equals("") )
            {
                InternetAddress address = new InternetAddress(mailProperties.getProperty(TO));
                msg.setRecipient(Message.RecipientType.TO, address);
                Transport.send(msg);
            }
        }
    }

    public static void main(String[] args)
    {
        Properties mailProperties = new Properties();
        /*
        //Work props
        mailProperties.setProperty(MailManager.TO,             "xx.xxxx@xx-xxxx.se");
        mailProperties.setProperty(MailManager.CC,             "");
        mailProperties.setProperty(MailManager.BCC,         "");
        mailProperties.setProperty(MailManager.FROM,         "xx.xxxx@xx-xxxx.se");
        mailProperties.setProperty(MailManager.SUBJECT,     "Subject");
        mailProperties.setProperty(MailManager.MESSAGE,     "Hej!");
        mailProperties.setProperty(MailManager.HOST,         "mail.xx-xxxx.se");
        mailProperties.setProperty(MailManager.PROTOCOL,     "mail.smtp.host");
        */


        //Hotmailprops
        mailProperties.setProperty(MailManager.TO,             "xxxxx@hotmail.com");
        mailProperties.setProperty(MailManager.CC,             "");
        mailProperties.setProperty(MailManager.BCC,         "");
        mailProperties.setProperty(MailManager.FROM,         "xxxxx@hotmail.com");
        mailProperties.setProperty(MailManager.SUBJECT,     "Subject");
        mailProperties.setProperty(MailManager.MESSAGE,     "Hej!");
        mailProperties.setProperty(MailManager.HOST,         "mail.hotmail.com");
        mailProperties.setProperty(MailManager.PROTOCOL,     "mail.smtp.host");

        MailManager.setPropertis(mailProperties);
        if(MailManager.isPropertiesSetForSend())
        {
            try
            {
                MailManager.sendMail();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}
Avatar billede europe Nybegynder
22. februar 2004 - 11:36 #1
Jeg har lige kigget lidt på det og har prøvet at kører det på to forskellige netværk som standalone.

Får jeg skrevet den rigtige smtp server ind i '//Work props' så får jeg fint en mail igennem. Men skriver jeg en smtp server som jeg ikke har adgang til får jeg en

javax.mail.SendFailedException: Sending failed;
  nested exception is:
    javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    javax.mail.SendFailedException: 554 <jorgen.pedersen@bestseller.com>: Recipient address rejected: Access denied

fejl.....

Prøver jeg at kører delen '//Hotmailprops' får jeg ingen fejl men der kommer heller ikke nogen mail igennem...

Jeg regner med at du er sikker på at den smtp du angiver at du har adgang til den...

Ellers har jeg ikke nogen forslag til hvad det kan være....
Avatar billede fredand Forsker
05. juli 2004 - 07:44 #2
Hello!
I'm pretty sure that the problem is that some server demands authentication.
But I still think you should have the points so give me a "Svar" europe before 8/7-2004 so I can give it to you. Else I take it back.
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