[LIB] [v1.4.1] SMTP-Email - Send Emails with your plugin

Discussion in 'Resources' started by GermanCoding, May 18, 2014.

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

    jusjus112

    GermanCoding
    I am logged in, in gmail. but i got the same error. I dont know what im doing wrong. but what you said, gmail can block my ip-addres. but how do i turn it off. and i dont know!
    Here is an new error/debug: http://pastebin.com/TZJR5VGu
     
  2. Offline

    GermanCoding

    jusjus112
    This log is better, but is also missing some important lines.
    Nevertheless, I think I have now the cause for your problem. Try to go to https://security.google.com/settings/security/activity (you have to be logged in with the correct gmail/google account) and verify the smtp connection there.
    Another solution: You could try to solve the captcha (https://accounts.google.com/DisplayUnlockCaptcha). After this send an email with my lib. A problem could be that you have to unlock the captcha with the ip address of your server and not with your pc-ip-address.

    Your problem is caused when you send to many emails (http://www.serversmtp.com/en/smtp-server-google). Google does not like this. Or you are blocked by google if your ip address is not trustworthy.
     
  3. Offline

    GermanCoding

    Update to version 1.1! Now you can add file attachments!
     
  4. Offline

    GermanCoding

    Update to version 1.2! More compatibility to old smtp servers, it uses now quoted-printable encoding for the email by default. (If you want to change that, change the Content-Transfer-Encoding header before you call the content() function)
     
  5. Offline

    AppleMen

    Hello, I cant get the HTML function working. I saw what you explained about that earlier. Something with call the function add()..... But where do I add that? I've tried to put that under the
    Code:java
    1. email.add("Content-Type", "multipart/mixed;\r\n boundary=\"" + randomBoundary + "\"");
    And tried to replace it. None of the tries worked. Have I done something wrong? I'm sure my client supports HTML mails.
     
  6. Offline

    GermanCoding

    AppleMen

    You read an explanation for an old version. Actually, you just have to create a new email object and call the add("Content-Type", "text/html") function. You do not have to change anything in the smtp class. Afte that you call functions like to() and body() to complete the email object.

    I wrote this message on my phone, I can give you more and better help when I am at home.
     
  7. Offline

    AppleMen

    Could you give me an example on how to? I'm a little confused. I made the mail option inside a command function:
    Code:java
    1. String smpthost = "smtp.live.com"; // //Address of Outlook
    2. String mailsender = "[EMAIL][email protected][/EMAIL]"; //Your email
    3. String password = "***"; //Your password (Will be send encrypted to the server)"
    4. String receiver = p.getName(); //Name of the recipient
    5. String receiveremail = email; //Email of the recipient
    6. String content = "<strong>Content</strong>";
    7.  
    8. SMTP.sendEmail(smpthost, mailsender, password, "Server", receiver, receiveremail, "Subject", content, true);
     
  8. Offline

    GermanCoding

    AppleMen
    Code:java
    1. SMTP.Email email = SMTP.createEmptyEmail();
    2. email.add("Content-Type", "text/html");
    3. email.from("AppleMen", "address@mail");
    4. email.to("AppleMen", "email@address");
    5. email.subject("Subject");
    6. email.body("<strong>HTML formatted Email.</strong><p><i>special characters are converted into quoted-printable (ßäöü)</i>");
    7. SMTP.sendEmail("smtp.live.com", "address@mail", "****", email, true);


    Edit: I think these overloaded sendEmail() functions are confusing. I think I will change that to only one sendEmail() function, would this be better to understand?
     
  9. Offline

    AppleMen

    That would be easier yes.
     
  10. Offline

    GermanCoding

    Update to version 1.3. Only one sendEmail() function left, I hope this is more user-friendly.
     
  11. Offline

    AppleMen

    Much more user friendly! But I get some errors tho..

    Code:
    The method add(String, String) is undefined for the type String
    The method from(String, String) is undefined for the type String
    The method to(String, String) is undefined for the type String
    The method subject(String) is undefined for the type String
    The method body(String) is undefined for the type String
    Also
    Code:java
    1. class Email {
    Had to change to
    Code:java
    1. public class Email {
    Otherwise it isn't visable :p
     
  12. Offline

    GermanCoding

    AppleMen
    You are right with the public declaration (I only tested the code in the same package, so I do not saw that :rolleyes: ) , but I can not understand the errors, in my tests it always worked fine. Do you have maybe multiple classes that are called "String"? Can I see your code?

    Edit: Changed declaration of the email class to public.
     
  13. Offline

    AppleMen

    This is the class where I use the mail function
    https://gist.github.com/matthijs110/f34f45a8e41a1e1150f1
     
  14. Offline

    GermanCoding

    AppleMen
    Change that:
    Code:java
    1. SMTP.Email mail = SMTP.createEmptyEmail();
    2. email.add("Content-Type", "text/html");
    3. email.from("You", mailsender);
    4. email.to(receiver, receiveremail);
    5. email.subject("A subject");
    6. email.body("<b>Test</b>");


    to that:
    Code:java
    1. SMTP.Email mail = SMTP.createEmptyEmail();
    2. mail.add("Content-Type", "text/html");
    3. mail.from("You", mailsender);
    4. mail.to(receiver, receiveremail);
    5. mail.subject("A subject");
    6. mail.body("<b>Test</b>");


    The difference between your variable mail and email ;)
     
  15. Offline

    AppleMen

    Haha, I just saw it! Stupid mistake.. Thank you very much for making me this all clear and for making the application more user-friendly!
     
  16. Offline

    GermanCoding

    AppleMen
    No problem :) Tell me if you need more help.
     
  17. Offline

    GermanCoding

    Update to version 1.4. Improved indentation and syntax, added some comments, a bugfix (close the files after reading them) and some other things.
     
  18. Offline

    michael566

  19. Offline

    GermanCoding

    michael566
    As you can read in your error log, this is not a problem with the lib. Bukkit is unable to load your (test) plugin, because it can not create a new instance of your main class. If you need more help, please post the source code.
     
  20. Offline

    michael566

    GermanCoding So I made a separate plugin to test it out and I still get an error: http://pastebin.com/z9y9AtuB
    Here is my class for the command:
    Code:
    package me.michael566.email;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Email extends JavaPlugin {
     
        @Override
        public void onEnable() {
     
        }
     
     
        @Override
        public void onDisable() {
     
        }
     
     
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
            if (command.getName().equalsIgnoreCase("email")) {
                SMTP.Email email = SMTP.createEmptyEmail();
                email.add("", ""); //Headers for the email (useful for html) you do not have to set them)
                email.from("You", "hidden"); //The sender of the email.
                email.to("Me", "hidden"); //The recipient of the email.
                email.subject("test"); //Subject of the email
                email.body("Content of the email. Encoded in quoted-printable. You can use control \n characters or html if you set the header");
     
                SMTP.sendEmail(
                        "smtp.gmail.com", //Address of the smtp server. In many cases you can set it to null.
                        "hidden", //Your email (Used for login at the smtp server)
                        "hideden", //Your password (Used for login at the smtp server, will be send encrypted to the server)
                        email, //Email object to send.
                        false);
            }
            p.sendMessage("Email sent!");
     
            return true;
        }
    }
    
     
  21. Offline

    GermanCoding

    michael566
    Now, your error code belongs to your plugin "kick". Your test-plugin email where you test my lib gives no error.
     
  22. Offline

    shohouku

    I'm getting an error also:

    Code:
    [22:05:09 ERROR]: Could not pass event AsyncPlayerChatEvent to PlayerMenu v0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:483) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.chat(PlayerConnection.j
    ava:881) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :831) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:84)
    [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.channelRead0(NetworkManag
    er.java:204) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.SimpleChannelInboundHandler.chann
    elRead(SimpleChannelInboundHandler.java:98) [craftbukkit.jar:git-Bukkit-1.7.9-R0
    .2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invo
    keChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit
    -1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fire
    ChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1
    .7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.handler.codec.ByteToMessageDecoder.channe
    lRead(ByteToMessageDecoder.java:173) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g
    3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invo
    keChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit
    -1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fire
    ChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1
    .7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.handler.codec.ByteToMessageDecoder.channe
    lRead(ByteToMessageDecoder.java:173) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g
    3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invo
    keChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit
    -1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fire
    ChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1
    .7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.handler.codec.MessageToMessageDecoder.cha
    nnelRead(MessageToMessageDecoder.java:103) [craftbukkit.jar:git-Bukkit-1.7.9-R0.
    2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invo
    keChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit
    -1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fire
    ChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1
    .7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.handler.timeout.ReadTimeoutHandler.channe
    lRead(ReadTimeoutHandler.java:149) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3f
    d9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invo
    keChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit
    -1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fire
    ChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1
    .7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelPipeline.fireChanne
    lRead(DefaultChannelPipeline.java:785) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11
    -g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.nio.AbstractNioByteChannel$NioByt
    eUnsafe.read(AbstractNioByteChannel.java:100) [craftbukkit.jar:git-Bukkit-1.7.9-
    R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.nio.NioEventLoop.processSelectedK
    ey(NioEventLoop.java:480) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b30
    98jnks]
            at net.minecraft.util.io.netty.channel.nio.NioEventLoop.processSelectedK
    eysOptimized(NioEventLoop.java:447) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3
    fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.channel.nio.NioEventLoop.run(NioEventLoop
    .java:341) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.util.io.netty.util.concurrent.SingleThreadEventExecutor
    $2.run(SingleThreadEventExecutor.java:101) [craftbukkit.jar:git-Bukkit-1.7.9-R0.
    2-11-g3fd9db2-b3098jnks]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_51]
    Caused by: plugin.SMTP$SMTPConnectException: java.net.ConnectException: Connecti
    on timed out: connect
            at plugin.SMTP.connect(SMTP.java:554) ~[?:?]
            at plugin.SMTP.connect(SMTP.java:546) ~[?:?]
            at plugin.SMTP.<init>(SMTP.java:526) ~[?:?]
            at plugin.SMTP.sendEmail(SMTP.java:947) ~[?:?]
            at plugin.menu.onPlayerChat(menu.java:3021) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            ... 31 more
    Caused by: java.net.ConnectException: Connection timed out: connect
            at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[?:1.7.0_5
    1]
            at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[?:1
    .7.0_51]
            at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[?:1.7.0_
    51]
            at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[?
    :1.7.0_51]
            at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[?:1.7.0_51
    ]
            at java.net.PlainSocketImpl.connect(Unknown Source) ~[?:1.7.0_51]
            at java.net.SocksSocketImpl.connect(Unknown Source) ~[?:1.7.0_51]
            at java.net.Socket.connect(Unknown Source) ~[?:1.7.0_51]
            at java.net.Socket.connect(Unknown Source) ~[?:1.7.0_51]
            at java.net.Socket.<init>(Unknown Source) ~[?:1.7.0_51]
            at java.net.Socket.<init>(Unknown Source) ~[?:1.7.0_51]
            at plugin.SMTP.connect(SMTP.java:551) ~[?:?]
            at plugin.SMTP.connect(SMTP.java:546) ~[?:?]
            at plugin.SMTP.<init>(SMTP.java:526) ~[?:?]
            at plugin.SMTP.sendEmail(SMTP.java:947) ~[?:?]
            at plugin.menu.onPlayerChat(menu.java:3021) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            ... 31 more
    My code:

    Code:java
    1. if(reporting.contains(p.getName())) {
    2. System.out.println(1);
    3. if(event.getMessage().contains(event.getMessage())) {
    4. System.out.println(2);
    5. event.setCancelled(true);
    6.  
    7. final Email email = SMTP.createEmptyEmail();
    8. email.add("Content-Type", "text/html"); //Headers for the email (useful for html) you do not have to set them)
    9. email.from("You", "hidden"); //The sender of the email.
    10. email.to("Me", "hidden"); //The recipient of the email.
    11. email.subject("hidden"); //Subject of the email
    12. email.body(event.getMessage());
    13. SMTP.sendEmail(
    14. "smtp.gmail.com", //Address of the smtp server. In many cases you can set it to null.
    15. "hidden", //Your email (Used for login at the smtp server)
    16. "hidden", //Your password (Used for login at the smtp server, will be send encrypted to the server)
    17. email, //Email object to send.
    18. true); //Debug mode. If true the console logs the whole connection (output & input)
    19. reporting.set(p.getName(), null);
    20.  
    21.  
    22.  
    23.  
    24. }
    25. }
     
  23. Offline

    michael566

    GermanCoding Did you look all the way at the bottom of the error?

    EDIT: Nvm, it works removed that other plugin, lol
     
  24. Offline

    GermanCoding

    shohouku
    The lib can not connect to smtp.gmail.com. Maybe the smtp service of gmail is down, or your (test)-server blocks the connection?
     
  25. Offline

    shohouku


    Ah fixed it.

    I used port 587 for Smtp.live.com and I unblocked it from my firewall and now it works :)
     
  26. Offline

    CoderRyan

  27. Offline

    GermanCoding

    CoderRyan
    You did not set the smtp server address properly (You copied the example value "smtp.server.address"). Set it to null and try whether this works.

    Edit: See topic "I do not know what the 'SMTP Server Address' is?" for more help.
     
  28. Offline

    CoderRyan

  29. Offline

    GermanCoding

    CoderRyan

    Are you sure that it is the same error? which error do you get when you set it to null?
     
  30. Offline

    CoderRyan

    i changed the port from 25 to 587 and now it works. any reason why??
     
Thread Status:
Not open for further replies.

Share This Page