Skip to main content

JavaMail Authentication Problem

2 replies [Last post]
frost750
Offline
Joined: 2006-12-22
Points: 0

Brand new to the JavaMail API. Using the following code to try and send a message from my Java desktop application:

String protocol = "smtp";

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.washington.edu");
props.put("mail.from", "*********@u.washington.edu");
props.put("mail." + protocol + ".auth", "true");
Session session = Session.getInstance(props, null);

try
{ MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, "********@hotmail.com");
msg.setSubject("a test");
msg.setSentDate(new Date());
msg.setText("a test message\n");
Transport t = session.getTransport(protocol);

try
{ t.connect("*******", "*********");
t.sendMessage(msg, msg.getAllRecipients());
} finally { t.close(); }

} catch (MessagingException mex) { System.out.println("send failed, exception: " + mex); }

The usernames and passwords have been replaced with the ****. This question has probably been asked before but I cannot find a thread with search. The error i get is:

send failed, exception: javax.mail.SendFailedException: Invalid Addresses;
nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 530 5.7.1 Relaying denied. Proper authentication required.

The username, password and mail server are all valid. Thanks for any suggestions.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
frost750
Offline
Joined: 2006-12-22
Points: 0

Problem was solved by adding the following line of code.

props.put("mail.smtp.starttls.enable","true");

scorpius87
Offline
Joined: 2010-12-01
Points: 0

Hi, I have a problem with this code. I compile in Console project and all is OK, but when I want to compile in a desktop application project doesn't work. I don't know what happend with this code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

try {

postMail("Prueba de envio", "Saludos", "luisramirezsilva87@gmail.com");

} catch (Exception e) {

e.printStackTrace();

}

}

public static void postMail(String Asunto, String Mensaje, String Correo) {

boolean exito = true;

try {

String TO = Correo;//Destinatario

java.util.Properties props = new java.util.Properties();

props.put("mail.smtp.host", "gmail.smtp.com");//Con este ip envia a todos los correos incluido los de la uis

Session s = Session.getInstance(props);

MimeMessage message = new MimeMessage(s);

InternetAddress from = new InternetAddress(Correo);

message.setFrom(from);

InternetAddress to = new InternetAddress(Correo);//Correo destinatario

message.addRecipient(Message.RecipientType.TO, to);

message.setSubject(Asunto);

MimeMultipart mm = new MimeMultipart();

MimeBodyPart mbp = new MimeBodyPart();

String mensaje = Mensaje;

System.out.println(mensaje);

mbp.setContent(mensaje, "text/html");

mm.addBodyPart(mbp);

message.setContent(mm);

Transport.send(message);

} catch (javax.mail.MessagingException er) {

System.out.println("ha ocurrido un fallo en cls_correo.enviarCorreo");

System.out.println("Mensaje: " + er.getMessage());

exito = false;

}

System.out.println("Operación correo:" + exito);

System.out.print("mensaje enviado");

}

Please somebody HELP ME! :)