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();
}
}
}
}
