java mail modification de l’adresse de l’expéditeur
bonjour,
je dois envoyer un mail avec mon serveur mail Maven .
mais le souci avec le code suivant c'est qu'il ne me modifie pas l'email de l’expéditeur il garde l'adresse email de Gmail.
Code:
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
|
public void sendEmail(String expediteur,String destinataire, String messages) {
// Recipient's email ID needs to be mentioned.
String to = destinataire;//change accordingly
// Sender's email ID needs to be mentioned
String from = expediteur+"<"+expediteur+">";//change accordingly
final String username = "IDgmail";//change accordingly
final String password = "******";//change accordingly
// Assuming you are sending email through relay.jangosmtp.net
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
props.setProperty("mail.from",expediteur);
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("webserice mail");
// Now set the actual message
message.setText(messages);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
} |