E-Mail versenden mit Java

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Hallo,

    Ich Danke dir, nur ein Problem hab ich, ich bin noch nicht so Lange in der Java programmierung, und deshalb weiß ich nicht was mit dem Classpath gemeint ist, für kurze Erklärung wäre ich dankbar.

    EDIT//: Den Classpath hab ich, hab den Zip ordner entpackt, nur wo soll ich die beiden datein einfügen?

    Aleex
  • "phax" schrieb:

    Du meinst, du willst die beiden Mail JARs in deine JAR-Datei packen?
    Das ist nicht notwendig!
    Wenn du auf der Console compilierst musst du beim compilieren den CLASSPATH um den absoluten Pfad zu den JAR-Dateien ergänzen, und beim Ausführen sollten einfach alle JAR files im selben Verzeichnis liegen.


    Danke, ich werde es ausprobieren :D
  • Mal ne Frage.
    Was benutzt du zum Mail Versenden?
    Solltest du javamail verwenden behaupte ich mal, dass du zu faul bist zu lesen =)
    Nimms mir bitte nicht übel, bin "größtenteils harmlos"

    Das hier ist ein Demoprogramm, was beim javamail dabei ist.
    Hoffe es hilft

    Quellcode

    1. /*
    2. * @(#)sendfile.java 1.11 03/06/19
    3. *
    4. * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
    5. *
    6. * Redistribution and use in source and binary forms, with or without
    7. * modification, are permitted provided that the following conditions
    8. * are met:
    9. *
    10. * - Redistributions of source code must retain the above copyright
    11. * notice, this list of conditions and the following disclaimer.
    12. *
    13. * - Redistribution in binary form must reproduce the above copyright
    14. * notice, this list of conditions and the following disclaimer in the
    15. * documentation and/or other materials provided with the distribution.
    16. *
    17. * Neither the name of Sun Microsystems, Inc. or the names of contributors
    18. * may be used to endorse or promote products derived from this software
    19. * without specific prior written permission.
    20. *
    21. * This software is provided "AS IS," without a warranty of any kind. ALL
    22. * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
    23. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
    24. * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
    25. * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
    26. * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
    27. * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
    28. * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
    29. * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
    30. * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
    31. * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
    32. * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
    33. *
    34. * You acknowledge that Software is not designed, licensed or intended
    35. * for use in the design, construction, operation or maintenance of any
    36. * nuclear facility.
    37. */
    38. import java.util.*;
    39. import java.io.*;
    40. import javax.mail.*;
    41. import javax.mail.internet.*;
    42. import javax.activation.*;
    43. /**
    44. * sendfile will create a multipart message with the second
    45. * block of the message being the given file.<p>
    46. *
    47. * This demonstrates how to use the FileDataSource to send
    48. * a file via mail.<p>
    49. *
    50. * usage: <code>java sendfile <i>to from smtp file true|false</i></code>
    51. * where <i>to</i> and <i>from</i> are the destination and
    52. * origin email addresses, respectively, and <i>smtp</i>
    53. * is the hostname of the machine that has smtp server
    54. * running. <i>file</i> is the file to send. The next parameter
    55. * either turns on or turns off debugging during sending.
    56. *
    57. * @author Christopher Cotton
    58. */
    59. public class sendfile {
    60. public static void main(String[] args) {
    61. if (args.length != 5) {
    62. System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false");
    63. System.exit(1);
    64. }
    65. String to = args[0];
    66. String from = args[1];
    67. String host = args[2];
    68. String filename = args[3];
    69. boolean debug = Boolean.valueOf(args[4]).booleanValue();
    70. String msgText1 = "Sending a file.\n";
    71. String subject = "Sending a file";
    72. // create some properties and get the default Session
    73. Properties props = System.getProperties();
    74. props.put("mail.smtp.host", host);
    75. Session session = Session.getInstance(props, null);
    76. session.setDebug(debug);
    77. try {
    78. // create a message
    79. MimeMessage msg = new MimeMessage(session);
    80. msg.setFrom(new InternetAddress(from));
    81. InternetAddress[] address = {new InternetAddress(to)};
    82. msg.setRecipients(Message.RecipientType.TO, address);
    83. msg.setSubject(subject);
    84. // create and fill the first message part
    85. MimeBodyPart mbp1 = new MimeBodyPart();
    86. mbp1.setText(msgText1);
    87. // create the second message part
    88. MimeBodyPart mbp2 = new MimeBodyPart();
    89. // attach the file to the message
    90. FileDataSource fds = new FileDataSource(filename);
    91. mbp2.setDataHandler(new DataHandler(fds));
    92. mbp2.setFileName(fds.getName());
    93. // create the Multipart and add its parts to it
    94. Multipart mp = new MimeMultipart();
    95. mp.addBodyPart(mbp1);
    96. mp.addBodyPart(mbp2);
    97. // add the Multipart to the message
    98. msg.setContent(mp);
    99. // set the Date: header
    100. msg.setSentDate(new Date());
    101. // send the message
    102. Transport.send(msg);
    103. } catch (MessagingException mex) {
    104. mex.printStackTrace();
    105. Exception ex = null;
    106. if ((ex = mex.getNextException()) != null) {
    107. ex.printStackTrace();
    108. }
    109. }
    110. }
    111. }
    Alles anzeigen


    edit:
    Was u.U. einigen helfen könnte, die Mails mit javamail versenden wollen. Bitte nicht vergessen activation.jar (JAVABEANS(tm) ACTIVATION FRAMEWORK, jaf) mit in den Classpath einzubinden. Sonst gibts ne nichtssagende Fehlermeldung.
    Ubuntu Edgy * Kernel 2.6.17 * Gnome 2.16 * Beryl
    2 x Athlon MP 1900 * MSI K7D Master-L * 1024 MB ECC DDR333
    Hercules 9800XT 256 MB Ram * 1x 250 GB IDE
    Wasserkühlung