Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem with the code send SMTP email in java.
When I click button, it does not have any feedback (no JOptionPane) and I don't get mail.
I am beginner in java and I don't know how to fix it.
Please hepl me. Thank.
Here is my code:
Java
private void btn_mailActionPerformed(java.awt.event.ActionEvent evt) {
    Properties pro = new Properties();
    pro.put("mail.smtp.host","smtp.gmail.com");
    pro.put("mail.smtp.socketFactory.port","465");
    pro.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLServerSocketFactory");
    pro.put("mail.smtp.auth","true");
    pro.put("mail.smtp.port","465");
    Session session = Session.getDefaultInstance(pro,
            new javax.mail.Authenticator()
                {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("abc@gmail.com","abc") ;
                    }
                }
            );
    try
    {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("abc@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("xyz@gmail.com"));
        message.setSubject("Test Mail");
        message.setText("Hello how are you???");
        Transport.send(message);
        JOptionPane.showMessageDialog(null, "Send");
    }
    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
    }
Posted
Comments
Kuthuparakkal 15-Feb-15 8:39am    
package com.test;

import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.Test;

public class EmailService {

@Test
public void test(){
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", true); // added this line
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", "username");
props.put("mail.smtp.password", "password");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", true);



Session session = Session.getInstance(props,null);
MimeMessage message = new MimeMessage(session);

System.out.println("Port: "+session.getProperty("mail.smtp.port"));

// Create the email addresses involved
try {
InternetAddress from = new InternetAddress("username");
message.setSubject("Yes we can");
message.setFrom(from);
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("receivermail"));

// Create a multi-part to combine the parts
Multipart multipart = new MimeMultipart("alternative");

// Create your text message part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("some text to send");

// Add the text part to the multipart
multipart.addBodyPart(messageBodyPart);

// Create the html part
messageBodyPart = new MimeBodyPart();
String htmlMessage = "Our html text";
messageBodyPart.setContent(htmlMessage, "text/html");


// Add html part to multi part
multipart.addBodyPart(messageBodyPart);

// Associate multi-part with message
message.setContent(multipart);

// Send message
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", "username", "password");
System.out.println("Transport: "+transport.toString());
transport.sendMessage(message, message.getAllRecipients());


} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Source: StackOverFlow
Pandu Rang 15-Feb-15 11:23am    
Are you getting any exception?
Member 10390715 15-Feb-15 23:51pm    
No, it is silent.
Pandu Rang 16-Feb-15 0:47am    
are you sure both from and to address exists: most of the time smtp server performs checks the from address is of the same domain and it exists. we too faced such issue, and found that if we send the mail from valid from address which exists and from the same domain the mail goes.
Member 10390715 16-Feb-15 1:20am    
Thank. Both of address exists.I still use both address.
As to security, I use "abc@gmail.com" and "xyz@gmai.com" for example.

1 solution

Gmail is not good for testing. They ban you after you have received mails for a certain time - imho 15min.

Use a local mailserver like the free https://www.hmailserver.com/
(There is also a ban option in the setting, which can be switched off easily!)
 
Share this answer
 
Comments
Member 10390715 16-Feb-15 4:17am    
Thank you for your help. I will try it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900