Skip to content
This repository

added mail attachment extension #114

Merged
merged 3 commits into from over 1 year ago

2 participants

Brent Thornton Tim Windsor
Brent Thornton

No description provided.

Smartphone/MailAttachment/README.md
((37 lines not shown))
  37
+## Summary
  38
+
  39
+	var result = webworks.mail.attachment.send(emailAddress, subject, body, filePath);
  40
+
  41
+   
  42
+## Code Example
  43
+	<html>
  44
+		<head>
  45
+		<meta name="viewport" id="viewport" content="height=device-height,width=device-width,user-scalable=no" />
  46
+		<script type="text/javascript" >
  47
+		function doClick(){
  48
+			var email = "test@test.com";
  49
+			var subject = "See attachment";
  50
+			var body = "Some text goes here";
  51
+
  52
+			var file = "file:///myiamge.png";
2
Tim Windsor Owner

minor typo here. Can you fix that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Tim Windsor timwindsor merged commit ffe9c3b into from November 05, 2012
Tim Windsor timwindsor closed this November 05, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
75  Smartphone/MailAttachment/README.md
Source Rendered
... ...
@@ -0,0 +1,75 @@
  1
+# Mail Attachment
  2
+This extension will allow the user to send an email with an attachment.
  3
+
  4
+**Author:** [Brent Thornton](https://github.com/bthornton32) - Special thanks to plepleus for posting some of the code here: http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/Email-with-Attachment-from-Webworks-app/m-p/1760425#M24580
  5
+
  6
+## Tested On
  7
+
  8
+* BlackBerry Bold 9790 v7.1.0.221
  9
+* BlackBerry Bold 9900 v7.1.0.251
  10
+* BlackBerry Bold 9900 v7.0.0.540
  11
+
  12
+**Requires BlackBerry WebWorks SDK for Smartphones v2.0 or higher**
  13
+
  14
+Have a problem with this extension?  [Log an Issue](https://github.com/blackberry/WebWorks-Community-APIs/issues) or contact the [Author](https://github.com/bthornton32)
  15
+
  16
+## How To Configure The Extension For Use
  17
+
  18
+1. Locate your BlackBerry WebWorks SDK for Smartphone extensions directory using your File Explorer.  Default path is _**C:\Program Files\Research In Motion\BlackBerry WebWorks Packager\ext**_
  19
+
  20
+2. Create a new _**webworks.mail.attachment**_ directory in the _**ext**_ directory
  21
+
  22
+3. Download the source from this repository and unzip it to a location on your computer
  23
+
  24
+4. Using File Explorer browse to this extension's downloaded source code _**Smartphone\MailAttachment**_
  25
+
  26
+5. Copy the _**library.xml**_ file from the downloaded _**Smartphone\MailAttachment**_ directory to your new _**ext\webworks.mail.attachment**_ directory
  27
+
  28
+6. Copy the downloaded _**Smartphone\MailAttachment\webworks**_ directory to your new _**ext\webworks.mail.attachment\webworks**_ directory
  29
+
  30
+**NOTE:** Be sure to back-up this _**ext\webworks.mail.attachment**_ directory in your WebWorks SDK extensions directory before performing a WebWorks SDK upgrade. Simply copy it back into the _**ext**_ directory after you have completed your SDK upgrade.
  31
+
  32
+## Required Feature ID
  33
+Whenever you use the below feature id in any of your WebWorks applications this extension will be loaded for use.
  34
+
  35
+    <feature id="webworks.mail.attachment" />
  36
+
  37
+## Summary
  38
+
  39
+	var result = webworks.mail.attachment.send(emailAddress, subject, body, filePath);
  40
+
  41
+   
  42
+## Code Example
  43
+	<html>
  44
+		<head>
  45
+		<meta name="viewport" id="viewport" content="height=device-height,width=device-width,user-scalable=no" />
  46
+		<script type="text/javascript" >
  47
+		function doClick(){
  48
+			var email = "test@test.com";
  49
+			var subject = "See attachment";
  50
+			var body = "Some text goes here";
  51
+
  52
+			var file = "file:///myimage.png";
  53
+
  54
+		    var result = webworks.mail.attachment.send(email, subject, body, file);
  55
+		    if (result === true) alert("Email sent.");
  56
+		    else alert("Error sending email.");
  57
+		}
  58
+		</script>
  59
+		</head>
  60
+		<body >
  61
+		<h1>Hello World</h1>
  62
+		<button onclick="doClick();">Send Email</button> 
  63
+		<div id="debug"></div>
  64
+		</body>
  65
+	</html>
  66
+    
  67
+
  68
+## Usage Information
  69
+It is best to use this with the filePicker extension to ensure a true file path. Check your Messages after sending the email. It should appear in sent items.
  70
+
  71
+## Disclaimer
  72
+
  73
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  74
+
  75
+
34  Smartphone/MailAttachment/library.xml
... ...
@@ -0,0 +1,34 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+<!--
  3
+* Copyright 2010-2012 Research In Motion Limited.
  4
+
  5
+ Licensed under the Apache License, Version 2.0 (the "License");
  6
+ you may not use this file except in compliance with the License.
  7
+ You may obtain a copy of the License at
  8
+
  9
+ http://www.apache.org/licenses/LICENSE-2.0
  10
+
  11
+ Unless required by applicable law or agreed to in writing, software
  12
+ distributed under the License is distributed on an "AS IS" BASIS,
  13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14
+ See the License for the specific language governing permissions and
  15
+ limitations under the License.
  16
+-->
  17
+<library>
  18
+    <extension id="webworks.mail.attachment">
  19
+        <entryClass>webworks.mail.attachment.MailAttachmentExtension</entryClass>
  20
+    </extension>
  21
+    <platforms>
  22
+    <platform value="JAVA">
  23
+      <target version="default" config="JAVA" />
  24
+    </platform>
  25
+  </platforms>
  26
+  <configurations>
  27
+    <configuration name="JAVA" version="1.0" >
  28
+      <src type="text/java" path="webworks" comment="API implementation in Java" />
  29
+    </configuration>
  30
+  </configurations>  
  31
+    <features>
  32
+        <feature id="webworks.mail.attachment" version="1.0.0" ></feature>
  33
+    </features>
  34
+</library>
51  Smartphone/MailAttachment/webworks/mail/attachment/MailAttachmentExtension.java
... ...
@@ -0,0 +1,51 @@
  1
+/*
  2
+* Copyright 2010-2012 Research In Motion Limited.
  3
+*
  4
+* Licensed under the Apache License, Version 2.0 (the "License");
  5
+* you may not use this file except in compliance with the License.
  6
+* You may obtain a copy of the License at
  7
+*
  8
+* http://www.apache.org/licenses/LICENSE-2.0
  9
+*
  10
+* Unless required by applicable law or agreed to in writing, software
  11
+* distributed under the License is distributed on an "AS IS" BASIS,
  12
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+* See the License for the specific language governing permissions and
  14
+* limitations under the License.
  15
+*/
  16
+package webworks.mail.attachment;
  17
+
  18
+import org.w3c.dom.Document;
  19
+
  20
+import net.rim.device.api.browser.field2.BrowserField;
  21
+import net.rim.device.api.script.ScriptEngine;
  22
+import net.rim.device.api.web.WidgetConfig;
  23
+import net.rim.device.api.web.WidgetExtension;
  24
+
  25
+public final class MailAttachmentExtension implements WidgetExtension {
  26
+
  27
+	private final static String[] _features = { MailAttachmentNamespace.NAME };
  28
+	
  29
+	public String[] getFeatureList() {
  30
+		return _features;
  31
+	}
  32
+
  33
+	public void loadFeature(String feature, String version, Document doc,
  34
+			ScriptEngine scriptEngine) throws Exception {
  35
+		if (feature.equals(MailAttachmentNamespace.NAME)) {
  36
+            scriptEngine.addExtension(feature, new MailAttachmentNamespace());
  37
+        }
  38
+
  39
+	}
  40
+
  41
+	public void register(WidgetConfig widgetConfig, BrowserField browserField) {
  42
+		// TODO Auto-generated method stub
  43
+
  44
+	}
  45
+
  46
+	public void unloadFeatures(Document doc) {
  47
+		// TODO Auto-generated method stub
  48
+
  49
+	}
  50
+
  51
+}
110  Smartphone/MailAttachment/webworks/mail/attachment/MailAttachmentFunction.java
... ...
@@ -0,0 +1,110 @@
  1
+/*
  2
+* Copyright 2010-2012 Research In Motion Limited.
  3
+*
  4
+* Licensed under the Apache License, Version 2.0 (the "License");
  5
+* you may not use this file except in compliance with the License.
  6
+* You may obtain a copy of the License at
  7
+*
  8
+* http://www.apache.org/licenses/LICENSE-2.0
  9
+*
  10
+* Unless required by applicable law or agreed to in writing, software
  11
+* distributed under the License is distributed on an "AS IS" BASIS,
  12
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+* See the License for the specific language governing permissions and
  14
+* limitations under the License.
  15
+*/
  16
+package webworks.mail.attachment;
  17
+
  18
+import java.io.DataInputStream;
  19
+
  20
+import javax.microedition.io.Connector;
  21
+import javax.microedition.io.file.FileConnection;
  22
+
  23
+import net.rim.blackberry.api.mail.Address;
  24
+import net.rim.blackberry.api.mail.Folder;
  25
+import net.rim.blackberry.api.mail.Message;
  26
+import net.rim.blackberry.api.mail.Multipart;
  27
+import net.rim.blackberry.api.mail.Session;
  28
+import net.rim.blackberry.api.mail.SupportedAttachmentPart;
  29
+import net.rim.blackberry.api.mail.TextBodyPart;
  30
+import net.rim.blackberry.api.mail.Transport;
  31
+import net.rim.device.api.lbs.picker.LocationPicker;
  32
+import net.rim.device.api.lbs.picker.MapsLocationPicker;
  33
+import net.rim.device.api.script.Scriptable;
  34
+import net.rim.device.api.script.ScriptableFunction;
  35
+import net.rim.device.api.ui.UiApplication;
  36
+import net.rim.device.api.gps.*;
  37
+import net.rim.device.api.io.IOUtilities;
  38
+import net.rim.device.api.system.Application;
  39
+import net.rim.device.api.ui.*;
  40
+import net.rim.device.api.ui.component.*;
  41
+import net.rim.device.api.ui.container.*;
  42
+
  43
+public final class MailAttachmentFunction extends ScriptableFunction {
  44
+	
  45
+	public static final String NAME = "send";
  46
+	//private 
  47
+	
  48
+	public Object invoke(Object obj, Object[] args) throws Exception
  49
+	{
  50
+		//input = (email address, subject, body, full attachment path)
  51
+       	
  52
+		String email = (String)args[0].toString();
  53
+	    String subject = (String)args[1].toString();
  54
+	    String body = (String)args[2].toString();
  55
+	    String attachment = (String)args[3].toString();
  56
+	    String fName = attachment;
  57
+	    byte[] data = null;
  58
+	    FileConnection fconn = null;
  59
+	    DataInputStream is = null;
  60
+	    try{
  61
+	    fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
  62
+	    is = fconn.openDataInputStream();             
  63
+	    data = IOUtilities.streamToBytes(is);
  64
+    	    is.close();
  65
+    	    fconn.close();
  66
+	    } catch (Exception ex) {
  67
+	    	 Dialog.inform("Error in file path: " + ex.toString());
  68
+	    	 return new Boolean(false);
  69
+	    }
  70
+      //create a multipart
  71
+        Multipart mp = new Multipart();
  72
+
  73
+        //create the file
  74
+        SupportedAttachmentPart sap = new SupportedAttachmentPart(mp,"application/x-example",attachment,data);
  75
+
  76
+        TextBodyPart tbp = new TextBodyPart(mp, body);
  77
+
  78
+        //add the file to the multipart
  79
+        mp.addBodyPart(tbp);
  80
+        mp.addBodyPart(sap);
  81
+
  82
+        //create a message in the sent items folder
  83
+        Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);
  84
+
  85
+        Message message = new Message(folders[0]);
  86
+        Address toAdd = new Address(email,"my email");
  87
+        Address toAdds[] = new Address[1];
  88
+        toAdds[0] = toAdd;
  89
+        message.addRecipients(Message.RecipientType.TO, toAdds);
  90
+        message.setSubject(subject);
  91
+        message.setContent(mp);
  92
+        Session session = Session.getDefaultInstance();
  93
+        Transport trans = session.getTransport();
  94
+        //add recipients to the message and send
  95
+        boolean sent = false;
  96
+        try {
  97
+             trans.send(message);
  98
+             sent = true;
  99
+        } catch (Exception e) {
  100
+             Dialog.inform("Error while sending: " + e.toString());
  101
+        }
  102
+    
  103
+
  104
+    return new Boolean(sent);
  105
+	}
  106
+
  107
+	
  108
+
  109
+}
  110
+
43  Smartphone/MailAttachment/webworks/mail/attachment/MailAttachmentNamespace.java
... ...
@@ -0,0 +1,43 @@
  1
+/*
  2
+* Copyright 2010-2012 Research In Motion Limited.
  3
+*
  4
+* Licensed under the Apache License, Version 2.0 (the "License");
  5
+* you may not use this file except in compliance with the License.
  6
+* You may obtain a copy of the License at
  7
+*
  8
+* http://www.apache.org/licenses/LICENSE-2.0
  9
+*
  10
+* Unless required by applicable law or agreed to in writing, software
  11
+* distributed under the License is distributed on an "AS IS" BASIS,
  12
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+* See the License for the specific language governing permissions and
  14
+* limitations under the License.
  15
+*/
  16
+package webworks.mail.attachment;
  17
+
  18
+import net.rim.device.api.script.Scriptable;
  19
+
  20
+public final class MailAttachmentNamespace extends Scriptable {
  21
+	
  22
+	public static final String FIELD_INVOKE_LP = MailAttachmentFunction.NAME;
  23
+	public static final String NAME = "webworks.mail.attachment";
  24
+	
  25
+	private MailAttachmentFunction mailAttachmentFunc;
  26
+
  27
+	public MailAttachmentNamespace() {
  28
+		this.mailAttachmentFunc = new MailAttachmentFunction();
  29
+	}
  30
+
  31
+	// The getField() function is called when the 
  32
+	//  dot '.' extender is used on your JavaScript object.
  33
+	public Object getField(String name) throws Exception {
  34
+		if (name.equals(FIELD_INVOKE_LP)) {
  35
+			return new MailAttachmentFunction();
  36
+		} 
  37
+		return super.getField(name);
  38
+	}
  39
+	
  40
+	public boolean putField(String field, Object value) throws Exception {
  41
+        return super.putField(field, value);
  42
+    }
  43
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.