You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Nweike Onwuyali <nw...@gmail.com> on 2015/04/23 09:59:08 UTC

[email]-Cannot send mail embedded with image from my Play Framework

Hi,
I am a newbie and started using commons email only a few days ago.I am able
to send mail with html content successfully. But when i tried to embed an
image like a logo, i get the following error
++++++++++++++++++++++++++++++++++++++++++++++
java.lang.NullPointerException
org.apache.commons.mail.ImageHtmlEmail.replacePattern(ImageHtmlEmail.java:151)
[error] play - Cannot invoke the action, eventually got an error:
java.lang.Exception: java.lang.NullPointerException
at
org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:105)
at org.apache.commons.mail.Email.send(Email.java:1436)
at utils.Mailer.sendMail(Mailer.java:65)
at controllers.Users.save(Users.java:64)
++++++++++++++++++++++++++++++++++++++++++++++
Below is my code:

HtmlEmail email = new ImageHtmlEmail();

URL logoURL = new URL(mailDetails.get("logoURL").toString());
String cidlogo = email.embed(logoURL, "Ziphii logo");
email.setHtmlMsg("<html>The apache logo - <img
src=\"cid:"+cidlogo+"\"></html>");

String[] to = (String[]) mailDetails.get("recipient");
String[] from = (String[]) mailDetails.get("from");
String[] credentials = (String[]) mailDetails.get("credentials");

email.setHostName(mailDetails.get("hostName").toString());
email.setSmtpPort(Integer.parseInt(mailDetails.get("hostPort").toString()));//587
email.setAuthentication(credentials[0], credentials[1]);

email.addTo(to[0], to[1]);
email.setFrom(from[0],from[1]);
email.setSubject(mailDetails.get("subject").toString());

 email.setTextMsg(mailDetails.get("alternativeRouteUrl").toString());
 email.setDebug(true);
 email.send();

+++++++++++++++++++++++++++++++++++++++++++

I am using a Play! Framework and i am in dev mode.

Re: [email]-Cannot send mail embedded with image from my Play Framework

Posted by Nweike Onwuyali <nw...@gmail.com>.
Hi Thomas,
Thanks. Its working now. Wow. I am so grateful

On Thu, Apr 23, 2015 at 9:58 AM, Thomas Neidhart <th...@gmail.com>
wrote:

> Hi again,
>
> btw your example can be simplified like that (the DataSourceUrlResolver
> already supports embedding resources automatically):
>
> ImageHtmlEmail email = new ImageHtmlEmail();
>
> email.setDataSourceResolver(new DataSourceUrlResolver(null)); // or your
> local domain if you have relative urls
> email.setHtmlMsg("<html>The apache logo - <img
> src=\""+mailDetails.get("logoURL")+"\"></html>");
>
> Thomas
>
>
> On Thu, Apr 23, 2015 at 10:34 AM, Thomas Neidhart <
> thomas.neidhart@gmail.com
> > wrote:
>
> > Hi,
> >
> > you need to specify a DataSourceUrlResolver for your HtmlEmail instance
> > when embedding images by URL.
> >
> > See the example in ther userguide (
> > http://commons.apache.org/proper/commons-email/userguide.html).
> > I just realized that the example in the userguide is outdated, instead of
> > the DataSourceResolverImpl, use a DataSourceUrlResolver.
> >
> > The baseURL does not matter if you use absolute urls.
> >
> > Thomas
> >
> > On Thu, Apr 23, 2015 at 9:59 AM, Nweike Onwuyali <
> nweikeonwuyali@gmail.com
> > > wrote:
> >
> >> Hi,
> >> I am a newbie and started using commons email only a few days ago.I am
> >> able
> >> to send mail with html content successfully. But when i tried to embed
> an
> >> image like a logo, i get the following error
> >> ++++++++++++++++++++++++++++++++++++++++++++++
> >> java.lang.NullPointerException
> >>
> >>
> org.apache.commons.mail.ImageHtmlEmail.replacePattern(ImageHtmlEmail.java:151)
> >> [error] play - Cannot invoke the action, eventually got an error:
> >> java.lang.Exception: java.lang.NullPointerException
> >> at
> >>
> >>
> org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:105)
> >> at org.apache.commons.mail.Email.send(Email.java:1436)
> >> at utils.Mailer.sendMail(Mailer.java:65)
> >> at controllers.Users.save(Users.java:64)
> >> ++++++++++++++++++++++++++++++++++++++++++++++
> >> Below is my code:
> >>
> >> HtmlEmail email = new ImageHtmlEmail();
> >>
> >> URL logoURL = new URL(mailDetails.get("logoURL").toString());
> >> String cidlogo = email.embed(logoURL, "Ziphii logo");
> >> email.setHtmlMsg("<html>The apache logo - <img
> >> src=\"cid:"+cidlogo+"\"></html>");
> >>
> >> String[] to = (String[]) mailDetails.get("recipient");
> >> String[] from = (String[]) mailDetails.get("from");
> >> String[] credentials = (String[]) mailDetails.get("credentials");
> >>
> >> email.setHostName(mailDetails.get("hostName").toString());
> >>
> >>
> email.setSmtpPort(Integer.parseInt(mailDetails.get("hostPort").toString()));//587
> >> email.setAuthentication(credentials[0], credentials[1]);
> >>
> >> email.addTo(to[0], to[1]);
> >> email.setFrom(from[0],from[1]);
> >> email.setSubject(mailDetails.get("subject").toString());
> >>
> >>  email.setTextMsg(mailDetails.get("alternativeRouteUrl").toString());
> >>  email.setDebug(true);
> >>  email.send();
> >>
> >> +++++++++++++++++++++++++++++++++++++++++++
> >>
> >> I am using a Play! Framework and i am in dev mode.
> >>
> >
> >
>

Re: [email]-Cannot send mail embedded with image from my Play Framework

Posted by Thomas Neidhart <th...@gmail.com>.
Hi again,

btw your example can be simplified like that (the DataSourceUrlResolver
already supports embedding resources automatically):

ImageHtmlEmail email = new ImageHtmlEmail();

email.setDataSourceResolver(new DataSourceUrlResolver(null)); // or your
local domain if you have relative urls
email.setHtmlMsg("<html>The apache logo - <img
src=\""+mailDetails.get("logoURL")+"\"></html>");

Thomas


On Thu, Apr 23, 2015 at 10:34 AM, Thomas Neidhart <thomas.neidhart@gmail.com
> wrote:

> Hi,
>
> you need to specify a DataSourceUrlResolver for your HtmlEmail instance
> when embedding images by URL.
>
> See the example in ther userguide (
> http://commons.apache.org/proper/commons-email/userguide.html).
> I just realized that the example in the userguide is outdated, instead of
> the DataSourceResolverImpl, use a DataSourceUrlResolver.
>
> The baseURL does not matter if you use absolute urls.
>
> Thomas
>
> On Thu, Apr 23, 2015 at 9:59 AM, Nweike Onwuyali <nweikeonwuyali@gmail.com
> > wrote:
>
>> Hi,
>> I am a newbie and started using commons email only a few days ago.I am
>> able
>> to send mail with html content successfully. But when i tried to embed an
>> image like a logo, i get the following error
>> ++++++++++++++++++++++++++++++++++++++++++++++
>> java.lang.NullPointerException
>>
>> org.apache.commons.mail.ImageHtmlEmail.replacePattern(ImageHtmlEmail.java:151)
>> [error] play - Cannot invoke the action, eventually got an error:
>> java.lang.Exception: java.lang.NullPointerException
>> at
>>
>> org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:105)
>> at org.apache.commons.mail.Email.send(Email.java:1436)
>> at utils.Mailer.sendMail(Mailer.java:65)
>> at controllers.Users.save(Users.java:64)
>> ++++++++++++++++++++++++++++++++++++++++++++++
>> Below is my code:
>>
>> HtmlEmail email = new ImageHtmlEmail();
>>
>> URL logoURL = new URL(mailDetails.get("logoURL").toString());
>> String cidlogo = email.embed(logoURL, "Ziphii logo");
>> email.setHtmlMsg("<html>The apache logo - <img
>> src=\"cid:"+cidlogo+"\"></html>");
>>
>> String[] to = (String[]) mailDetails.get("recipient");
>> String[] from = (String[]) mailDetails.get("from");
>> String[] credentials = (String[]) mailDetails.get("credentials");
>>
>> email.setHostName(mailDetails.get("hostName").toString());
>>
>> email.setSmtpPort(Integer.parseInt(mailDetails.get("hostPort").toString()));//587
>> email.setAuthentication(credentials[0], credentials[1]);
>>
>> email.addTo(to[0], to[1]);
>> email.setFrom(from[0],from[1]);
>> email.setSubject(mailDetails.get("subject").toString());
>>
>>  email.setTextMsg(mailDetails.get("alternativeRouteUrl").toString());
>>  email.setDebug(true);
>>  email.send();
>>
>> +++++++++++++++++++++++++++++++++++++++++++
>>
>> I am using a Play! Framework and i am in dev mode.
>>
>
>

Re: [email]-Cannot send mail embedded with image from my Play Framework

Posted by "siegfried.goeschl@it20one.com" <si...@it20one.com>.
Hi folks,

it is also a good idea to check the existing tests :-)

There is also a a "Live Test" which allows you to send the emails to 
your actual SMTP server (when configured properly)

http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/java/org/apache/commons/mail/EmailLiveTest.java?revision=1628546&view=markup

Cheers,

Siegfried Goeschl

On 23.04.15 10:34, Thomas Neidhart wrote:
> Hi,
>
> you need to specify a DataSourceUrlResolver for your HtmlEmail instance
> when embedding images by URL.
>
> See the example in ther userguide (
> http://commons.apache.org/proper/commons-email/userguide.html).
> I just realized that the example in the userguide is outdated, instead of
> the DataSourceResolverImpl, use a DataSourceUrlResolver.
>
> The baseURL does not matter if you use absolute urls.
>
> Thomas
>
> On Thu, Apr 23, 2015 at 9:59 AM, Nweike Onwuyali <nw...@gmail.com>
> wrote:
>
>> Hi,
>> I am a newbie and started using commons email only a few days ago.I am able
>> to send mail with html content successfully. But when i tried to embed an
>> image like a logo, i get the following error
>> ++++++++++++++++++++++++++++++++++++++++++++++
>> java.lang.NullPointerException
>>
>> org.apache.commons.mail.ImageHtmlEmail.replacePattern(ImageHtmlEmail.java:151)
>> [error] play - Cannot invoke the action, eventually got an error:
>> java.lang.Exception: java.lang.NullPointerException
>> at
>>
>> org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:105)
>> at org.apache.commons.mail.Email.send(Email.java:1436)
>> at utils.Mailer.sendMail(Mailer.java:65)
>> at controllers.Users.save(Users.java:64)
>> ++++++++++++++++++++++++++++++++++++++++++++++
>> Below is my code:
>>
>> HtmlEmail email = new ImageHtmlEmail();
>>
>> URL logoURL = new URL(mailDetails.get("logoURL").toString());
>> String cidlogo = email.embed(logoURL, "Ziphii logo");
>> email.setHtmlMsg("<html>The apache logo - <img
>> src=\"cid:"+cidlogo+"\"></html>");
>>
>> String[] to = (String[]) mailDetails.get("recipient");
>> String[] from = (String[]) mailDetails.get("from");
>> String[] credentials = (String[]) mailDetails.get("credentials");
>>
>> email.setHostName(mailDetails.get("hostName").toString());
>>
>> email.setSmtpPort(Integer.parseInt(mailDetails.get("hostPort").toString()));//587
>> email.setAuthentication(credentials[0], credentials[1]);
>>
>> email.addTo(to[0], to[1]);
>> email.setFrom(from[0],from[1]);
>> email.setSubject(mailDetails.get("subject").toString());
>>
>>   email.setTextMsg(mailDetails.get("alternativeRouteUrl").toString());
>>   email.setDebug(true);
>>   email.send();
>>
>> +++++++++++++++++++++++++++++++++++++++++++
>>
>> I am using a Play! Framework and i am in dev mode.
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [email]-Cannot send mail embedded with image from my Play Framework

Posted by Thomas Neidhart <th...@gmail.com>.
Hi,

you need to specify a DataSourceUrlResolver for your HtmlEmail instance
when embedding images by URL.

See the example in ther userguide (
http://commons.apache.org/proper/commons-email/userguide.html).
I just realized that the example in the userguide is outdated, instead of
the DataSourceResolverImpl, use a DataSourceUrlResolver.

The baseURL does not matter if you use absolute urls.

Thomas

On Thu, Apr 23, 2015 at 9:59 AM, Nweike Onwuyali <nw...@gmail.com>
wrote:

> Hi,
> I am a newbie and started using commons email only a few days ago.I am able
> to send mail with html content successfully. But when i tried to embed an
> image like a logo, i get the following error
> ++++++++++++++++++++++++++++++++++++++++++++++
> java.lang.NullPointerException
>
> org.apache.commons.mail.ImageHtmlEmail.replacePattern(ImageHtmlEmail.java:151)
> [error] play - Cannot invoke the action, eventually got an error:
> java.lang.Exception: java.lang.NullPointerException
> at
>
> org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:105)
> at org.apache.commons.mail.Email.send(Email.java:1436)
> at utils.Mailer.sendMail(Mailer.java:65)
> at controllers.Users.save(Users.java:64)
> ++++++++++++++++++++++++++++++++++++++++++++++
> Below is my code:
>
> HtmlEmail email = new ImageHtmlEmail();
>
> URL logoURL = new URL(mailDetails.get("logoURL").toString());
> String cidlogo = email.embed(logoURL, "Ziphii logo");
> email.setHtmlMsg("<html>The apache logo - <img
> src=\"cid:"+cidlogo+"\"></html>");
>
> String[] to = (String[]) mailDetails.get("recipient");
> String[] from = (String[]) mailDetails.get("from");
> String[] credentials = (String[]) mailDetails.get("credentials");
>
> email.setHostName(mailDetails.get("hostName").toString());
>
> email.setSmtpPort(Integer.parseInt(mailDetails.get("hostPort").toString()));//587
> email.setAuthentication(credentials[0], credentials[1]);
>
> email.addTo(to[0], to[1]);
> email.setFrom(from[0],from[1]);
> email.setSubject(mailDetails.get("subject").toString());
>
>  email.setTextMsg(mailDetails.get("alternativeRouteUrl").toString());
>  email.setDebug(true);
>  email.send();
>
> +++++++++++++++++++++++++++++++++++++++++++
>
> I am using a Play! Framework and i am in dev mode.
>