Bom galera estou tetando enviar e-mail.No meu serviço que tem um servidor de e-mail eu consigo enviar normalmente mas para enviar fora da rede eu peciso de autenticação ai criei este codigo mas não obtive sucesso
ele só ficar em running não voltar erro algum.
codigo abaixo:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
/**
* A simple email sender class.
*/
public class SimpleSender
{
/**
* Main method to send a message given on the command line.
*/
public static void main(String args[])
{
try
{
String smtpServer= "mail.dominio.com.br";
String to="giovanni@dominio.com.br";
String from="giovanni@dominio.com.br";
String subject="teste";
String body="teste";
send(smtpServer, to, from, subject, body);
}
catch (Exception ex)
{
System.out.println("Usage: java com.lotontech.mail.SimpleSender"
+" smtpServer toAddress fromAddress subjectText bodyText");
}
System.exit(0);
}
/**
* "send" method to send the message.
*/
public static void send(String smtpServer, String to, String from
, String subject, String body)
{
try
{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
// props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
msg.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
transport.connect(smtpServer, "giovanni@dominio.com.br", "senha");
transport.sendMessage(msg, msg.getAllRecipients());
// -- Send the message --
Transport.send(msg);
System.out.println("Message sent OK.");
transport.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}