Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help to get throw with the simplest sending mail example

User_H3Z8GOct 11 2021

I found example:

package sendmailtest;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendMailTest {
public static void main(String [] args) {
String to = "test@gmail.com";
String from = "myemail@gmail.com";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("smtp.gmail.com", host);
Session session = Session.getDefaultInstance(properties);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("subject test");
message.setText("test msg");
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}

I suppose I have to install my own email server as the example contains no password. Am I right? But is there API to send email via mailbox email server (eg gmail). Sorry for probably lame terms. Btw I need API that allow to insert images in a mail body.

Comments
Post Details
Added on Oct 11 2021
1 comment
165 views