Guest User

Untitled

a guest
Apr 10th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. import java.util.Date;
  2. import java.util.Properties;
  3.  
  4. import javax.activation.DataHandler;
  5. import javax.activation.DataSource;
  6. import javax.activation.FileDataSource;
  7. import javax.mail.Authenticator;
  8. import javax.mail.BodyPart;
  9. import javax.mail.Message;
  10. import javax.mail.Multipart;
  11. import javax.mail.PasswordAuthentication;
  12. import javax.mail.Session;
  13. import javax.mail.Transport;
  14. import javax.mail.internet.InternetAddress;
  15. import javax.mail.internet.MimeBodyPart;
  16. import javax.mail.internet.MimeMessage;
  17. import javax.mail.internet.MimeMultipart;
  18.  
  19. /** *Klasse zum versenden einer eMail mit/ohne Attachment * @author Chris */
  20. public class SendMail {
  21.     private String server;
  22.     private String user;
  23.     private String password;
  24.     private String port;
  25.  
  26.     /** * Konstruktor */
  27.     public SendMail() {
  28.         port = "25";
  29.     }
  30.  
  31.     /** * * @return Port des Mailservers (Standard 25) */
  32.     public String getPort() {
  33.         return port;
  34.     }
  35.  
  36.     /** * * @param port Port des Mailservers (Standard 25) */
  37.     public void setPort(String port) {
  38.         this.port = port;
  39.     }
  40.  
  41.     /** * * @return Passwort für den Mailserver */
  42.     public String getPassword() {
  43.         return password;
  44.     }
  45.  
  46.     /** * * @param password Passwort für den Mailserver */
  47.     public void setPassword(String password) {
  48.         this.password = password;
  49.     }
  50.  
  51.     /** * * @return Username für den Mailserver */
  52.     public String getUser() {
  53.         return user;
  54.     }
  55.  
  56.     /** * * @param user Username für den Mailserver */
  57.     public void setUser(String user) {
  58.         this.user = user;
  59.     }
  60.  
  61.     /** * * @return Mailserver */
  62.     public String getServer() {
  63.         return server;
  64.     }
  65.  
  66.     /** * * @param server Mailserver */
  67.     public void setServer(String server) {
  68.         this.server = server;
  69.     }
  70.  
  71.     /**
  72.      * * * @param sVon eMailadresse des Absenders * @param sAn eMailadresse des
  73.      * Empfängers * @param sSubject Subjekt (Betreffzeile) * @param sBody Text
  74.      * der Mail * @return
  75.      */
  76.     public boolean sendeMail(String sVon, String sAn, String sSubject,
  77.             String sBody) {
  78.         return sendeMail(sVon, sAn, sSubject, sBody, "");
  79.     }
  80.  
  81.     /**
  82.      * * * @param sVon eMailadresse des Absenders * @param sAn eMailadresse des
  83.      * Empfängers * @param sSubject Subjekt (Betreffzeile) * @param sBody Text
  84.      * der Mail * @param sFilename Attachment (Pfad zur Datei) * @return
  85.      */
  86.     public boolean sendeMail(String sVon, String sAn, String sSubject,
  87.             String sBody, String sFilename) {
  88.         try {
  89.             String file = "";
  90.             Properties prop = System.getProperties();
  91.             if (server == null || server.isEmpty()) {
  92.                 return false;
  93.             }
  94.             prop.put("mail.transport.protocol", "smtp");
  95.             prop.put("mail.smtp.auth", "true");
  96.             prop.put("mail.smtp.host", server);
  97.             prop.put("mail.smtp.port", port);
  98.             prop.put("mail.user", user);
  99.             prop.put("mail.password", password);
  100.             Authenticator auth = null;
  101.             auth = new Authenticator() {
  102.                 @Override
  103.                 public PasswordAuthentication getPasswordAuthentication() {
  104.                     return new PasswordAuthentication(user, password);
  105.                 }
  106.             };
  107.             javax.mail.Session ses1 = Session.getInstance(prop, auth);
  108.             MimeMessage msg = new MimeMessage(ses1);
  109.             msg.setFrom(new InternetAddress(sVon));
  110.             msg.addRecipient(Message.RecipientType.TO, new InternetAddress(sAn));
  111.             msg.setSubject(sSubject);
  112.             Multipart multipart = new MimeMultipart();
  113.             BodyPart messageBodyPart = new MimeBodyPart();
  114.             messageBodyPart.setText(sBody);
  115.             multipart.addBodyPart(messageBodyPart);
  116.             file = sFilename.substring(sFilename.lastIndexOf("/") + 1);
  117.             if (file.equalsIgnoreCase("")) {
  118.                 file = sFilename.substring(sFilename.lastIndexOf("\\") + 1);
  119.             }
  120.             if (!file.equalsIgnoreCase("")) {
  121.                 messageBodyPart = new MimeBodyPart();
  122.                 DataSource source = new FileDataSource(sFilename);
  123.                 messageBodyPart.setDataHandler(new DataHandler(source));
  124.                 messageBodyPart.setFileName(file);
  125.                 multipart.addBodyPart(messageBodyPart);
  126.             }
  127.             msg.setContent(multipart);
  128.             msg.setSentDate(new Date());
  129.             Transport.send(msg);
  130.         } catch (Exception e) {
  131.             System.out.print(e.fillInStackTrace());
  132.             return false;
  133.         }
  134.         return true;
  135.     }
  136.  
  137. }
Add Comment
Please, Sign In to add comment