[TUT] Adding Email functionality to your plugin (JavaMail)

Discussion in 'Resources' started by zeeveener, Aug 5, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    zeeveener

    So, I recently decided that I wanted to add an email feature to a plugin I was writing. The reason for it was to send myself an email anytime there was an error, so I knew right away.

    Considering that there is an API you can use (JavaMail), I thought it would be simple like adding Vault or any other Bukkit API to your plugin. WRONG! I was so wrong haha

    After spending probably a week trying to search every corner of the interwebs and hacking my plugin to shit, I figured out a method that works nicely. I will write it out so people don't have to go through the same trouble that I did...

    Note: There are some nice pictures to help you along your way. If you know what is going on then skip the spoilers because its the very basics, but if you are a new coder, they are there for you.

    First things first, you will need the required libraries. JavaMail is a standalone .jar file, but it still requires another .jar to pull code from.
    JavaBeans Activation Framework can be downloaded from: http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html

    Download both of those and place the unzipped folders somewhere easy to access. I placed them into the server folder where CraftBukkit.jar resides...

    Now, you are going to have to set up your plugin to read off of these files.
    The method that I use currently may have an extra step or six above what is required, but this is what I had to do to get it to work so if you have a better method, I am all ears :)

    So, let's start from a fresh plugin in Eclipse named Emailer.
    Show Spoiler
    [​IMG]


    Right click on the src folder and create a new package. Im going to name mine: me.zeeveener.Emailer
    Show Spoiler
    [​IMG]

    Show Spoiler
    [​IMG]


    Now right click on the package and add a new class. I called mine Emailer
    Show Spoiler
    [​IMG]


    Now, we want to add another folder for where we will import the required classes...
    Right click on the Project and add a new folder called lib.
    Show Spoiler
    [​IMG]

    Show Spoiler
    [​IMG]


    Right click on the lib folder and Import. We want to import the two libraries (mail.jar and activation.jar)
    Show Spoiler
    [​IMG]

    Show Spoiler
    [​IMG]

    Show Spoiler
    [​IMG]

    Show Spoiler
    [​IMG]


    After importing the two files, Right click on them and Add to Build Path
    Show Spoiler
    [​IMG]


    Now we will add Bukkit to our Build Path as well. Can't really do anything without that now can we?
    Show Spoiler
    [​IMG]


    Now, seeing as we have everything we need inside the plugin, lets move our required files to where they need to be on the outside of the plugin... We will move both activation.jar and mail.jar into a folder named lib that is located inside the plugins folder of the server...
    Show Spoiler
    [​IMG]


    Let's start working on the code of the plugin now. We are going to set up a very simple plugin that emails you upon startup. You can figure out how to make it work for your own purposes on your own.
    Also, I use the Gmail mail servers for this. If you have another email method just Google how to use JavaMail with your email provider.
    Ok. So let's start by adding the required stuff for the plugin. Inside the main class, extend JavaPlugin and add the unimplemented methods.
    Show Spoiler
    [​IMG]


    Now add the plugin.yml inside the src folder and add the required stuff for that.
    Show Spoiler
    [​IMG]


    Inside the main class, let's add a new function called email(). It will send a simple text email message just to let us know that the server is online. You will have to import all the classes that the function is calling on...
    Show Spoiler
    [​IMG]

    The code, so you can copy it:
    Code:java
    1. public void email(){
    2. Properties props = new Properties();
    3. props.put("mail.smtp.host", "smtp.gmail.com");
    4. props.put("mail.smtp.socketFactory.port", "465");
    5. props.put("mail.smtp.socketFactory.class",
    6. "javax.net.ssl.SSLSocketFactory");
    7. props.put("mail.smtp.auth", "true");
    8. props.put("mail.smtp.port", "465");
    9. Session session = Session.getInstance(props,
    10. new javax.mail.Authenticator() {
    11. protected PasswordAuthentication getPasswordAuthentication() {
    12. return new PasswordAuthentication("[U]Username[/U]","Password");
    13. //[U]Username[/U] is the [U]username[/U] to your actual email...
    14. //Password is the password you use to log into that email address
    15. }
    16. });
    17. try {
    18. Message message = new MimeMessage(session);
    19. message.setFrom(new InternetAddress("address[U]@whatever.com[/U]"));
    20. //address[U]@whatever.com[/U] is what you want the email's FROM area to look like, although it seems it just uses the email you logged in with...
    21. message.setRecipients(Message.RecipientType.TO,
    22. InternetAddress.parse("to[U]@email.com[/U]"));
    23. //to[U]@email.com[/U] is the address you are sending this email to
    24. message.setSubject("[[U]Emailer[/U]] Your server has just come online!");
    25. message.setText("The server that we are running has started. People are most likely playing on it because it is SO AWESOME!");
    26. Transport.send(message);
    27. } catch (MessagingException e) {
    28. throw new RuntimeException(e);
    29. }
    30. }

    Show Spoiler
    [​IMG]


    Continued below due to image constraints.
     
  2. Offline

    evilmidget38

    I really like it, thanks for making it. I think I'll set something like this up for Bukkit Arena, make it a bit easier for bug reports and for collecting mass information.

    Also, on the first post you have
    "Right click on the lib folder and Import. We want to import the two classes (main.jar and activation.jar)"
    It should be
    "Right click on the lib folder and Import. We want to import the two classes (mail.jar and activation.jar)"
     
  3. Offline

    zeeveener

    Thanks fixed.
     
  4. Offline

    Kodfod

    Amazing. Great TuT!
     
  5. These aren't classes xD they are jars...
    nice tut though:)
     
  6. Offline

    evilmidget38

    Yeah, just noticed that. All I was doing was changing main to mail, as that's a spelling typo.
     
  7. Awesome, think I'll add this to a plugin I'm making.
     
    zeeveener likes this.
  8. Offline

    dvdbrander

    I don't think you should ever spread a plugin like this, since there's something called decompilers... Just a warning to everybody, Be carefull with entering passwords inside a jar! Always use config files or so.
     
    bobacadodl likes this.
  9. I would suggest a dedicated email

    Got it set up, pretty awesome

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  10. Offline

    Theodossis

    Problem on line: Transport.send(message);
    Please help!
     
  11. Offline

    javoris767

  12. Offline

    zeeveener

  13. Offline

    hice3000

    Thank you for this, but I think I'll connect to a side which sends the mail per php, so much easier ;)
     
  14. Im getting this error on start up..?
    Code:
    01:14:38 [SEVERE] Could not load 'plugins\ReportMailer.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ClassNotFoundException: me.M
    rSamCraft.ReportMailer
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:184)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:305)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:230)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.loadPlugins(CraftServer.jav
    a:235)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.<init>(CraftServer.java:213
    )
            at net.minecraft.server.v1_4_6.PlayerList.<init>(PlayerList.java:52)
            at net.minecraft.server.v1_4_6.DedicatedPlayerList.<init>(SourceFile:11)
     
            at net.minecraft.server.v1_4_6.DedicatedServer.init(DedicatedServer.java
    :104)
            at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:
    399)
            at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:84
    9)
    Caused by: java.lang.ClassNotFoundException: me.MrSamCraft.ReportMailer
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader
    .java:80)
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:53)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:173)
            ... 9 more
    >
     
  15. Offline

    ImDeJay

    Neat idea

    The only thing i see wrong with it, which i dont know how someone hasnt already picked it up but if someone downloads your jar file and decompiles it, they can find your email and password very easily since it is put plainly within your code.

    Keep that in mind.

    *EDIT*
    and do please correct me if im wrong, but im pretty sure this is the case.
     
  16. Offline

    zeeveener

    The referenced jar files aren't mine... They are publicly available API's. Which jar would have my email and pass? Or are you talking in general for if someone was to do this themselves?
     
  17. Offline

    ImDeJay

    Code:
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("[U]Username[/U]","Password");
    //[U]Username[/U] is the [U]username[/U] to your actual email...
    //Password is the password you use to log into that email address
    That is where you visibly put in your email and password.

    Im not sure which class it is in, but im sure if you compiled it someone could decompile it and see your email and pass.
     
  18. Offline

    zeeveener

    You're right, but who would think to do that? Or, if I was smart, I would create a separate email solely for this plugin, or a general email for all my plugins with this functionality. That way, if it gets hacked, woopdy doo, they don't have my main email.
     
  19. Offline

    ImDeJay


    Im not sure if anybody would think to do it or not, some people actually do do that kinda stuff. for the simple reason that people's emails are attached to paypal accounts and other things.

    Just thought i'd warn you so you will be aware to not put your main email address inside the plugin.
     
  20. Offline

    teunie75

    Great tutorial!
    If anyone wants to use HTML in the mail, it worked for me when mailing to outlook
    Use: "message.setContent("HTML stuff", "text/html; charset=utf-8");"
     
  21. Offline

    Flybelette

    Anyone know how to make it Async ? Cause it cause 120 skipped ticks all times it send a Mail ^^, Thanks in advance !
     
  22. Offline

    zeeveener

  23. Offline

    apple2114

    i have this wierd error i need help with for the code heres the error..
    Code:
    org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: javax/mail/MessagingException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:184) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugins(CraftServer.java:255) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.<init>(CraftServer.java:233) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        at net.minecraft.server.v1_7_R1.PlayerList.<init>(PlayerList.java:63) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        at net.minecraft.server.v1_7_R1.DedicatedPlayerList.<init>(SourceFile:14) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:126) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:414) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
    Caused by: java.lang.NoClassDefFoundError: javax/mail/MessagingException
        at java.lang.Class.forName0(Native Method) ~[?:1.7.0_51]
        at java.lang.Class.forName(Class.java:270) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:173) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        ... 9 more
    Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366) ~[?:1.7.0_51]
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355) ~[?:1.7.0_51]
        at java.security.AccessController.doPrivileged(Native Method) ~[?:1.7.0_51]
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader.java:80) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:53) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425) ~[?:1.7.0_51]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ~[?:1.7.0_51]
        at java.lang.Class.forName0(Native Method) ~[?:1.7.0_51]
        at java.lang.Class.forName(Class.java:270) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:173) ~[craftbukkit.jar:git-Bukkit-1.6.4-R2.0-12-g378718e-b2932jnks]
        ... 9 more
    

    heres my code..
    Code:java
    1. public void email(){
    2. Properties props = new Properties();
    3. props.put("mail.smtp.host", "smtp.gmail.com");
    4. props.put("mail.smtp.socketFactory.port", "465");
    5. props.put("mail.smtp.socketFactory.class",
    6. "javax.net.ssl.SSLSocketFactory");
    7. props.put("mail.smtp.auth", "true");
    8. props.put("mail.smtp.port", "465");
    9. Session session = Session.getInstance(props,
    10. new javax.mail.Authenticator() {
    11. protected PasswordAuthentication getPasswordAuthentication() {
    12. return new PasswordAuthentication("[email]DcDenver.DnsDojo.Com@gmail.com[/email]","***");
    13. //[U]Username[/U] is the [U]username[/U] to your actual email...
    14. //Password is the password you use to log into that email address
    15. }
    16. });
    17. try{
    18. Message message = new MimeMessage(session);
    19. message.setFrom(new InternetAddress("[email]DcDenver.DnsDojo.Com@gmail.com[/email]"));
    20. //address[U]@whatever.com[/U] is what you want the email's FROM area to look like, although it seems it just uses the email you logged in with...
    21. message.setRecipients(Message.RecipientType.TO,
    22. InternetAddress.parse("[email]DcDenver.DnsDojo.Com@gmail.com[/email]"));
    23. //to[U]@email.com[/U] is the address you are sending this email to
    24. message.setSubject("[Emailer] Your server has just come online!");
    25. message.setText("The server that we are running has started. People are most likely playing on it because it is SO AWESOME!");
    26. Transport.send(message);
    27. }catch (MessagingException e){
    28. throw new RuntimeException(e);
    29. }
    30. }
     
  24. Offline

    Rockett8855

    If you don't want your email and password to be seen, create an external yml/properties file that contains all that data, then load it as a generic, getConfig().getString("email.username"); and getConfig.getString("password");
     
  25. Offline

    apple2114

    lol i dont mind that i just need to fix the error

    if any of you want to text instead of email heres how you do that

    Sprint phonenumber@messaging.sprintpcs.com
    Verizon phonenumber@vtext.com
    T-Mobile phonenumber@tmomail.net
    AT&T phonenumber@txt.att.net
    AIM +1phonenumber

    change the begining like so

    ymail this does work
    • SMTP server: nokia.smtp.mail.yahoo.com
    • Use SSL
    • Port: 465
    • Use authentication
    • Account Name/Login Name: Your Nokia Mail ID (your email address without the "@nokiamail.com" or "ovi.com" domain, for example, "jo.bloggs")
    • Email Address: Your Nokia Mail address (for example, "jo.bloggs@nokiamail.com" or "jo.bloggs@ovi.com")
    • Password: Your Nokia Mail password
    for gmail has errors

    SMTP Host: smtp.gmail.com
    SMTP Port: 587
    SSL Protocol: OFF
    TLS Protocol: ON
    SMTP Username: (your Gmail username)
    SMTP Password: (your Gmail password)

    but should work also remember to make a lib folder in your servers plugins folder and shove the activation.jar and mail.jar in it :) hope this helped anyone with errors on this

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  26. Offline

    user_90854156

    Bit of a bump, but could anyone explain this a little more in-depth, since none of the pictures are working?
     
  27. Offline

    ArsenArsen

    If you want to avoid that make email and pass in configs.
     
Thread Status:
Not open for further replies.

Share This Page