A mail user agent in java solution
Here is a simple example of a Mail User Agent (MUA) in Java using the JavaMail API:
MailUserAgent.java
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class MailUserAgent {
public static void main(String[] args) {
// Set up mail server properties
Properties props = new Properties();
props.put("mail.smtp.host", "your-smtp-host");
props.put("mail.smtp.port", "your-smtp-port");
props.put("mail.smtp.auth", "true");
// Create a session
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your-email-username", "your-email-password");
}
});
// Create a message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your-email-username"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient-email-username"));
message.setSubject("Test email");
message.setText("This is a test email");
// Send the message
Transport.send(message);
}
}
This code sets up a mail session with the specified SMTP host, port, and authentication credentials. It then creates a new message with a from address, to address, subject, and text body. Finally, it sends the message using the Transport.send()
method.
Note: You'll need to replace the placeholders (your-smtp-host
, your-smtp-port
, your-email-username
, your-email-password
, and recipient-email-username
) with your actual mail server settings and email credentials.
Additional features:
To add more features to your MUA, you can use the JavaMail API's various classes and methods. For example:
- To add attachments, you can use the
MimeBodyPart
class and itsaddAttachment()
method. - To set a reply-to address, you can use the
Message.setReplyTo()
method. - To set a priority for the message, you can use the
Message.setPriority()
method. - To set a custom header, you can use the
Message.setHeader()
method.
Here's an updated example that adds an attachment and sets a reply-to address:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class MailUserAgent {
public static void main(String[] args) {
// Set up mail server properties
Properties props = new Properties();
props.put("mail.smtp.host", "your-smtp-host");
props.put("mail.smtp.port", "your-smtp-port");
props.put("mail.smtp.auth", "true");
// Create a session
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your-email-username", "your-email-password");
}
});
// Create a message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your-email-username"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient-email-username"));
message.setSubject("Test email");
message.setText("This is a test email");
// Add an attachment
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName("example.txt");
attachment.setContent("This is an example attachment", "text/plain");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(attachment);
message.setContent(multipart);
// Set a reply-to address
message.setReplyTo(new InternetAddress("reply-to-email-username"));
// Send the message
Transport.send(message);
}
}
This code adds an attachment to the message using the MimeBodyPart
class and sets a reply-to address using the Message.setReplyTo()
method.