How to send Emai using Java-SMTP (Without images/Attachments)

Dependency:
<dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.4.4</version>
</dependency>

Code:

import java.util.Date;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
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.MimeMutipart;

Pubic void SendEmail(){

Properties prop = new Properties();
props.put("mail.smtp.host", Your_hostname);
props.put("mail.smtp.port", Your_port);
props.put("mail.debug", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);

//Set Email address from where you want to send email(Example: XYZ@gmail.com)
message.setFrom(new InternetAddress("XYZ@gmail.com");


//Set Email address to which you want to send email(Example: ABC@gmail.com)
message.setRecipient(Recipient.TO, new InternetAddress("ABC@gmail.com"));

//set subject
message.setSubject("This is subject");

//set body content
message.setText("This is body")
message.setSentDate(new Date());

Transport t = session.getTransport("smtp");
try{
    t.connect();
    Transport.send(message);
}finally{
    t.close();
}
}

Comments

Popular Posts