Message Boards Message Boards

0
|
2420 Views
|
0 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Email with Multiple attachements

Posted 9 years ago

I'm using the JLINK example provided here (http://code.gluonvision.com/SendEmail/) to send some of my reports via email and make them appear in the email body rather than just attachments.

Unfortunately when I attach more than one chart only the final listed one ever appears.

This is the code I'm using in Mathematica

SendEmail@{"Server" -> "**Server**",
  "UserName" -> "", "Password" -> "",
  "To" -> {"**Email**"}, 
  "From" -> "**Email**", 
  "FullName" -> "**My name**", 
  "Subject" -> "**Subject** " <> DateString[], "Body" -> htmlbody,
   "PortNumber" -> 25.
  "Attachments"\[Rule] {"C:\Users\lj\Documents\MainChart.png", "C:\Users\lj\Documents\Chart1.png", "C:\Users\lj\Documents\Chart2.png", "C:\Users\lj\Documents\Chart3.png"}
  }

I think that what is happening is that for each attachment it sets the attachment to the email as the attachment rather than as another attachment. Looking at the .m file which this code references. I think the main issue is with the Do loops which I've highlighted below (whole code included incase I'm missing something)

BeginPackage["SendEmail`", {"JLink`"}]
JLink`InstallJava[];
JLink`AddToClassPath[FileNameJoin[{DirectoryName[FindFile[$Input]], "Resources"}]];
SendEmail::usage = "SendEmail[rules] sends an email message built up from the elements specified in rules, similar
to SendMail, but using Apache Commons, allowing SendEmail to work in webMathematica. Furthermore HTML emails are
enabled.";
SendEmail::filenoutfound = "Attachment `1` was not found.";
SendEmail::badbody = "The message body is neither a string nor a list of strings.";
Begin["`Private`"]
(* Implementation of the package *)
Options[ SendEmail ] = Options[SendMail];
SetOptions[SendEmail, "PortNumber" -> 465];
SendEmail[OptionsPattern[]] :=
Module[ {fileattachments, attachment, email, defauth, fileattach,
filenamewithoutdirectories, body, sslsmtpport, httpMatchQ, att},
fileattachments = Flatten[{OptionValue[ "Attachments" ]}];
If[ Length[fileattachments] > 0,
attachment = JLink`JavaNew[ "org.apache.commons.mail.EmailAttachment" ];

(*This is where I think the I need to make an alertation*)
    Do[ 

        If[ !FileExistsQ[fileattach],
           Message[SendEmail::filenotfound, fileattach]
        ,
           If[StringFreeQ[fileattach, $PathnameSeparator],
          att = FileNameJoin[{Directory[], fileattach}]
           ,
             att = fileattach
           ];
        filenamewithoutdirectories = Last @ FileNameSplit[att];
        attachment @ setPath[att];
       attachment @ setDescription[ filenamewithoutdirectories ];
       attachment @ setName[ filenamewithoutdirectories ];
       ] 
    ,
    {fileattach, fileattachments}
    ];
(*This is end the bit I think I need to alter*)

];
body = OptionValue["Body"];
(* Create the email message *)
email = Which[
!MatchQ[body, _String] && !MatchQ[body, {___String}] ,
Message[SendEmail::badbody];
$Failed ,
MatchQ[body, {___String}] ,
JLink`JavaNew["org.apache.commons.mail.HtmlEmail"] ,
StringMatchQ[body, "*<html>*</html>*", IgnoreCase -> True] ,
JLink`JavaNew["org.apache.commons.mail.HtmlEmail"],
True,
JLink`JavaNew[ "org.apache.commons.mail.MultiPartEmail" ]
];
httpMatchQ[s_String /; StringMatchQ[s, "http://*.*", IgnoreCase -> True]] :=
True;
httpMatchQ[s_String] :=
False;
sslsmtpport = ToString @ OptionValue["PortNumber"];
(* for the moment: only support 465 *)
If[ sslsmtpport === "465",
email @ setTLS[True];
email @ setSslSmtpPort[ToString @ sslsmtpport ];
];
defauth = JLink`JavaNew[ "org.apache.commons.mail.DefaultAuthenticator", (*OptionValue["UserName"], OptionValue["Password"]*) ];
email @ setAuthenticator[defauth];
email @ setHostName[ OptionValue["Server"] ];
Do[ email @ addTo[ to, "" ], {to, Flatten[{ OptionValue["To"] }]} ];
Do[ email @ addCc[ cc, "" ], {cc, Flatten[{ OptionValue["Cc"] }]} ];
Do[ email @ addBcc[ bcc, "" ], {bcc, Flatten[{ OptionValue["Bcc"] }]} ];
If[ OptionValue["ReplyTo"]=!=Automatic,
Do[ email @ addReplyTo[ rto, "" ], {rto, Flatten[{ OptionValue["ReplyTo"] }]} ]
];
(email @ setFrom[ OptionValue["From"], OptionValue["FullName"]/.None:>Sequence[] ]) /; StringQ[OptionValue["FullName"]];
email @ setSubject[ OptionValue["Subject"] ];
If[ StringMatchQ[body, "*<html>*</html>*", IgnoreCase -> True],
email @ setHtmlMsg @ body,
email @ setMsg @ body
];
(* // add the attachment *)
If[ Length[fileattachments] > 0,
email @ attach[attachment]
];
(* // send the email *)
email@send[]
];

End[]
EndPackage[]

Unfortunately I'm not 100% on this piece of code. Does another have any suggestions?

POSTED BY: L J
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract