|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Apr 2004
Messaggi: 364
|
[Java] Mandare email
Salve ragazzi, sto cercando di inviare email in java, però ho un problema, che penso che derivi dall'host di libero, con il quale sto cercando di inviare email, comunque posto il codice con la speranza che qualcuno mi aiuti.
Codice:
public void manda()throws Exception
{
URL url=new URL("http://193.70.192.25");
URLConnection uc = url.openConnection();
System.setProperty("user.fromaddr", "[email protected]");
PrintStream ps = new PrintStream(uc.getOutputStream());
System.out.println("Sending SMTP headers");
ps.print("Subject: Test\r\n");
ps.flush();
ps.print("From: [email protected]\r\n");
ps.flush();
System.out.println("Sending blank line");
ps.print("r\n");
ps.flush();
// Body
System.out.println("Sending body");
ps.print("It's just\r\n");
ps.flush();
ps.print("a little test\r\n");
ps.flush();
// Finish
System.out.println("Closing stream");
ps.close();
Thread.sleep(100000);
System.out.println("Finished");
}
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: May 2005
Messaggi: 340
|
perche metti un'url invece di un server smtp? :S
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Apr 2004
Messaggi: 364
|
Vorresti dire che la stringa che passo al costruttore della classe URL, deve contenere il nome di un server smtp?
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Jun 2001
Città: Alessandria (provincia)
Messaggi: 4772
|
Sicuro che sia il modo giusto?
Stai scrivendo istruzioni che mi sembrano abbastanza sensate per inviare una email tramite telnet sulla porta 25 di un server SMTP, però non mi pare tu gestisca le risposte del server. E ti connetti, come già detto, tramite protocollo http (e non SMTP) sulla porta 80.... Prova a usare la libreria javaMail (l'ho scritto alla veloce, potrebbero esserci errori) Codice:
try
{
String nomeServerSmtp = "smtp.libero.it"; // controlla il server
String emailFrom = "[email protected]";
String emailTo = "[email protected]";
String mailSubject = "Titolo di prova";
String mailBody = "Corpo del messaggio";
Properties prop = System.getProperties();
prop.put ("mail.host", nomeServerSmtp);
prop.put ("mail.transport.protocol", "smtp");
javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(prop, null);
mailSession.setDebug(false);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(emailFrom));
InternetAddress[]toAddress = {new InternetAddress(emailTo)};
msg.setRecipients(Message.RecipientType.TO, toAddress);
// oppure: msg.setRecipients(Message.RecipientType.BC, toAddress);
// oppure: msg.setRecipients(Message.RecipientType.BCC, toAddress);
msg.setSubject(mailSubject);
msg.setSentDate(new Date());
msg.setText(mailBody);
Transport.send(msg);
}
catch (Exception e) {System.out.println("Eccezione: "+e);}
|
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Apr 2004
Messaggi: 364
|
Ho provato ad usare il tuo codice, saresti così gentile da dirmi quali import devo aggiungere, ho provato ad usare l'import di javax.mail, ma dice che il package non esiste
|
|
|
|
|
|
#6 | |
|
Senior Member
Iscritto dal: Feb 2002
Città: Trento
Messaggi: 962
|
Quote:
__________________
"Et Eärallo Endorenna utúlien. Sinome maruvan ar Hildinyar tenn' Ambar-metta!" -- Aragorn Elessar, Heir of Isildur Mixmar -- OpenSuSE 11.1 on AMD 64 3000+ on DFI LanParty nF4-D | GeForce 6600 GT + Thermaltake Schooner on Samsung 710N Storage -- ( 2 x Hitachi Deskstar 80 Gb + 1 x Hitachi 250 Gb ) = 1 RAID 5 + 1 Storage space LaCie Ethernet Disk Mini 250 Gb | HP - DV2150 EL MILAN CLAN |
|
|
|
|
|
|
#7 | |
|
Senior Member
Iscritto dal: Jun 2001
Città: Alessandria (provincia)
Messaggi: 4772
|
Quote:
http://java.sun.com/products/javamai...ads/index.html Spero non sia cambiata molto rispetto al codice che ho scritto. |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 10:38.



















