Possibile duplicato:
Come si invia email da un'app Java utilizzando Gmail?
Come posso inviare un messaggio SMTP da Java?
Ecco un esempio per smtp di Gmail:
import Java.io.*;
import Java.net.InetAddress;
import Java.util.Properties;
import Java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import com.Sun.mail.smtp.*;
public class Distribution {
public static void main(String args[]) throws Exception {
Properties props = System.getProperties();
props.put("mail.smtps.Host","smtp.gmail.com");
props.put("mail.smtps.auth","true");
Session session = Session.getInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("[email protected]"));;
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]", false));
msg.setSubject("Heisann "+System.currentTimeMillis());
msg.setText("Med vennlig hilsennTov Are Jacobsen");
msg.setHeader("X-Mailer", "Tov Are's program");
msg.setSentDate(new Date());
SMTPTransport t =
(SMTPTransport)session.getTransport("smtps");
t.connect("smtp.gmail.com", "[email protected]", "<insert password here>");
t.sendMessage(msg, msg.getAllRecipients());
System.out.println("Response: " + t.getLastServerResponse());
t.close();
}
}
Ora, fallo in questo modo solo se desideri mantenere al minimo le dipendenze del progetto, altrimenti ti consiglio caldamente di usare le classi di Apache
http://commons.Apache.org/email/
Saluti
Tov Are Jacobsen
Un altro modo è usare l'aspirina ( https://github.com/masukomi/aspirin ) in questo modo:
MailQue.queMail(MimeMessage message)
..dopo aver costruito il tuo messaggio simile come sopra.
Aspirin è un 'server' smtp in modo da non doverlo configurare. Tuttavia, tieni presente che l'invio di e-mail a un ampio gruppo di destinatari non è così semplice come sembra a causa delle numerose regole di filtro anti-spam che ricevono i server di posta e si applicano le applicazioni client.
Si prega di vedere questo post
Come posso inviare un'e-mail tramite l'applicazione Java utilizzando GMail, Yahoo o Hotmail?
È specifico di gmail ma puoi sostituire le tue credenziali SMTP.
Vedi l'API JavaMail e i javadoc associati.
Vedi il seguente tutorial su Java Practices.
import javax.mail.*;
import javax.mail.internet.*;
import Java.util.*;
public void postMail(String recipients[], String subject,
String message , String from) throws MessagingException {
//Set the Host smtp address
Properties props = new Properties();
props.put("mail.smtp.Host", "smtp.jcom.net");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
// 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);
// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}