Archive | June, 2011

MessageBoard App #4 Email using Gmail and more nginx

30 Jun

So as is traditional a quick reminder for myself of what my todo list was looking like:

  • Switch to use the live DB currently being used by the PHP app. -mainly done – should be easy to switch over now
  • Make links automatically out of URLs in messages
  • Clean up html so it looks presentable (this could be a big one)
  • Either set up email notifications, or some alternative, e.g. posts to twitter proven in theory – using gmail
  • Setup a domain name. done
  • Setup a webserver, to stand in front of tomcat(which is running on port 8080) so can use port 80 (aka a reverse proxy server I’ve learnt) done, using nginx, but want to tidy up config and do some testing around logging in/out
  • So, as is traditional, I didn’t get to spend much time on it this week, if only I didn’t have any family or freinds or leisure activities I might have more time to write a crappy message board application. Oh well.

    But have managed to prove that I should be able to use a gmail account to send notification emails from – not sure if this is an advantage or not over setting up an email server, but in the spirit of just getting something working in whatever way is easiest it does the job.

    Also a bit of tweaking of nginx config seemed to be having no effect, despite “ctrl shift delete, delete all cache” in firefox, which stumped me till I found a default file in /etc/nginx/sites-available which was what seemed to be taking precedence. Or maybe nginx wasn’t reloading the config like I thought it was when I ran “nginx -s reload”. Anyway, it’s working now, but need to experiment a bit to be confident that I’ve got any sort of a clue as to why.

    Code for emailing from Java using gmail (mostly from http://www.velocityreviews.com/forums/t141237-send-smtp-mail-using-javamail-with-gmail-account.html
    ) , sorry wordpress doesn’t format better …:

    package uk.co.mrdw.util;

    import java.security.Security;
    import java.util.Date;
    import java.util.Properties;

    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;

    /**
    * based on code here
    * http://www.velocityreviews.com/forums/t141237-send-smtp-mail-using-javamail-with-gmail-account.html
    * @author Mike
    *
    */
    public class Emailer {
    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String SMTP_PORT = "465";
    private static final String emailMsgTxt = "Test Message Contents";
    private static final String emailSubjectTxt = "A test from gmail";
    private static final String emailFromAddress = "xxxxxxxxxx@gmail.com";
    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static final String[] sendTo = { "yyyyyyy@gmail.com"};

    public static void main(String args[]) throws Exception {

    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

    new Emailer().sendSSLMessage(sendTo, emailSubjectTxt,
    emailMsgTxt, emailFromAddress);
    System.out.println("Sucessfully Sent mail to All Users");
    }

    public void sendSSLMessage(String recipients[], String subject,
    String message, String from) throws MessagingException {
    boolean debug = true;

    Properties props = new Properties();
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.port", SMTP_PORT);
    props.put("mail.smtp.socketFactory.port", SMTP_PORT);
    props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.put("mail.smtp.socketFactory.fallback", "false");

    Session session = Session.getDefaultInstance(props,
    new javax.mail.Authenticator() {

    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("xxxxxxxxxxx@gmail.com", "PASSWORD");
    }
    });

    session.setDebug(debug);

    Message msg = new MimeMessage(session);
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++) {
    addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
    }
    }

    MessageBoard App #3 – Domain Name and nginx

    21 Jun

    (Not sure if this is actually the 3rd post on the message board – but will say it is for the sake of having a title)
    So just a quick reminder for myself of what my todo list was looking like:

  • Switch to use the live DB currently being used by the PHP app.
  • Make links automatically out of URLs in messages
  • Clean up html so it looks presentable (this could be a big one)
  • Either set up email notifications, or some alternative, e.g. posts to twitter
  • Setup a domain name.
  • Setup a webserver, which stand infront of tomcat (which is running on port 8080) so can use port 80
  • First one is pretty much done, although I haven’t tested what the performance is going to be like. I might to just copy the database over to the new server if it’s terrible, but would be nice to be able to run the old PHP app.

    Number 4 (domain name) is done – got http://www.msgbrd.co.uk from 123-reg.co.uk , I didn’t do a lot of research on how domain name’s work, but it was pretty easy to set up. I’ve got a pdf or two I might go back and read if I feel like I really need to know more.

    For number 5 (webserver) I’ve taken my brother’s advice and used nginx, seems it’s smaller and faster than apache and is designed to act in the way I want it – i.e. passing on requests to tomcat. I’ve got it installed and running, and can edit the config to prevent the processing of requests, or just display a welcome to nginx screen. Need to do a little more reading to see how to get it to forward to stuff on to tomcat.

    I haven’t had much time again to spend on it this week, so pleased to be making progress in the right direction, and feels like I’m at a point where anytime I’ve got a spare 30 minutes I can sit down and do something useful on it, which is good.

    Blogging like this is working well to just give me a little bit extra motivation. I’m pretty much sure I could safely use this blog as a place to store my passwords as I’m the only one reading it, which is how I like it at the moment. Although there are loads of spammers commenting, which is a bit annoying (I’m not accepting their comments , so they shouldn’t be visible), maybe I need to upgrade wordpress to get a better captcha.

    Little Progress

    14 Jun

    So another week over and what have we done ?
    A new one just begining, I hope it is fun.

    Well, I’ve remembered two other things I need to do, so my new list is as follows:

    Remaining tasks before I consider myself eligible for the underwater housing treat are:

      Switch to use the live DB currently being used by the PHP app.
      Make links automatically out of URLs in messages
      Clean up html so it looks presentable (this could be a big one)
      Either set up email notifications, or some alternative, e.g. posts to twitter
      Setup a domain name.
      Setup a webserver, which stand infront of tomcat (which is running on port 8080) so can use port 80

    So that’s progress of a sort, but basically the wrong sort.

    One thing I have done is do the majority of the first task,so it should now be very simple to switch over to use the live database that the PHP app uses. This is important so I can have a period of them running side by side, and then move over to a database hosted on my memset server when I’m confident it’s working.

    One thing I can say is that my coding so far is very rough and ready, and not something I’d want to show anyone else. So maybe there’l be a post in a few weeks, bemoaning the fact that I didn’t do it properly in the first place. Or maybe there will be one saying how it was more important to have just done something, and have got it out there. At the moment I feel like the second is more likely to be the case, at least in terms of keeping me motivated, but we’ll see what problems my slap dash approach throughs up in the future, maybe.

    Otherwise my ankle seems to have recovered well from a sprain, so various races and club training nights provide an excuse for so little progress.

    Slow Progress – A Message Board Project

    7 Jun

    So over a year since my last post, I’m going to reduce the quality threshold on what I post and see if I can post more regularly.
    One of the posts has actually been useful to me to come back and remember what I was doing, so as long as it’s ocasionally useful to me then it’s worth doing.

    So my current home programming project is converting the message board that I’ve set up for a couple of running clubs, e.g. Wrekin Road Runners, currently hosted here, http://www.mrdw.co.uk/wrr/noticeboard.php , from a crappy php application, to a crappy Java one (although it will be allowing ajax posting of messages and logging in) once I’ve done that and got it up on line I’m going to treat myself to an underwater housing for my camera http://www.camerasunderwater.co.uk/ewa_marine/slr/ub-series.html

    So far I’ve got a Java app running on tomcat on a Memset virtual server ( https://www.memset.com/dedicated-servers/virtual.php ) using mysql that roughly does what I need. I went with Memset partly because of there claim to be green and a vague desire to support a UK company. Linode ( http://www.linode.com/ ) seems to be the default choice for this sort of thing at the moment, and I might try them for a comparison at somepoint.

    Remaining tasks before I consider myself eligible for the underwater housing treat are:

  • Switch to use the live DB currently being used by the PHP app.
  • Make links automatically out of URLs in messages
  • Clean up html so it looks presentable (this could be a big one)
  • Either set up email notifications, or some alternative, e.g. posts to twitter
  • Once I’ve done that, there’s loads I could do, but think it might be a usefull base project for trying out different web frameworks, got a plan to learn ruby on rails, so maybe writing it as a ruby on rails project will come. Also from point of view of making the message board useful, allowing logons from Facebook, twitter , openid etc. would be good, and an rss feed is pretty much essential I think.

    I’m undecided as to whether there is really any point running clubs having message boards instead of just using something free like facebook, or a google group. There does seem to be something in not having to force people to create another logon for a little message board for people who all know each other (currently everyone posting uses the same shared password) , and also for not forcing people to sign up to something like facebook if they don’t want to. (Plus I don’t really like facebook). Also as it’s dedicated to just providing a simple message board it can be less cluttered and I don’t intend on having adverts. (I don’t really like adverts). Anyway, as long as it is useful as a practical learning project for me it being of any use to anyone else is a bonus.

    Right, now I’ve blogged about it, maybe I’ll do some work on it.