Java Requires Chilkat v11.0.0+
Chilkat2 Java
Forward an Email using CreateForward
See more Email Object Examples
Reads an email from an IMAP server, creates a forward version of the email using the CreateForward method, and sends the email to another recipient.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.MailMan;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Read the 1st (most recent) email in an Inbox.
Imap imap = new Imap();
// Connect to an IMAP server.
imap.setSsl(true);
imap.setPort(993);
try {
imap.connect("imap.example.com");
// Login
imap.login("myLogin", "myPassword");
// Select an IMAP mailbox
imap.selectMailbox("Inbox");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
int numEmails = imap.getNumMessages();
// Fetch the email at the last sequence number.
// (We are assuming the Inbox has at least 1 email)
Email email = new Email();
try {
imap.fetchEmail(false, numEmails, false, email);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Disconnect from the IMAP server.
imap.disconnect();
System.out.println(email.getSubject());
Email eForward = new Email();
try {
email.toForward(eForward);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The eForward email has no To or CC recipients yet.
// Add one or more..
eForward.addTo("Joe", "joe@example.com");
// We also need to specify the From name/address.
eForward.setFromAddress("matt@someMailServer.com");
eForward.setFromName("Matt");
// If we wish to add text at the start of the email body:
StringBuilder sbHtmlBody = new StringBuilder();
if (eForward.hasHtmlBody()) {
sbHtmlBody.append(eForward.getHtmlBody());
sbHtmlBody.prepend("<p>Hello, this is an email I'm forwarding to you...</p>");
eForward.setHtmlBody(sbHtmlBody.getAsString());
}
StringBuilder sbPtBody = new StringBuilder();
if (eForward.hasPlainTextBody()) {
sbPtBody.append(eForward.getPlainTextBody());
sbPtBody.prepend("Hello, this is an email I'm forwarding to you...\r\n\r\n");
eForward.setTextBody(sbPtBody.getAsString(), "text/plain");
}
// We could save the .eml, then double-click on it to view in our mail program, such as Outlook or Thunderbird..
eForward.saveEml("qa_output/forward.eml");
// We could send (forward) the email..
MailMan mailman = new MailMan();
mailman.setSmtpHost("smtp.example.com");
mailman.setSmtpUsername("myLogin");
mailman.setSmtpPassword("myPassword");
mailman.setSmtpPort(587);
mailman.setStartTLS(true);
try {
mailman.sendEmail(eForward);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
try {
mailman.closeSmtpConnection();
} catch (ChilkatException e) {
System.out.println("Connection to SMTP server not closed cleanly.");
}
System.out.println("Mail Sent!");
}
}