Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create and Send Emails in Java
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.setSubject("New message created by Aspose.Email for .NET");
message.setBody("This is the body of the email.");
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));
// Add To recipients and CC recipients
message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false));
message.getCC().addItem(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Add attachments
message.getAttachments().addItem(new Attachment("word.docx"));
// Save message in EML, EMLX, MSG and MHTML formats
message.save("EmailMessage.eml", SaveOptions.getDefaultEml());
message.save("EmailMessage.emlx", SaveOptions.createSaveOptions(MailMessageSaveType.getEmlxFormat()));
message.save("EmailMessage.msg", SaveOptions.getDefaultMsgUnicode());
message.save("EmailMessage.mhtml", SaveOptions.getDefaultMhtml());
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.setSubject("New message created by Aspose.Email for .NET");
message.setHtmlBody("<b>This line is in bold.</b> <br/> <br/> <font color=blue>This line is in blue color</font>");
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));
// Specify encoding
message.setBodyEncoding(Charset.forName("US-ASCII"));
// Add To recipients and CC recipients
message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false));
message.getCC().addItem(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Save message in EML, EMLX, MSG and MHTML formats
message.save("EmailMessage.eml", SaveOptions.getDefaultEml());
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.setSubject("New message created by Aspose.Email for .NET");
message.setHtmlBody("<b>This line is in bold.</b> <br/> <br/> <font color=blue>This line is in blue color</font>");
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));
// Add To recipients and CC recipients
message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false));
message.getCC().addItem(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Save message in EML, EMLX, MSG and MHTML formats
message.save("EmailMessage.eml", SaveOptions.getDefaultEml());
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.setSubject("New message created by Aspose.Email for .NET");
message.setHtmlBody("<b>This line is in bold.</b> <br/> <br/> <font color=blue>This line is in blue color</font>");
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));
// Set notification options
message.setDeliveryNotificationOptions(DeliveryNotificationOptions.OnSuccess);
message.getHeaders().add("Return-Receipt-To", "from@domain.com");
message.getHeaders().add("Disposition-Notification-To", "from@domain.com");
// Add To recipients
message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false));
// Create an instance of SmtpClient Class
SmtpClient client = new SmtpClient();
// Specify your mailing host server, Username, Password, Port
client.setHost("smtp.server.com");
client.setUsername("Username");
client.setPassword("Password");
client.setPort(25);
try
{
// Client.Send will send this message
client.send(message);
}
catch (Exception ex)
{
ex.printStackTrace();
}
// Create instances of MailMessage class and specify To, From, Subject and Message
MailMessage message1 = new MailMessage("msg1@from.com", "msg1@to.com", "Subject1", "message1, how are you?");
MailMessage message2 = new MailMessage("msg1@from.com", "msg2@to.com", "Subject2", "message2, how are you?");
MailMessage message3 = new MailMessage("msg1@from.com", "msg3@to.com", "Subject3", "message3, how are you?");
// Create an instance of MailMessageCollection and add email messages to it
MailMessageCollection manyMsg = new MailMessageCollection();
manyMsg.addItem(message1);
manyMsg.addItem(message2);
manyMsg.addItem(message3);
// Create SmtpClient and specify server, port, user name and password
SmtpClient client = new SmtpClient("mail.server.com", 25, "Username", "Password");
try
{
// Send messages
client.send(manyMsg);
}
catch (Exception ex)
{
ex.printStackTrace();
}
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.setSubject("New message created by Aspose.Email for .NET");
message.setHtmlBody("<b>This line is in bold.</b> <br/> <br/> <font color=blue>This line is in blue color</font>");
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));
// Specify encoding
message.setBodyEncoding(Charset.forName("US-ASCII"));
// Add To recipients and CC recipients
message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false));
message.getCC().addItem(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Create an instance of SmtpClient Class
SmtpClient client = new SmtpClient();
// Specify your mailing host server, Username, Password, Port
client.setHost("smtp.server.com");
client.setUsername("Username");
client.setPassword("Password");
client.setPort(25);
try
{
// Client.beginSend will send this message
client.beginSend(message);
}
catch (Exception ex)
{
ex.printStackTrace();
}
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.setSubject("New message created by Aspose.Email for .NET");
message.setHtmlBody("<b>This line is in bold.</b> <br/> <br/> <font color=blue>This line is in blue color</font>");
message.setFrom(new MailAddress("from@domain.com", "Sender Name", false));
// Specify encoding
message.setBodyEncoding(Charset.forName("US-ASCII"));
// Add To recipients and CC recipients
message.getTo().addItem(new MailAddress("to1@domain.com", "Recipient 1", false));
message.getCC().addItem(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Create an instance of SmtpClient Class
SmtpClient client = new SmtpClient();
// Specify your mailing host server, Username, Password, Port
client.setHost("smtp.server.com");
client.setUsername("Username");
client.setPassword("Password");
client.setPort(25);
try
{
// Client.Send will send this message
client.send(message);
}
catch (Exception ex)
{
ex.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.