This example demonstrates how to send a mail in Java with a package javax.mail-1.6.2.jar. These steps include creating a session of a configuration of parameters and sending a message of object.
Cods
1)
package com.beginner.examples;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmailExample {
public static void main(String[] args) {
// Parameter configuration
Properties pro = new Properties();
pro.put("mail.smtp.host", "smtp.xxx.com");
pro.put("mail.smtp.auth", "true");
pro.put("mail.smtp.prot", "25");
// Get the session object
Session session = Session.getInstance(pro);
// Create mail through a session
Message message = new MimeMessage(session);
try {
// Email address of the sender
message.setFrom(new InternetAddress("xxxxxx.com"));
// Set the address of the recipient
message.setRecipient(Message.RecipientType.CC, new InternetAddress("xxxxxxxxxx.com"));
// (you can set multiple recipients)
// message.setResipients(Message.RecipientType.CC,InternetAddress.parse("xxxx.com,yyyy.com,zzzz.com",false);
// Set the subject of your email
message.setSubject("Java");
// Set the content of the message
// You can also use the setContent() method
message.setText("I love Java");
message.setSentDate(new Date());
// Get the transport object through the session
Transport transport = session.getTransport("smtp");
transport.connect("Address of sender", "Email third-party server authorization code");
transport.sendMessage(message, message.getAllRecipients());
System.out.println("Send a success!");
transport.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
2)
package com.beginner.examples;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendEmailWithAttachment {
public static void main(String[] args) throws Exception {
// Parameter configuration
Properties pro = new Properties();
pro.setProperty("mail.smtp.host", "smtp server");
pro.setProperty("mail.smtp.auth", "true");
pro.setProperty("mail.smtp.prot", "25");
// Get the session object
Session session = Session.getInstance(pro);
// Create a Message
Message message = createMessage(session, "[email protected]", "[email protected]");
//Get transfer object
Transport transport = session.getTransport("smtp");
//Establish a connection
transport.connect("[email protected]", "password");
//Send the message
transport.sendMessage(message, message.getAllRecipients());
System.out.println("Done");
transport.close();
}
static Message createMessage(Session session, String from, String to) {
// Create a Message through the session
Message message = new MimeMessage(session);
MimeBodyPart part = new MimeBodyPart();
// Multipart is used to store multiple parts
Multipart multipart = new MimeMultipart();
FileDataSource source = new FileDataSource("test.html");
try {
// This method provides the mechanism to set this body part's content.
part.setDataHandler(new DataHandler(source));
part.setFileName(source.getName());
// Add bodyPart
multipart.addBodyPart(part);
// Set this body message's content
message.setContent(multipart);
message.setSubject("JAVA_Email");
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.CC, new InternetAddress(to));
} catch (MessagingException e) {
e.printStackTrace();
}
return message;
}
}
References
Imported packages in Java documentation: