[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

    GermanCoding

    This thread is an update of @Icyene's Emailer and is not limited to Yahoo & Gmail. It can send emails to all providers that supports SMTP with (START)TLS. The original code for the very cool and safe smtp connection was made by Xiaomao Chen @ IvyBits and not by me. I modified his code and made some extensions.

    The code is available here.

    Simple usage:
    Code:java
    1.  
    2. SMTP.Email email = SMTP.createEmptyEmail();
    3. email.add("Content-Type", "text/html"); //Headers for the email (useful for html) you do not have to set them)
    4. email.from("You", "YourEmail@address"); //The sender of the email.
    5. email.to("Me", "MyEmail@address"); //The recipient of the email.
    6. email.subject("A subject"); //Subject of the email
    7. email.body("Content of the email. Encoded in quoted-printable. You can use control \n characters or html if you set the header");
    8.  
    9. SMTP.sendEmail(
    10. "smtp.server.address", //Address of the smtp server. In many cases you can set it to null.
    11. "YourEmail@address", //Your email (Used for login at the smtp server)
    12. "****", //Your password (Used for login at the smtp server, will be send encrypted to the server)
    13. email, //Email object to send.
    14. false); //Debug mode. If true the console logs the whole connection (output & input)



    The Email class:
    With the email object you can modify your email. You can add your own headers (for example HTML emails) to your email or you can set CC and BCC. You can even add files to your email (file attachments, see below)!

    How to add file attachments:

    Code:java
    1. SMTP.Email email = SMTP.createEmptyEmail();
    2. //email.add("Content-Type", "text/html"); //Optional
    3. email.from("You", "you@address");
    4. email.to("Me", "me@address");
    5. email.subject("Subject");
    6. email.body("Content");
    7. try {
    8. SMTP.addFileAttachment(email, new File("File"));
    9. } catch (IOException e) { //Can not read file
    10. e.printStackTrace();
    11. }
    12. SMTP.sendEmail(...);


    Note: Never change the email object after calling addFileAttachment

    Changing the email type (HTML email):
    You want to use HTML in your email? No problem, just call the function add("Content-Type", "text/html") in your email object.
    Example:
    Code:java
    1. SMTP.Email email = SMTP.createEmptyEmail();
    2. email.add("Content-Type", "text/html");
    3. email.from(...);
    4. email.to(...);
    5. email.subject("...");
    6. email.body("...");
    7. SMTP.sendEmail("smtp.address", "email@address", "****", email, debug);

    Note: It is set to text/plain by default.





    I do not know what the 'SMTP Server Address' is?
    It is the address to connect the socket. For example, when I want to use an gmail account, I have to set the address to "smtp.gmail.com:465". (Please note that gmail accounts are only available when you use port 465 or 587, other providers are available at the standart port 25)
    In most cases it looks like this:
    Code:java
    1. public static String PROVIDER_GMAIL = "smtp.gmail.com:465";
    2. public static String PROVIDER_GMX_DE = "smtp.gmx.de";
    3. public static String PROVIDER_GMX_COM = "smtp.gmx.com";
    4. public static String PROVIDER_YAHOO_DE = "smtp.yahoo.de";
    5. public static String PROVIDER_YAHOO_COM = "smtp.yahoo.com";

    And so on...
    Note: Some more providers are listed here (Since version 1.4 you can find this list also in the source code)

    Important note:
    I highly recommend to call the SMTP.sendEmail() function NOT on the main-thread or bukkit-thread, because sending an email costs up to 10 seconds of time (with attachments up to a few minutes (5 mb file = 1 minute at my computer)).

    Example debug log - How it works
    Code:
     // > = Out, < = In
    >EHLO smtp.gmx.de // Greet the server
    <250-gmx.com Hello smtp.gmx.de [87.160.119.25]
    <250-SIZE 69920427 //max email size
    <250-AUTH LOGIN PLAIN 
    <250 STARTTLS // Server requires/supports TLS
    >STARTTLS // We also want TLS :)
    <220 OK
    >EHLO smtp.gmx.de // Reauthenticate - Session reset. From this point all data is TLS/SSL encrypted.
    <250-gmx.com Hello smtp.gmx.de [87.160.119.25]
    <250-SIZE 69920427
    <250 AUTH LOGIN PLAIN
    >AUTH PLAIN Some nasty key
    <235 Authentication succeeded
    >mail FROM:<[email protected]>
    <250 Requested mail action okay, completed
    >rcpt TO:<[email protected]>
    <250 OK
    >data
    <354 Start mail input; end with <CRLF>.<CRLF>
    >X-Mailer: Java/SMTP
    MIME-version: 1.0
    Content-Transfer-Encoding: quoted-printable
    From: "Me" <[email protected]>
    To: "You" <[email protected]>
    Subject: Email Test
    Date: Sat, 17 January 2015 13:24:17 +0100
    Content-Type: text/plain
    
    Begin=0aTest Message=0aEnd.
    .
    <250 Requested mail action okay, completed: id=mail-id

    If you have any problems...

    Or suggestions, just ask me!

    Have fun with this resource!
     
    Last edited: Jan 21, 2015
  2. Offline

    Bammerbom

  3. Offline

    bigteddy98

    does Bukkit allow us to use this in our public plugins? I can understand it if such things aren't allowed.
     
    GermanCoding likes this.
  4. Offline

    GermanCoding

    bigteddy98 Why should this be forbidden? Of course, spamming mails to people is not an good idea, but to notify server owners? I do not have a public plugin with email-function, but I think it would be allowed.
     
  5. Offline

    masterofsixpack

    Do you have an example with Gmail?
     
  6. Offline

    GermanCoding

    masterofsixpack
    Code:java
    1. SMTP.sendEmail("smtp.gmail.com", //Address of gmail
    2. "GermanCoding(at)gmail.com", //Your email
    3. "****", //Your password (Will be send encrypted to the server)"
    4. "GermanCoding", //Your name
    5. "masterofsixpack", //Name of the recipient
    6. "masterofsixpack(at)whatever.com", //Email of the recipient
    7. "Your example", //Subject of the email
    8. "Do you understand \n it?", //Content of the email. You can use control characters like \n for a new line
    9. false); //Debug mode. If true the console logs the whole connection (output & input)
     
  7. Offline

    mazentheamazin

    GermanCoding
    I couldn't stand the indentation and syntax, it physically hurt me. Here is the gist where I fixed it.
     
    GermanCoding and iiHeroo like this.
  8. Offline

    GermanCoding

  9. Offline

    iiHeroo


    Pretty sure BuyCraft does this, but it'd be considered a backdoor (possibly) so you might have to leave a note saying on your plugin page saying such thing exists within the plugin.
     
  10. Offline

    BungeeTheCookie

    GermanCoding
    Here are a few suggestions that will help improve this utility and make it more practical to use.
    I. Instead of typing "smtp." in front of the first parameter, why don't you just have it built in? So all you need to do is type in the website's email address.
    II. Instead of saying (at), you just use the .replace() method, allowing people to say "[email protected]" instead of having to say "bobjones(at)gmail.com." You can just use .replace("@", "(at)").

    Otherwise, this thing is awesome!!!
     
    ChipDev and GermanCoding like this.
  11. Offline

    mazentheamazin

    Some smtp servers are labelled differently, so having the ability to configure the sub-domain is useful
     
    GermanCoding likes this.
  12. Offline

    GermanCoding

    A little note to II:
    You have to use [email protected] in the parameter, I wrote the (at) only because I thought that bukkit automatically adds a "mailto:" link and I wanted to avoid that.

    Update:
    I added a second mail() function where you do not have to give the address of the smtp-server. The function connects automatically to your email address, but it works only if server and email have the same domain.

    Thanks for the feedback!
     
  13. Offline

    BungeeTheCookie

    Oh. Well, thank you so much!!!
     
    GermanCoding likes this.
  14. Offline

    Plo124

    Code:java
    1. // Testing:
    2. [EMAIL]123@gmail.com[/EMAIL]

    Yep, it does add [email ] around it
     
    GermanCoding likes this.
  15. Offline

    GermanCoding

  16. Offline

    Wizehh

    Does this offer any advantage over the JavaMail API?
     
  17. Offline

    GermanCoding

    Wizehh
    This is only one class and not a big framework like JavaMail. And I think that JavaMail isn't included in all JRE's, isn't it? So with this class you don't have any dependencies. In my opinion this class offers enough features for bukkit/minecraft plugins.
     
  18. Offline

    Zupsub

    So you have to give your personal private eMail password away with the plugin using this function? I'd never do for public ones.
     
  19. Offline

    GermanCoding

    Zupsub
    You don't have to give it "away". You can handle it like a mysql database: Ask the server admindistrator to write an email address into the config. Or you can create a new email account for this, the password is never stored in the class and it is send encrypted to the smtp server.
     
  20. Offline

    MarinusLeeuweri

    GermanCoding
    It works like a charm, but I wonder if it'd be possible to send emails in a HTML format too. Do you have any clues on how to do this? :) . Anyways, great work!
     
    GermanCoding likes this.
  21. Offline

    GermanCoding

    MarinusLeeuweri
    If you want to send an html email, just write html tags + text in the content parameter. But the receiver email provider/software decides whether the html is shown or not, this is out of our control.
     
  22. Offline

    Plo124

    I would imagine if you are using those Multicraft Panel hosted servers they wouldn't have JavaMail installed, in which this would work but JavaMail would throw NoClassDefFound exceptions and not work.
     
  23. Offline

    MarinusLeeuweri

    GermanCoding
    I think I have followed your tips, but it sadly doesn't work.

    This is my code:
    Code:java
    1. String content = "<strong>HI!</strong>";
    2. SMTP.sendEmail(smpthost, sender, password, "Marinus Leeuwerik", reciever, recieveremail, "Java SMTP mail test!", content, true);


    But when I sent the email, and recieve it in my inbox I get this:
    [​IMG]

    I am most likely doing something wrong I suppose, but well, nobody is perfect.

    Thanks =)
     
  24. Offline

    GermanCoding

    MarinusLeeuweri
    You are right, I forget something. You have to modify one header in the email and say that you send an html-email. To do that, just add the following line into the sendEmail() function:

    Code:java
    1. mail.add("Content-Type", "text/html");


    Add this before the smtp.sendMail() function and your html will be shown. If you do not set this header, Content-Type is set to text/plain by default and html is not translated.

    Thank you for the hint!
     
  25. Offline

    MarinusLeeuweri

    Ah, thank you very much, I already found something like this, but couldn't find it in the library, it works like a charm!

    Have a nice evening =)
     
    GermanCoding likes this.
  26. correct JavaMail is not part of ths Standard Edition of Java, but is part of Enterprise Edition by default.
     
  27. Offline

    jusjus112

  28. Offline

    GermanCoding

    jusjus112
    Seems like google rejects the request. Please check username & password and the address of the smtp server.
    If you need more help, please set the 'debug' parameter to true and send me the output (You can remove your personal data like your email-address (and the encrypted password) from the log before you give it to me)

    jusjus112
    I do not have an gmail account, but I read a minute ago that you have to enable smtp in your gmail account?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  29. Offline

    jusjus112

    GermanCoding
    Do you know how to enable that? or what i must have for port e.t.c.
    This is how i send it:
    Code:java
    1. SMTP.sendEmail("smtp.gmail.com", //Address of gmail
    2. "[email][email protected][/email]", //Your email
    3. "****", //Your password (Will be send encrypted to the server)"
    4. "SchockByte Register Service", //Your name
    5. "Player jusjus112", //Name of the recipient
    6. "[email][email protected][/email]", //Email of the recipient
    7. "Your example", //Subject of the email
    8. "Do you understand \n it?", //Content of the email. You can use control characters like \n for a new line
    9. false);
    10. sender.sendMessage(ChatColor.GOLD + "Email send!");

    he says: "
    Code:
    07.06 13:37:30 [Server] INFO 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 m44sm29803890eeh.14 - gsmtp
    07.06 13:37:30 [Server] INFO 5.7.14 Learn more at
    07.06 13:37:30 [Server] INFO 5.7.14 LRzvuQA> Please log in via your web browser and then try again.
     
  30. Offline

    GermanCoding

    jusjus112
    There should be more output, like a hello message. Is there really no more output?

    Edit: You could try to change the smtp address to smtp.gmail.com:465

    And maybe you should log into your account via browser, maybe you have to enter a captcha, because your ip could be blocked by gmail (too many incorrect logins?)

    I created a gmail account and the lib works fine. I used this code:

    Code:java
    1. SMTP.sendEmail("[email][email protected][/email]", "BukkitTest", "Sender", "Recipient", "*My-Private-Mail*", "A Test", "A message", true);


    PS: I already deleted the google account, so do not try to use this username & password ;)

    As a result I can say that your problem is not a problem with the lib. I recommended that you do the last step in the previous post (Log into your account via browser).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page