| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 
 | import javax.mail.internet.*;
import javax.mail.*;
import java.util.*;
import java.io.*;
import javax.mail.internet.InternetAddress;
 
public class SimpleSender
{
 public static void main(String args[])
  {
    try
    {
      String smtpServer = "smtp.mail.yahoo.fr";
      String to = "lilie@yahoo.fr";
      String from = "francois@yahoo.fr";
      String subject = "msg envoyé par javamail";
      String body = "slt, c le 1er essai avec javamail. j'espère que ça marchera inchallah! salam";
      send(smtpServer, to, from, subject, body);
    }
    catch (Exception ex)
    {
      System.out.println("Usage: java com.lotontech.mail.SimpleSender"
       +" smtpServer toAddress fromAddress subjectText bodyText");
    }
    System.exit(0);
  }
 
  // "send" method to send the message
  public static void send(String smtpServer, String to, String from, String subject, String body)
  {
    try
    {
      Properties props = System.getProperties();
      // -- Attaching to default Session, or we could start a new one --
      props.put("mail.smtp.host", smtpServer);
      Session session = Session.getDefaultInstance(props, null);
      // -- Create a new message --
      Message msg = new MimeMessage(session);
      // -- Set the FROM and TO fields --
      msg.setFrom(new InternetAddress(from));
      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
      // -- We could include CC recipients too --
      // if (cc != null)
      // msg.setRecipients(Message.RecipientType.CC
      // ,InternetAddress.parse(cc, false));
      // -- Set the subject and body text --
      msg.setSubject(subject);
      msg.setText(body);
      // -- Set some other header information --
      msg.setHeader("X-Mailer", "LOTONtechEmail");
      msg.setSentDate(new Date());
      // -- Send the message --
      Transport.send(msg);
      System.out.println("Message sent OK.");
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
      System.out.println("Erreur: Exception in 'send' method !");
      System.out.println(ex.getMessage());
    }
  }
} | 
Partager