PDA

View Full Version : [Java] - JavaMail


JCoder87
09-11-2008, 10:38
Ciao a tutti... spero che qualcuno conosca la Java Mail, perchè è da un paio di ore che mi stò inalberando:

allora:
Ho del codice che deve inviare un e-mail, ma una volta lanciato mi va in eccezione perchè la porta 25 è chiusa e non si riesce a connettere col server SMTP da me specificato...

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class MailExample {
public static void main (String args[]) throws Exception {
String host = "smtp.email.msn.com";
String from = "xxx@xx.it";
String to = "mio_indirizzo@hotmail.it";

// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", host);

// Get session
Session session = Session.getDefaultInstance(props, null);

// Define message
MimeMessage message = new MimeMessage(session);

// Set the from address
message.setFrom(new InternetAddress(from));

// Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));

// Set the subject
message.setSubject("Hello JavaMail");

// Set the content
message.setText("Welcome to JavaMail");

// Send message
Transport.send(message);
}
}


Il problema è che già in passato avevo usato questo tipo di codice e mai nessun problema... E'capitato a qualcun altro??

Grazie...

k0nt3
09-11-2008, 10:41
EDIT: vuoi usare l'smtp di msn per mandare mail?

JCoder87
09-11-2008, 10:51
si... crea problemi che tu sappia?

k0nt3
09-11-2008, 10:54
penso che msn richieda l'autenticazione per mandare mail, ho trovato il codice per mandare mail con gmail (grazie a google :P ):

import java.security.Security;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SimpleMail
{
private String mailhost = "smtp.gmail.com";

public synchronized void sendMail(String subject, String body, String sender, String recipients)
throws Exception
{

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{ return new PasswordAuthentication("username","password"); }
});

MimeMessage message = new MimeMessage(session);
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setContent(body, "text/plain");
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));


Transport.send(message);

}


public static void main(String args[]) throws Exception
{
MailUtils mailutils = new MailUtils();
mailutils.sendMail("test", "test", "from@gmail.com", "To@gmail.com");

}

}

penso che per msn non serva impostare la porta 465, di default usa quella standard di smtp.
anche la riga che riguarda SSL non serve

JCoder87
09-11-2008, 11:18
ok.. testo e ti faccio sapere... alla fine posso usare anche google tranquillamente...:)

JCoder87
09-11-2008, 12:26
Ok... vaiggia alla grande!!

Grazie mille...

Doma sera se ho tempo inizio a vedere la ricezione..:mc:

JCoder87
27-11-2008, 10:25
Ciao a tutti, riprendo questo vecchio Topic per parlare della MessagingException, è un eccezione che può essere lanciata in varie occasioni nel utilizzo della libreria...

Il mio problema è che non capisco il motivo del suo lancio...
Cioè, ad esempio per l'AddressException capisco il perchè la lancia e so come gestirla, ma per la MessagingException come mi comporto?

Spero che qualcuno possa aiutarmi..

Grazie a tutti