Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active January 18, 2022 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/7126ba23a3ba099642e13dbfbfd94970 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/7126ba23a3ba099642e13dbfbfd94970 to your computer and use it in GitHub Desktop.
Aspose.Email - Create and Send Emails
// Create a new instance of MailMessage class
MailMessage message = new MailMessage();
// Set subject of the message, Html body and sender information
message.Subject = "New message created by Aspose.Email for .NET";
message.HtmlBody = "<b>This line is in bold.</b> <br/> <br/>" + "<font color=blue>This line is in blue color</font>";
message.From = new MailAddress("from@domain.com", "Sender Name", false);
// Add To recipients and CC recipients
message.To.Add(new MailAddress("to1@domain.com", "Recipient 1", false));
message.CC.Add(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Save message in EML/EMLX/MSG/MHTML format
message.Save("EmailMessage.eml", SaveOptions.DefaultEml);
// Create a new instance of MailMessage class
MailMessage message = new MailMessage();
// Set subject of the message, Html body, sender, and receiver information
message.Subject = "New message created by Aspose.Email for .NET";
message.HtmlBody = "<b>This line is in bold.</b> <br/> <br/>" + "<font color=blue>This line is in blue color</font>";
message.From = new MailAddress("from@domain.com", "Sender Name", false);
message.To.Add(new MailAddress("to1@domain.com", "Recipient 1", false));
// Specify encoding
message.BodyEncoding = Encoding.ASCII;
// Save message in EML/EMLX/MSG/MHTML format
message.Save("EmailMessage.msg", SaveOptions.DefaultMsgUnicode);
// Create a new instance of MailMessage class
MailMessage message = new MailMessage();
// Set subject of the message, body and sender information
message.Subject = "New message created by Aspose.Email for .NET";
message.Body = "This is the body of the email.";
message.From = new MailAddress("from@domain.com", "Sender Name", false);
// Add To recipients and CC recipients
message.To.Add(new MailAddress("to1@domain.com", "Recipient 1", false));
message.CC.Add(new MailAddress("cc1@domain.com", "Recipient 3", false));
// Add attachments
message.Attachments.Add(new Attachment("word.docx"));
// Save message in EML, EMLX, MSG and MHTML formats
message.Save("EmailMessage.eml", SaveOptions.DefaultEml);
message.Save("EmailMessage.emlx", SaveOptions.CreateSaveOptions(MailMessageSaveType.EmlxFormat));
message.Save("EmailMessage.msg", SaveOptions.DefaultMsgUnicode);
message.Save("EmailMessage.mhtml", SaveOptions.DefaultMhtml);
// Create SmtpClient and specify server, port, user name and password
SmtpClient client = new SmtpClient("mail.server.com", 25, "Username", "Password");
// 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.Add(message1);
manyMsg.Add(message2);
manyMsg.Add(message3);
try
{
// Send messages
client.Send(manyMsg);
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
// Create MailMessage instance. You can create a new message or load a already created message file (eml, msg, etc.)
MailMessage msg = MailMessage.Load("EmailMessage.msg");
// Create an instance of SmtpClient class
SmtpClient client = new SmtpClient();
// Specify your mailing Host, Username, Password, Port # and Security option
client.Host = "mail.server.com";
client.Username = "username";
client.Password = "password";
client.Port = 587;
client.SecurityOptions = SecurityOptions.SSLExplicit;
// Specify your mailing Host, Username, Password, Port # and Security option
client.Host = "mail.server.com";
client.Username = "username";
client.Password = "password";
client.Port = 587;
client.SecurityOptions = SecurityOptions.SSLExplicit;
// Send messages
client.SendAsync(msg);
Console.WriteLine("Sending message...");
msg.Dispose();
// Create MailMessage instance. You can create a new message or load a already created message file (eml, msg, etc.)
MailMessage msg = MailMessage.Load("EmailMessage.msg");
// Create an instance of SmtpClient class
SmtpClient client = new SmtpClient();
// Specify your mailing Host, Username, Password, Port # and Security option
client.Host = "mail.server.com";
client.Username = "username";
client.Password = "password";
client.Port = 587;
client.SecurityOptions = SecurityOptions.SSLExplicit;
try
{
// Send this email
client.Send(msg);
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment