IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API standards et tierces Java Discussion :

[JavaMail] Problème d'authentification pr l'envoi de mail avec Yahoo


Sujet :

API standards et tierces Java

  1. #1
    Nouveau membre du Club
    Profil pro
    Développeur Java
    Inscrit en
    Janvier 2008
    Messages
    42
    Détails du profil
    Informations personnelles :
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Janvier 2008
    Messages : 42
    Points : 34
    Points
    34
    Par défaut [JavaMail] Problème d'authentification pr l'envoi de mail avec Yahoo
    Bonsoir,

    J'essaye depuis un bon moment d'envoyer un simple mail avec Yahoo avec un code trouvé, mais un prb d authentification apparait lors de l'envoi. Pourtant le password est correcte!!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
     
     
     
    /*
     * Created on 14/09/2005
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package biling.web.common;
     
    import java.util.Properties;
     
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
     
    public class Envoi {
     
    	private static final String SMTP_HOST_NAME = "smtp.mail.yahoo.fr";
     
     
    	private static final String SMTP_AUTH_USER = "from@yahoo.fr";
     
    	private static final String SMTP_AUTH_PWD = "mypass";
     
    	private static final String emailMsgTxt = "Please visit my project at ";
     
    	private static final String emailSubjectTxt = "Order Confirmation Subject";
     
    	private static final String emailFromAddress = "from@yahoo.fr";
     
    	// Add List of Email address to who email needs to be sent to
    	private static final String[] emailList = { "dest@yahoo.fr" };
     
    	public static void main(String args[]) throws Exception {
    		Envoi smtpMailSender = new Envoi();
    		smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt,
    				emailFromAddress);
    		System.out.println("Sucessfully Sent mail to All Users");
    	}
     
    	public void postMail(String recipients[], String subject, String message,
    			String from) throws MessagingException {
    		boolean debug = true;
    		java.security.Security
    				.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
     
    		//Set the host smtp address
    		Properties props = new Properties();
    		props.put("mail.transport.protocol", "smtp");
    		props.put("mail.smtp.starttls.enable","true");
    		props.put("mail.smtp.host", SMTP_HOST_NAME);
    		props.put("mail.smtp.auth", "true");
     
    		Authenticator auth = new SMTPAuthenticator();
    		Session session = Session.getDefaultInstance(props, auth);
     
    		session.setDebug(debug);
     
    		// create a message
    		Message msg = new MimeMessage(session);
     
    		// set the from and to address
    		InternetAddress addressFrom = new InternetAddress(from);
    		msg.setFrom(addressFrom);
     
    		InternetAddress[] addressTo = new InternetAddress[recipients.length];
    		for (int i = 0; i < recipients.length; i++) {
    			addressTo[i] = new InternetAddress(recipients[i]);
    		}
    		msg.setRecipients(Message.RecipientType.TO, addressTo);
     
    		// Setting the Subject and Content Type
    		msg.setSubject(subject);
    		msg.setContent(message, "text/plain");
    		Transport.send(msg);
    	}
     
    	/**
             * SimpleAuthenticator is used to do simple authentication when the SMTP
             * server requires it.
             */
    	private class SMTPAuthenticator extends javax.mail.Authenticator {
     
    		public PasswordAuthentication getPasswordAuthentication() {
    			String username = SMTP_AUTH_USER;
    			String password = SMTP_AUTH_PWD;
    			return new PasswordAuthentication(username, password);
    		}
    	}
    }
    L'erreur que j'ai est la suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
     
    535 authorization failed (#5.7.0)
    Exception in thread "main" javax.mail.SendFailedException: Sending failed;
      nested exception is:
    	class javax.mail.AuthenticationFailedException
    	at javax.mail.Transport.send0(Transport.java:218)
    	at javax.mail.Transport.send(Transport.java:80)
    	at biling.web.common.Envoi.postMail(Envoi.java:168)
    	at biling.web.common.Envoi.main(Envoi.java:129)
    Merci par avance

  2. #2
    Membre régulier
    Inscrit en
    Mars 2008
    Messages
    109
    Détails du profil
    Informations forums :
    Inscription : Mars 2008
    Messages : 109
    Points : 99
    Points
    99
    Par défaut

    si t'a pu résoudre ce probleme est ce que tu pourrai me l'envoyer par mail ou le poster sur le forum.

    merci.

  3. #3
    Nouveau membre du Club
    Profil pro
    Développeur Java
    Inscrit en
    Janvier 2008
    Messages
    42
    Détails du profil
    Informations personnelles :
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Janvier 2008
    Messages : 42
    Points : 34
    Points
    34
    Par défaut
    ca marche avec le pop gmail, il faudera l essayer avec le pop yahoo

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
     
    package test.sendmail;
     
     
     
     import java.io.IOException;
     import java.io.StringReader;
     import java.io.UnsupportedEncodingException;
     import java.security.Security;
     import java.util.Properties;
     
     import javax.activation.DataHandler;
     import javax.activation.DataSource;
     import javax.activation.FileDataSource;
     import javax.mail.Authenticator;
     import javax.mail.BodyPart;
     import javax.mail.Message;
     import javax.mail.MessagingException;
     import javax.mail.PasswordAuthentication;
     import javax.mail.Session;
     import javax.mail.Transport;
     import javax.mail.internet.InternetAddress;
     import javax.mail.internet.MimeBodyPart;
     import javax.mail.internet.MimeMessage;
     import javax.mail.internet.MimeMultipart;
     import javax.swing.text.BadLocationException;
     import javax.swing.text.Document;
    import javax.swing.text.html.HTMLEditorKit;
     
     
     public class Envoi2 {
     
     final private static String CHARSET = "charset=ISO-8859-1";
     
     final private static String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
     
     final private static int DEFAULT_SMTP_PORT = 25;
     
     final private Session _session;
     
     
     // Exemples
     public static void main(String args[]) throws UnsupportedEncodingException, IOException, MessagingException {
     
     // Message simple : (from et to sont indispensables)
     final MailMessage msg = new MailMessage();
     
     // connexion à un autre serveur de mail
     // (l'activation du compte pop est nécessaire pour gmail)
     final Envoi2 mail2 = new Envoi2("smtp.gmail.com", 465, "addresseFrom", "motDePasse", true);
     
     // Message avec texte html + images incluses + pièces jointes
     msg.setFrom(new InternetAddress("addresseFrom@gmail.com", "Nom..."));
     msg.setTo("adresseTo@gmail.com");
     msg.setCc(
     new InternetAddress[] {
     new InternetAddress("AddresseCopie1@bbb.ccc", "NOM..."),
     new InternetAddress("AddresseCopie2@ddd.eee", "NOM...")
     }
     );
     msg.setSubject("sujet");
     String msgContent = "<p><h3>Salut</h3></p><br><br>Si vous recevez ce mail, " +
     					 "c'est que le mailing avec java fonctionne" +
     					 ", et que ce programme vous permettera d\'envoyer meme des fichiers joint" +
     					 "<br>Cordialment,";
     
     msg.setContent(msgContent, true);
     
     mail2.sendMessage(msg);
     System.out.print("Votre message a été bien transmis");
     }
     
     
     
     // Constructeur n°1: Connexion au serveur mail
     public Envoi2(final String host, final int port, final String userName,
     final String password, final boolean ssl) {
     final String strPort = String.valueOf(port);
     final Properties props = new Properties();
     props.put("mail.smtp.host", host);
     props.put("mail.smtp.port", strPort);
     if (ssl) {
     // Connection SSL
     Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
     props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
     props.put("mail.smtp.socketFactory.fallback", "false");
     props.put("mail.smtp.auth", "true");
     props.put("mail.smtp.socketFactory.port", strPort);
     }
     if (null == userName || null == password) {
     _session = Session.getDefaultInstance(props, null);
     _session.setDebug(true);
     } else {
     // Connexion avec authentification
     _session = Session.getDefaultInstance(props, new Authenticator(){
     protected PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication(userName, password);
     }
     });
     }
     }
     
     // Autres constructeurs
     public Envoi2(final String host, final String userName,
     final String password, final boolean ssl) {
    	 this(host, DEFAULT_SMTP_PORT, userName, password, ssl);
     }
     
     public Envoi2(final String host, final String userName,
     final String password) {
    	 this(host, DEFAULT_SMTP_PORT, userName, password, false);
     }
     public Envoi2(final String host, final int port) {
    	 this(host, port, null, null, false);
     }
     public Envoi2(final String host) {
    	 this(host, DEFAULT_SMTP_PORT, null, null, false);
     }
     
     // Convertit un texte au format html en texte brut
     private static final String HtmlToText(final String s) {
     final HTMLEditorKit kit = new HTMLEditorKit();
     final Document doc = kit.createDefaultDocument();
     try {
     kit.read(new StringReader(s), doc, 0);
     return doc.getText(0, doc.getLength()).trim();
     } catch (final IOException ioe) {
     return s;
     } catch (final BadLocationException ble) {
     return s;
     }
     }
     
     // Défini les fichiers à joindre
     private void setAttachmentPart(final String[] attachmentPaths,
     final MimeMultipart related, final MimeMultipart attachment,
     final String body, final boolean htmlText)
     throws MessagingException {
     for (int i = 0; i < attachmentPaths.length; ++i) {
     // Création du fichier à inclure
     final MimeBodyPart messageFilePart = new MimeBodyPart();
     final DataSource source = new FileDataSource(attachmentPaths[i]);
     final String fileName = source.getName();
     messageFilePart.setDataHandler(new DataHandler(source));
     messageFilePart.setFileName(fileName);
     // Image à inclure dans un texte au format HTML ou pièce jointe
     if (htmlText && null != body && body.matches(
     ".*<img[^>]*src=[\"|']?cid:([\"|']?" + fileName + "[\"|']?)[^>]*>.*")) {
     // " <-- pour éviter une coloration syntaxique désastreuse...
     messageFilePart.setDisposition("inline");
     messageFilePart.setHeader("Content-ID", '<' + fileName + '>');
     related.addBodyPart(messageFilePart);
     } else {
     messageFilePart.setDisposition("attachment");
     attachment.addBodyPart(messageFilePart);
     }
     }
     }
     
     // Texte alternatif = texte + texte html
     private void setHtmlText(final MimeMultipart related,
     final MimeMultipart alternative, final String body)
     throws MessagingException {
     // Plain text
     final BodyPart plainText = new MimeBodyPart();
     plainText.setContent(HtmlToText(body), "text/plain; " + CHARSET);
     alternative.addBodyPart(plainText);
     // Html text ou Html text + images incluses
     final BodyPart htmlText = new MimeBodyPart();
     htmlText.setContent(body, "text/html; " + CHARSET);
     if (0 != related.getCount()) {
     related.addBodyPart(htmlText, 0);
     final MimeBodyPart tmp = new MimeBodyPart();
     tmp.setContent(related);
     alternative.addBodyPart(tmp);
     } else {
     alternative.addBodyPart(htmlText);
     }
     }
     
     // Définition du corps de l'e-mail
     private void setContent(final Message message,
     final MimeMultipart alternative, final MimeMultipart attachment,
     final String body)
     throws MessagingException {
     if (0 != attachment.getCount()) {
     // Contenu mixte: Pièces jointes + texte
     if (0 != alternative.getCount() || null != body) {
     // Texte alternatif = texte + texte html
     final MimeBodyPart tmp = new MimeBodyPart();
     tmp.setContent(alternative);
     attachment.addBodyPart(tmp, 0);
     } else {
     // Juste du texte
     final BodyPart plainText = new MimeBodyPart();
     plainText.setContent(body, "text/plain; " + CHARSET);
     attachment.addBodyPart(plainText, 0);
     }
     message.setContent(attachment);
     } else {
     // Juste un message texte
     if (0 != alternative.getCount()) {
     // Texte alternatif = texte + texte html
     message.setContent(alternative);
     }else {
     // Texte
     message.setText(body);
     }
     }
     }
     
     // Prototype n°1: Envoi de message avec pièce jointe
     public void sendMessage(final MailMessage mailMsg)
     throws MessagingException {
     final Message message = new MimeMessage(_session);
     // Subect
     message.setSubject(mailMsg.getSubject());
     // Expéditeur
     message.setFrom(mailMsg.getFrom());
     // Destinataires
     message.setRecipients(Message.RecipientType.TO, mailMsg.getTo());
     message.setRecipients(Message.RecipientType.CC, mailMsg.getCc());
     message.setRecipients(Message.RecipientType.BCC, mailMsg.getBcc());
     // Contenu + pièces jointes + images
     final MimeMultipart related = new MimeMultipart("related");
     final MimeMultipart attachment = new MimeMultipart("mixed");
     final MimeMultipart alternative = new MimeMultipart("alternative");
     final String[] attachments = mailMsg.getAttachmentURL();
     final String body = (String) mailMsg.getContent();
     final boolean html = mailMsg.isHtml();
     if (null != attachments)
     setAttachmentPart(attachments, related, attachment, body, html);
     if (html && null != body)
     setHtmlText(related, alternative, body);
     setContent(message, alternative, attachment, body);
     // Date d'envoi
     message.setSentDate(mailMsg.getSendDate());
     // Envoi
     Transport.send(message);
     // Réinitialise le message
     mailMsg.reset();
     }
     
     }
    Excuse moi pr le retard

  4. #4
    Membre à l'essai
    Inscrit en
    Juin 2009
    Messages
    10
    Détails du profil
    Informations forums :
    Inscription : Juin 2009
    Messages : 10
    Points : 13
    Points
    13
    Par défaut
    [QUOTE=hikamovic;3446319]ca marche avec le pop gmail, il faudera l essayer avec le pop yahoo

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
     
    package test.sendmail;
     
     
     
     import java.io.IOException;
     import java.io.StringReader;
     import java.io.UnsupportedEncodingException;
     import java.security.Security;
     import java.util.Properties;
     
     import javax.activation.DataHandler;
     import javax.activation.DataSource;
     import javax.activation.FileDataSource;
     import javax.mail.Authenticator;
     import javax.mail.BodyPart;
     import javax.mail.Message;
     import javax.mail.MessagingException;
     import javax.mail.PasswordAuthentication;
     import javax.mail.Session;
     import javax.mail.Transport;
     import javax.mail.internet.InternetAddress;
     import javax.mail.internet.MimeBodyPart;
     import javax.mail.internet.MimeMessage;
     import javax.mail.internet.MimeMultipart;
     import javax.swing.text.BadLocationException;
     import javax.swing.text.Document;
    import javax.swing.text.html.HTMLEditorKit;
     
     
     public class Envoi2 {
     
     final private static String CHARSET = "charset=ISO-8859-1";
     
     final private static String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
     
     final private static int DEFAULT_SMTP_PORT = 25;
     
     final private Session _session;
     
     
     // Exemples
     public static void main(String args[]) throws UnsupportedEncodingException, IOException, MessagingException {
     
     // Message simple : (from et to sont indispensables)
     final MailMessage msg = new MailMessage();
     
     // connexion à un autre serveur de mail
     // (l'activation du compte pop est nécessaire pour gmail)
     final Envoi2 mail2 = new Envoi2("smtp.gmail.com", 465, "addresseFrom", "motDePasse", true);
     
     // Message avec texte html + images incluses + pièces jointes
     msg.setFrom(new InternetAddress("addresseFrom@gmail.com", "Nom..."));
     msg.setTo("adresseTo@gmail.com");
     msg.setCc(
     new InternetAddress[] {
     new InternetAddress("AddresseCopie1@bbb.ccc", "NOM..."),
     new InternetAddress("AddresseCopie2@ddd.eee", "NOM...")
     }
     );
     msg.setSubject("sujet");
     String msgContent = "<p><h3>Salut</h3></p><br><br>Si vous recevez ce mail, " +
     					 "c'est que le mailing avec java fonctionne" +
     					 ", et que ce programme vous permettera d\'envoyer meme des fichiers joint" +
     					 "<br>Cordialment,";
     
     msg.setContent(msgContent, true);
     
     mail2.sendMessage(msg);
     System.out.print("Votre message a été bien transmis");
     }
     
     
     
     // Constructeur n°1: Connexion au serveur mail
     public Envoi2(final String host, final int port, final String userName,
     final String password, final boolean ssl) {
     final String strPort = String.valueOf(port);
     final Properties props = new Properties();
     props.put("mail.smtp.host", host);
     props.put("mail.smtp.port", strPort);
     if (ssl) {
     // Connection SSL
     Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
     props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
     props.put("mail.smtp.socketFactory.fallback", "false");
     props.put("mail.smtp.auth", "true");
     props.put("mail.smtp.socketFactory.port", strPort);
     }
     if (null == userName || null == password) {
     _session = Session.getDefaultInstance(props, null);
     _session.setDebug(true);
     } else {
     // Connexion avec authentification
     _session = Session.getDefaultInstance(props, new Authenticator(){
     protected PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication(userName, password);
     }
     });
     }
     }
     
     // Autres constructeurs
     public Envoi2(final String host, final String userName,
     final String password, final boolean ssl) {
    	 this(host, DEFAULT_SMTP_PORT, userName, password, ssl);
     }
     
     public Envoi2(final String host, final String userName,
     final String password) {
    	 this(host, DEFAULT_SMTP_PORT, userName, password, false);
     }
     public Envoi2(final String host, final int port) {
    	 this(host, port, null, null, false);
     }
     public Envoi2(final String host) {
    	 this(host, DEFAULT_SMTP_PORT, null, null, false);
     }
     
     // Convertit un texte au format html en texte brut
     private static final String HtmlToText(final String s) {
     final HTMLEditorKit kit = new HTMLEditorKit();
     final Document doc = kit.createDefaultDocument();
     try {
     kit.read(new StringReader(s), doc, 0);
     return doc.getText(0, doc.getLength()).trim();
     } catch (final IOException ioe) {
     return s;
     } catch (final BadLocationException ble) {
     return s;
     }
     }
     
     // Défini les fichiers à joindre
     private void setAttachmentPart(final String[] attachmentPaths,
     final MimeMultipart related, final MimeMultipart attachment,
     final String body, final boolean htmlText)
     throws MessagingException {
     for (int i = 0; i < attachmentPaths.length; ++i) {
     // Création du fichier à inclure
     final MimeBodyPart messageFilePart = new MimeBodyPart();
     final DataSource source = new FileDataSource(attachmentPaths[i]);
     final String fileName = source.getName();
     messageFilePart.setDataHandler(new DataHandler(source));
     messageFilePart.setFileName(fileName);
     // Image à inclure dans un texte au format HTML ou pièce jointe
     if (htmlText && null != body && body.matches(
     ".*<img[^>]*src=[\"|']?cid:([\"|']?" + fileName + "[\"|']?)[^>]*>.*")) {
     // " <-- pour éviter une coloration syntaxique désastreuse...
     messageFilePart.setDisposition("inline");
     messageFilePart.setHeader("Content-ID", '<' + fileName + '>');
     related.addBodyPart(messageFilePart);
     } else {
     messageFilePart.setDisposition("attachment");
     attachment.addBodyPart(messageFilePart);
     }
     }
     }
     
     // Texte alternatif = texte + texte html
     private void setHtmlText(final MimeMultipart related,
     final MimeMultipart alternative, final String body)
     throws MessagingException {
     // Plain text
     final BodyPart plainText = new MimeBodyPart();
     plainText.setContent(HtmlToText(body), "text/plain; " + CHARSET);
     alternative.addBodyPart(plainText);
     // Html text ou Html text + images incluses
     final BodyPart htmlText = new MimeBodyPart();
     htmlText.setContent(body, "text/html; " + CHARSET);
     if (0 != related.getCount()) {
     related.addBodyPart(htmlText, 0);
     final MimeBodyPart tmp = new MimeBodyPart();
     tmp.setContent(related);
     alternative.addBodyPart(tmp);
     } else {
     alternative.addBodyPart(htmlText);
     }
     }
     
     // Définition du corps de l'e-mail
     private void setContent(final Message message,
     final MimeMultipart alternative, final MimeMultipart attachment,
     final String body)
     throws MessagingException {
     if (0 != attachment.getCount()) {
     // Contenu mixte: Pièces jointes + texte
     if (0 != alternative.getCount() || null != body) {
     // Texte alternatif = texte + texte html
     final MimeBodyPart tmp = new MimeBodyPart();
     tmp.setContent(alternative);
     attachment.addBodyPart(tmp, 0);
     } else {
     // Juste du texte
     final BodyPart plainText = new MimeBodyPart();
     plainText.setContent(body, "text/plain; " + CHARSET);
     attachment.addBodyPart(plainText, 0);
     }
     message.setContent(attachment);
     } else {
     // Juste un message texte
     if (0 != alternative.getCount()) {
     // Texte alternatif = texte + texte html
     message.setContent(alternative);
     }else {
     // Texte
     message.setText(body);
     }
     }
     }
     
     // Prototype n°1: Envoi de message avec pièce jointe
     public void sendMessage(final MailMessage mailMsg)
     throws MessagingException {
     final Message message = new MimeMessage(_session);
     // Subect
     message.setSubject(mailMsg.getSubject());
     // Expéditeur
     message.setFrom(mailMsg.getFrom());
     // Destinataires
     message.setRecipients(Message.RecipientType.TO, mailMsg.getTo());
     message.setRecipients(Message.RecipientType.CC, mailMsg.getCc());
     message.setRecipients(Message.RecipientType.BCC, mailMsg.getBcc());
     // Contenu + pièces jointes + images
     final MimeMultipart related = new MimeMultipart("related");
     final MimeMultipart attachment = new MimeMultipart("mixed");
     final MimeMultipart alternative = new MimeMultipart("alternative");
     final String[] attachments = mailMsg.getAttachmentURL();
     final String body = (String) mailMsg.getContent();
     final boolean html = mailMsg.isHtml();
     if (null != attachments)
     setAttachmentPart(attachments, related, attachment, body, html);
     if (html && null != body)
     setHtmlText(related, alternative, body);
     setContent(message, alternative, attachment, body);
     // Date d'envoi
     message.setSentDate(mailMsg.getSendDate());
     // Envoi
     Transport.send(message);
     // Réinitialise le message
     mailMsg.reset();
     }
     
     }



    bonjour je vous remercie pour le code mais quand je l'implemente il ya une erreurr

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    MailMessage cannot be resolved to a type
    MailMessage cannot be resolved to a type

    at test.sendmail.Envoi2.main(Envoi2.java:45)


    alors il ne peut pas instancier le MailMessage svvvppp est ce qe vous pouvez reecrire votre classee

Discussions similaires

  1. Problème envoi de mail avec l'API JavaMail
    Par starkia dans le forum Général Java
    Réponses: 1
    Dernier message: 16/09/2013, 23h09
  2. [JavaMail] problème d'authentification sous Unix
    Par Michalak dans le forum API standards et tierces
    Réponses: 3
    Dernier message: 10/12/2007, 08h44
  3. envoi de mail avec authentification
    Par jfb53 dans le forum C++Builder
    Réponses: 4
    Dernier message: 07/08/2006, 12h23
  4. problème avec l'envoi de mail avec cdonts
    Par toussa dans le forum ASP
    Réponses: 9
    Dernier message: 16/06/2006, 16h38
  5. [Mail] Envoi de mail avec une boucle posant problème
    Par dj-julio dans le forum Langage
    Réponses: 7
    Dernier message: 09/01/2006, 10h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo