Java - Allgemein

E-Mail versenden

Um eine einfache Mail zu verwenden braucht es nicht viel Code:


import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
 
public class SendMail {
   public void sendMail(String smtpHost, int smtpPort,
                        String from, String to,
                        String subject, String content)
                throws AddressException, MessagingException {
      java.util.Properties props = new java.util.Properties();
      props.put("mail.smtp.host", smtpHost);
      props.put("mail.smtp.port", ""+smtpPort);
      Session session = Session.getDefaultInstance(props, null);
 
      Message msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress(from));
      msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
      msg.setSubject(subject);
      msg.setText(content);
 
      Transport.send(msg);
   }
 
   public static void main(String[] args) throws Exception {
      SendMail sm = new SendMail();
      sm.sendMail("hostname", 25, "test@xyz.com", "test2@xyz.com",
                  "Re: Treffen", "Gerne, heute gegen 20:30 Uhr?");
   }
}

Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007