[java-dev] Re: net.mail problems
Myer
myernore at yahoo.com
Tue Mar 25 01:17:01 MDT 2008
- Previous message: [java-dev] Re: net.mail problems
- Next message: [java-dev] Re: net.mail problems
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hello,
A while ago I created a mxj program to email mp3s of performances as attachments to gmails. I've attached everything. Let me know if you have suggestions or comments.
Myer
note - you need to 1.) start pop3 support in your gmail account settings 2.) get activation.jar and mail.jar, and put them in your mxj classpath, and edit the config file so it loads the jars
to get this to work
************************************
********BPATCHER IMPLEMENTATION*****
************************************
max v2;
#N vpatcher 27 84 746 317;
#P origin 10 -49;
#P window setfont "Sans Serif" 18.;
#P window linecount 1;
#P hidden comment 725 107 171 9109522 <-- handy bpatcher;
#P window setfont "Sans Serif" 10.;
#P comment 109 5 580 9109514 you have to enable pop3 email in your gmail settings and install mail.jar and activation.jar from sun in the mxj classpath.;
#P window setfont "Sans Serif" 18.;
#P window linecount 3;
#P hidden comment 218 370 140 9109522 dependencies: mail.jar activation.jar;
#P window setfont "Sans Serif" 14.;
#P window linecount 2;
#P hidden comment 360 383 347 9109518 Myer Nore 2008 \, with generous help from day 11 of Sams "Teach Yourself J2EE in 21 Days.";
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P hidden comment 925 32 62 9109513 filename;
#N comlet filename to attach;
#P hidden inlet 908 32 15 0;
#N comlet bang: deliver email with current settings;
#P hidden inlet 711 32 15 0;
#P comment 27 28 48 9109513 username;
#P user textedit 11 119 341 158 32968 139 9;
#P comment 289 107 43 9109513 filename;
#P hidden newex 147 390 48 9109513 print info;
#P hidden newex 11 390 51 9109513 print gmail;
#P hidden newex 68 390 75 9109513 print exception;
#P user textedit 353 41 510 62 32968 139 9;
#P user textedit 182 41 339 62 32968 139 9;
#P user textedit 11 41 103 62 32968 139 9;
#P user textedit 11 80 103 100 32968 139 9;
#P comment 25 67 48 9109513 password;
#P comment 353 29 18 9109513 cc;
#P user textedit 524 41 679 62 32968 139 9;
#P comment 524 29 23 9109513 bcc;
#P comment 101 44 58 9109513 @gmail.com;
#P comment 182 29 16 9109513 to;
#P user textedit 11 177 338 197 32968 139 9;
#P user textedit 351 75 678 199 32968 139 9;
#P window setfont "Sans Serif" 18.;
#P message 200 77 70 9109522 deliver;
#P window setfont "Sans Serif" 9.;
#P message 132 81 32 9109513 clear;
#P comment 326 74 30 9109513 body;
#P comment 290 165 42 9109513 subject;
#P window setfont "Sans Serif" 12.;
#P hidden newex 11 361 85 9109516 mxj MXJGmail;
#P window setfont "Sans Serif" 9.;
#P hidden newex 711 55 27 9109513 t b b;
#P hidden comment 728 32 62 9109513 bang delivers;
#P hidden newex 908 53 57 9109513 prepend set;
#N vpatcher 59 47 1206 485;
#P window setfont "Sans Serif" 12.;
#P window linecount 4;
#P comment 829 197 88 9109516 replace "text" with the appropriate field;
#P outlet 350 378 15 0;
#P inlet 734 57 15 0;
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#N vpatcher 15 55 271 367;
#P outlet 60 263 15 0;
#P inlet 60 38 15 0;
#P window setfont "Sans Serif" 9.;
#P newex 60 128 69 9109513 mxj list.Length;
#P newex 60 173 27 9109513 > 1;
#P newex 60 218 27 9109513 gate;
#P newex 60 83 27 9109513 t l l;
#P connect 4 0 0 0;
#P fasten 0 1 3 0 82 115 65 115;
#P connect 3 0 2 0;
#P connect 2 0 1 0;
#P connect 1 0 5 0;
#P fasten 0 0 1 1 65 107 50 107 50 203 82 203;
#P pop;
#P newobj 734 88 89 9109513 p gate_blank_fields;
#P inlet 638 57 15 0;
#N vpatcher 15 55 271 367;
#P outlet 60 263 15 0;
#P inlet 60 38 15 0;
#P window setfont "Sans Serif" 9.;
#P newex 60 128 69 9109513 mxj list.Length;
#P newex 60 173 27 9109513 > 1;
#P newex 60 218 27 9109513 gate;
#P newex 60 83 27 9109513 t l l;
#P connect 4 0 0 0;
#P fasten 0 1 3 0 82 115 65 115;
#P connect 3 0 2 0;
#P connect 2 0 1 0;
#P connect 1 0 5 0;
#P fasten 0 0 1 1 65 107 50 107 50 203 82 203;
#P pop;
#P newobj 638 88 89 9109513 p gate_blank_fields;
#P inlet 542 57 15 0;
#N vpatcher 15 55 271 367;
#P outlet 60 263 15 0;
#P inlet 60 38 15 0;
#P window setfont "Sans Serif" 9.;
#P newex 60 128 69 9109513 mxj list.Length;
#P newex 60 173 27 9109513 > 1;
#P newex 60 218 27 9109513 gate;
#P newex 60 83 27 9109513 t l l;
#P connect 4 0 0 0;
#P fasten 0 1 3 0 82 115 65 115;
#P connect 3 0 2 0;
#P connect 2 0 1 0;
#P connect 1 0 5 0;
#P fasten 0 0 1 1 65 107 50 107 50 203 82 203;
#P pop;
#P newobj 542 88 89 9109513 p gate_blank_fields;
#P inlet 446 57 15 0;
#P inlet 350 57 15 0;
#N vpatcher 15 55 271 367;
#P outlet 60 263 15 0;
#P inlet 60 38 15 0;
#P window setfont "Sans Serif" 9.;
#P newex 60 128 69 9109513 mxj list.Length;
#P newex 60 173 27 9109513 > 1;
#P newex 60 218 27 9109513 gate;
#P newex 60 83 27 9109513 t l l;
#P connect 4 0 0 0;
#P fasten 0 1 3 0 82 115 65 115;
#P connect 3 0 2 0;
#P connect 2 0 1 0;
#P connect 1 0 5 0;
#P fasten 0 0 1 1 65 107 50 107 50 203 82 203;
#P pop;
#P newobj 350 88 89 9109513 p gate_blank_fields;
#P inlet 254 57 15 0;
#N vpatcher 15 55 271 367;
#P outlet 60 263 15 0;
#P inlet 60 38 15 0;
#P window setfont "Sans Serif" 9.;
#P newex 60 128 69 9109513 mxj list.Length;
#P newex 60 173 27 9109513 > 1;
#P newex 60 218 27 9109513 gate;
#P newex 60 83 27 9109513 t l l;
#P connect 4 0 0 0;
#P fasten 0 1 3 0 82 115 65 115;
#P connect 3 0 2 0;
#P connect 2 0 1 0;
#P connect 1 0 5 0;
#P fasten 0 0 1 1 65 107 50 107 50 203 82 203;
#P pop;
#P newobj 254 88 89 9109513 p gate_blank_fields;
#P inlet 158 57 15 0;
#N vpatcher 15 55 271 367;
#P outlet 60 263 15 0;
#P inlet 60 38 15 0;
#P window setfont "Sans Serif" 9.;
#P newex 60 128 69 9109513 mxj list.Length;
#P newex 60 173 27 9109513 > 1;
#P newex 60 218 27 9109513 gate;
#P newex 60 83 27 9109513 t l l;
#P connect 4 0 0 0;
#P fasten 0 1 3 0 82 115 65 115;
#P connect 3 0 2 0;
#P connect 2 0 1 0;
#P connect 1 0 5 0;
#P fasten 0 0 1 1 65 107 50 107 50 203 82 203;
#P pop;
#P newobj 158 88 89 9109513 p gate_blank_fields;
#P inlet 62 57 15 0;
#P hidden newex 254 175 124 9109513 mxj list.Replace 0 fileName;
#P hidden newex 734 315 102 9109513 mxj list.Replace 0 text;
#P hidden newex 158 147 125 9109513 mxj list.Replace 0 password;
#P hidden newex 638 287 109 9109513 mxj list.Replace 0 bcc;
#P hidden newex 542 259 104 9109513 mxj list.Replace 0 cc;
#P hidden newex 62 119 127 9109513 mxj list.Replace 0 username;
#P hidden newex 350 203 125 9109513 mxj list.Replace 0 subject;
#P window linecount 0;
#N vpatcher 15 55 271 367;
#P outlet 60 263 15 0;
#P inlet 60 38 15 0;
#P window setfont "Sans Serif" 9.;
#P newex 60 128 69 9109513 mxj list.Length;
#P newex 60 173 27 9109513 > 1;
#P newex 60 218 27 9109513 gate;
#P newex 60 83 27 9109513 t l l;
#P connect 4 0 0 0;
#P fasten 0 1 3 0 82 115 65 115;
#P connect 3 0 2 0;
#P connect 2 0 1 0;
#P connect 1 0 5 0;
#P fasten 0 0 1 1 65 107 50 107 50 203 82 203;
#P pop;
#P newobj 62 88 89 9109513 p gate_blank_fields;
#P window linecount 1;
#N vpatcher 15 55 271 367;
#P outlet 60 263 15 0;
#P inlet 60 38 15 0;
#P window setfont "Sans Serif" 9.;
#P newex 60 128 69 9109513 mxj list.Length;
#P newex 60 173 27 9109513 > 1;
#P newex 60 218 27 9109513 gate;
#P newex 60 83 27 9109513 t l l;
#P connect 4 0 0 0;
#P fasten 0 1 3 0 82 115 65 115;
#P connect 3 0 2 0;
#P connect 2 0 1 0;
#P connect 1 0 5 0;
#P fasten 0 0 1 1 65 107 50 107 50 203 82 203;
#P pop;
#P newobj 446 88 89 9109513 p gate_blank_fields;
#P hidden newex 446 231 103 9109513 mxj list.Replace 0 to;
#P window setfont "Sans Serif" 12.;
#P window linecount 2;
#P comment 829 69 166 9109516 only let messages through if they aren't blank;
#P connect 11 0 3 0;
#P connect 3 0 5 0;
#P connect 13 0 12 0;
#P connect 12 0 8 0;
#P connect 15 0 14 0;
#P connect 14 0 10 0;
#P connect 17 0 16 0;
#P connect 16 0 4 0;
#P connect 4 0 25 0;
#P connect 5 0 25 0;
#P connect 8 0 25 0;
#P connect 10 0 25 0;
#P connect 1 0 25 0;
#P connect 6 0 25 0;
#P connect 7 0 25 0;
#P connect 9 0 25 0;
#P connect 18 0 2 0;
#P connect 2 0 1 0;
#P connect 20 0 19 0;
#P connect 19 0 6 0;
#P connect 22 0 21 0;
#P connect 21 0 7 0;
#P connect 24 0 23 0;
#P connect 23 0 9 0;
#P pop;
#P hidden newobj 11 320 339 9109513 p testArgLength;
#P window linecount 2;
#P hidden comment 356 314 232 9109513 username password filename subject to cc bcc body \; see inside to see how arguments are passed;
#P bpatcher 5 25 685 181 0 0 0;
#P window setfont "Sans Serif" 14.;
#P window linecount 1;
#P comment 3 3 107 9109518 MXJGmail note:;
#P hidden fasten 6 1 21 0 733 25 16 25;
#P lcolor 6;
#P hidden fasten 10 0 21 0 137 107 -3 107 -3 19 16 19;
#P lcolor 4;
#P hidden fasten 6 1 20 0 733 25 5 25 5 68 16 68;
#P lcolor 6;
#P hidden fasten 10 0 20 0 137 107 -3 107 -3 68 16 68;
#P lcolor 4;
#P hidden fasten 6 1 28 0 733 26 5 26 5 107 16 107;
#P lcolor 6;
#P hidden fasten 10 0 28 0 137 107 16 107;
#P lcolor 4;
#P hidden fasten 4 0 28 0 913 22 -12 22 -12 115 16 115;
#P hidden fasten 6 1 13 0 733 26 4 26 4 164 16 164;
#P lcolor 6;
#P hidden fasten 10 0 13 0 137 107 -3 107 -3 164 16 164;
#P lcolor 4;
#P hidden fasten 21 0 3 0 16 263 16 263;
#P lcolor 8;
#P hidden fasten 6 0 7 0 716 360;
#P hidden connect 3 0 7 0;
#P hidden connect 7 0 25 0;
#P hidden fasten 20 0 3 1 16 209 63 209;
#P lcolor 8;
#P hidden connect 7 1 24 0;
#P hidden fasten 28 0 3 2 16 231 110 231;
#P lcolor 8;
#P hidden connect 7 2 26 0;
#P hidden fasten 13 0 3 3 16 263 157 263;
#P lcolor 8;
#P hidden fasten 6 1 22 0 733 25 187 25;
#P lcolor 6;
#P hidden fasten 10 0 22 0 137 107 167 107 167 19 187 19;
#P lcolor 4;
#P hidden fasten 22 0 3 4 187 231 204 231;
#P lcolor 8;
#P hidden fasten 23 0 3 5 358 286 251 286;
#P lcolor 8;
#P hidden fasten 17 0 3 6 529 233 298 233;
#P lcolor 8;
#P hidden fasten 12 0 3 7 356 258 345 258;
#P lcolor 8;
#P hidden fasten 6 1 12 0 733 25 346 25 346 67 356 67;
#P lcolor 6;
#P hidden fasten 10 0 12 0 137 107 191 107 191 68 356 68;
#P lcolor 4;
#P hidden fasten 6 1 23 0 733 25 358 25;
#P lcolor 6;
#P hidden fasten 10 0 23 0 137 107 168 107 168 19 358 19;
#P lcolor 4;
#P hidden fasten 6 1 17 0 733 25 529 25;
#P lcolor 6;
#P hidden fasten 10 0 17 0 137 107 167 107 167 19 529 19;
#P lcolor 4;
#P hidden connect 30 0 6 0;
#P hidden fasten 11 0 6 0 205 112 176 112 176 13 703 13 703 51 716 51;
#P lcolor 1;
#P hidden connect 31 0 4 0;
#P pop;
***********************************
*********MXJGmail.java*************
***********************************
import com.cycling74.max.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class MXJGmail extends MaxObject {
private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final int SMTP_HOST_PORT = 465;
private static final int NORMAL_OUTLET = 0;
private static final int EXCEPTION_OUTLET = 1;
private String username = null;
private String password = null;
private String to = null;
private String cc = null;
private String bcc = null;
private String from = "nobody";
private String subject = null;
private String text = null;
private String fileName = null;
private String contentType = "text/html";
public MXJGmail( Atom args[] ) {
declareIO( 1,2 );
declareAttribute("username");
declareAttribute("password");
declareAttribute("to");
declareAttribute("cc");
declareAttribute("bcc");
declareAttribute("subject");
declareAttribute("text");
declareAttribute("fileName");
from = username + "@gmail.com";
}
public void bang ()
{
deliver();
}
public void clear( String message, Atom args[] ) {
username = password = to = cc = bcc = subject = text = fileName = null;
from = "nobody";
}
public void deliver() {
//Get System Properties object
Properties props = System.getProperties();
// Define properties
// it MUST be smtpS for gmail
props.put( "mail.transport.protocol", "smtps" );
props.put( "mail.smtps.host", SMTP_HOST_NAME );
props.put( "mail.smtps.auth", "true" );
// tells the system NOT to wait for verification
// of sent status from gmail
props.put( "mail.smtps.quitwait", "false" );
try {
// Get a mail Session object
// Pass SMTP properties to private constructor of new
// Session object
Session session = Session.getDefaultInstance( props );
// deal with transport directly because of tls and ssl
// which are required for gmail
Transport transport = session.getTransport();
// Create a new Message object
MimeMessage message = new MimeMessage( session );
//Pass the Mail Session as an argument and construct
//a MimeMessage with the Session
// Populate Message object
if ( from != "nobody" ) {
message.setFrom( new InternetAddress( from ) );
}
else
{
message.setFrom();
}
if ( to != null )
{
message.addRecipients( Message.RecipientType.TO,
InternetAddress.parse( to ) );
}
if ( cc != null )
{
message.addRecipients( Message.RecipientType.CC,
InternetAddress.parse( cc ) );
}
if ( bcc != null )
{
message.addRecipients( Message.RecipientType.BCC,
InternetAddress.parse( bcc ));
}
if ( subject != null )
{
message.setSubject( subject );
}
//Create message body part
BodyPart messageBodyPart = new MimeBodyPart();
//Specify that we want a Part interface for a new
//MIME object; construct object and assign it to
//messageBodyPart
if ( text != null )
{
messageBodyPart.setText( text );
}
if ( fileName != null )
{
//create a Multipart object to hold the BodyPart
Multipart multipart = new MimeMultipart();
multipart.addBodyPart( messageBodyPart );
//pass the textset BodyPart as an argument to the
//addBodyPart method of the multipart object
//Create the attachment body part
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource( fileName );
//make a capsule for the file that
//was referenced in the arguments
messageBodyPart.setDataHandler( new DataHandler(source) );
//pass DataSource to DataHandler so it can be
//interpreted by messageBodyPart
messageBodyPart.setFileName( fileName );
//create an official name for the file name
//referenced in the arguments - so that the
//MIME reader on the other end knows
//what to call it
multipart.addBodyPart( messageBodyPart );
//pass the BodyPart to the addBodyPart() method of the
//Multipart object so it can be added;
//insert the multipart into the message
message.setContent( multipart );
}
//send the message with mail transport object
transport.connect( SMTP_HOST_NAME, SMTP_HOST_PORT,
username, password );
transport.sendMessage( message,
message.getRecipients( Message.RecipientType.TO ) );
transport.close();
outlet(NORMAL_OUTLET, "sent");
} catch ( AddressException ae ) {
showException( null, ae );
outlet( EXCEPTION_OUTLET, "AddressException" );
} catch ( MessagingException me ) {
showException( null, me );
outlet( EXCEPTION_OUTLET, "MessagingException" );
}
}
}
--
"To Dwell is to Garden" - Martin Heidegger
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MXJGmail.class
Type: application/octet-stream
Size: 4046 bytes
Desc: not available
Url : http://www.cycling74.com/pipermail/java-dev/attachments/20080325/8876b276/MXJGmail.obj
- Previous message: [java-dev] Re: net.mail problems
- Next message: [java-dev] Re: net.mail problems
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
