|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Oct 2008
Città: Bergamo
Messaggi: 12
|
[Java] - JavaMail
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 = "[email protected]"; String to = "[email protected]"; // 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... |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Dec 2005
Messaggi: 7260
|
EDIT: vuoi usare l'smtp di msn per mandare mail?
|
|
|
|
|
|
#3 |
|
Junior Member
Iscritto dal: Oct 2008
Città: Bergamo
Messaggi: 12
|
si... crea problemi che tu sappia?
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Dec 2005
Messaggi: 7260
|
penso che msn richieda l'autenticazione per mandare mail, ho trovato il codice per mandare mail con gmail (grazie a google :P ):
Codice:
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", "[email protected]", "[email protected]");
}
}
anche la riga che riguarda SSL non serve |
|
|
|
|
|
#5 |
|
Junior Member
Iscritto dal: Oct 2008
Città: Bergamo
Messaggi: 12
|
ok.. testo e ti faccio sapere... alla fine posso usare anche google tranquillamente...
|
|
|
|
|
|
#6 |
|
Junior Member
Iscritto dal: Oct 2008
Città: Bergamo
Messaggi: 12
|
Ok... vaiggia alla grande!!
Grazie mille... Doma sera se ho tempo inizio a vedere la ricezione.. |
|
|
|
|
|
#7 |
|
Junior Member
Iscritto dal: Oct 2008
Città: Bergamo
Messaggi: 12
|
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 |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 07:40.



















