Last active
January 31, 2022 06:45
-
-
Save aspose-com-gists/2836280bbb0c0b9ea7df9fd3765751fa to your computer and use it in GitHub Desktop.
Create and Send Emails in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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