Domanda

Possibile duplicato:
Come si inviano e-mail da un'app Java utilizzando Gmail?

Come posso inviare un messaggio SMTP da Java?

È stato utile?

Soluzione

Ecco un esempio per Gmail SMTP:

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("mail@tovare.com"));;
        msg.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse("tov.are.jacobsen@iss.no", 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", "admin@tovare.com", "<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 le dipendenze del tuo progetto al minimo, altrimenti posso consigliare vivamente di utilizzare le classi di Apache

http://commons.apache.org/email/

Saluti

Tov Are Jacobsen

Altri suggerimenti

Un altro modo è usare l'aspirina (https://github.com/masukomi/aspirin) come questo:

MailQue.queMail(MimeMessage message)

..dopo aver costruito il tuo messaggio mime come sopra.

Aspirina È un "server" SMTP in modo da non doverlo configurare.Tieni presente, tuttavia, che l'invio di e-mail a un'ampia gamma di destinatari non è così semplice come sembra a causa delle numerose regole di filtraggio dello spam applicate ai server di posta e alle applicazioni client di ricezione.

Si prega di consultare questo post

Come posso inviare un'e-mail tramite l'applicazione Java utilizzando GMail, Yahoo o Hotmail?

È specifico per Gmail ma puoi sostituire le tue credenziali SMTP.

Vedi il API JavaMail e Javadoc associati.

Vedere il seguente tutorial in Java Practices.

http://www.javapractices.com/topic/TopicAction.do?Id=144

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);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top